mardi 1 septembre 2020

Problem with memory allocation for an array

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int main()
{
    int n,i,j,t,a[t],z,min=10000000,max=0;
    do
    {
        scanf("%d",&n);
    }
    while(n<=5 || n>20);
    for(i=0;i<n;i++)
    {
    t=(10)+rand()%(10-1000+1);
    a[t]=(int*)malloc(t*sizeof(int));
    for(j=0;j<t;j++)
    {
        a[t]=(-10)+rand()%(-10+1);
        if(a[t]<min) min=a[t];
        if(a[t]>max) max=a[t];
    }
    
    }
    printf("%d",z=abs(min-max));
    return 0;
    
}

The task is to enter a number from <5,20]. Then generate an integer t [10,1000] n number of times and allocate the memory for an array that has a size of t. Fill every array with [-10,0]. Find the smallest and the largest element and printf out the module of their difference.

The compiler only shows this error but im not sure if the rest of the code is good.

15 6 C:\Users\x\Documents\asfag.cpp [Error] invalid conversion from 'int' to 'int' [-fpermissive]




Aucun commentaire:

Enregistrer un commentaire