current position:Home>Write a function to randomly generate an array of length n with elements from 0 to 99

Write a function to randomly generate an array of length n with elements from 0 to 99

2022-02-02 19:17:32 CSDN Q & A

img


This is the requirement of the topic , I'm doing the first function , The title says n It's array length , But I don't know how to use that n. I use n=sizeof(a)/sizeof(a[0]) Will report a mistake .
Then I just don't care n, Defined #define N 10, Then call this function two times. , The two arrays generated are the same , Why is that

img

img




Refer to the answer 1:
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <windows.h>void Sort(int *p,int n){    int min,i,j,t;    for(i=0;i<n-1;i++)    {        min=i;        for(j=i+1;j<n;j++)            if(p[min]>p[j])                min=j;        t=p[i];p[i]=p[min];p[min]=t;    }}void Print(int *p,int n){    int j=0;    while(j<n)    {        printf("%d ",p[j]);        j++;    }    printf("\n");}void Init(int a[],int n){    int j;    srand((unsigned)time(NULL));     for( j = 0; j < n; j++)         {         a[j] = rand()%99;         }}int main(){        int j=0;        int n;        printf(" Please enter n:");        scanf("%d",&n);        int a[n];        Init(a,n);        printf(" Original array a[]:");Print(a,n);        Sort(a,n);        printf(" After ordering a[]:");Print(a,n);        Sleep(1000);         int b[n];        Init(b,n);        printf(" Original array b[]:");Print(b,n);        Sort(b,n);        printf(" After ordering b[]:");Print(b,n);        int n2=n*2;         int c[n2];        while(j<n)        {            c[j]=a[j];            j++;        }        while(j<n2)        {            c[j]=b[j-n];            j++;        }        printf(" Original array c[]:");Print(c,n2);        Sort(c,n2);        printf(" After ordering c[]:");Print(c,n2);        return 0;    }

img




Refer to the answer 2:



Refer to the answer 3:
#include <stdio.h>#include <time.h>#include <stdlib.h>void Init(int a[],int n){    int c;    for (int i=0;i<n;i++)    {        c=rand()%100;        a[i]= c;    }}void Print(int *p,int n){    for (int i=0;i<n;i++)    {        printf(" %2d", p[i]);    }    printf("\n");}int main(){    srand((unsigned int)time(NULL));    int b[3];    Init(b,3);    printf("b:");Print(b,3);    int a[5];    Init(a,5);    printf("a:");Print(a,5);    return 0;}//b: 2 2 84//a: 23 79 52 60 68//



Refer to the answer 4:



Refer to the answer 5:
#include <stdio.h>#include <stdlib.h>#include <time.h>#define N 10void Init(int a[], int n);int a[N];int main(void){    int i;    Init(a, N);        for(i = 0; i < N; i++)        printf("%d ", a[i]);    return 0;}void Init(int a[], int n){    time_t t;        /*  Initialize the random number generator  */    srand((unsigned)time(&t));        while(n >= 0)        a[--n] = rand() % 100;}



Refer to the answer 6:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021917263792.html

Random recommended