0

I've been assigned the task of writing a program which dinamically allocates n arrays of random sizes t, fills them with random values, finds the smallest and the largest elements and then prints out the module of their difference.

The value of n entered by the user must be in the range (5, 20], t should be generated in the range [10, 1000] and the range of the values used to fill those arrays should be [-10, 0].

This is my attempt:

#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));  // <-- This generates an error
    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 compiler only shows this error, but I'm not sure if the rest of the code is good.

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

What am I doing wrong?

Bob__
  • 12,361
  • 3
  • 28
  • 42
  • `a[t]` should be `*a`, and `a[t]=(int*)malloc(t*sizeof(int));` should be `a=malloc(t*sizeof(int));` don't forget to `free` memory – Iłya Bursov Sep 01 '20 at 20:32
  • Tha fixed the error but im not getting any output – jurepure123 Sep 01 '20 at 20:43
  • the value of t is good its % -989 not -9989 – jurepure123 Sep 01 '20 at 20:52
  • Do try and get out of the habit of declaring a whole heap of variables on one line, and make an effort to give these *meaningful names*. Tip: You can often declare loop variables inside the loop itself, reducing clutter and making it easier to verify the loop is correct: `for (int i = 0; ...)` is way better than declaring `int i` somewhere else where you need to go looking for it. – tadman Sep 01 '20 at 21:17

1 Answers1

1

There are some problems in your code but let's focus on the clear errors rather than the logic, that is something you should address yourself.

The line a[t]=(int*)malloc(t*sizeof(int)); is bad at several levels.

Let's suppose t is -500, you assign to the int stored in the array index a[-500] a memory block with the size of likely -2000 bytes deppending on the size of an int in your system.

The problem is arrays don't have negative indexes, memory blocks cannot have negative values and they must be assigned to pointers not ints which is what would be stored in your array at index -500 (??), if it existed I guess. What's more, you reallocate memory to the same pointer inside the loop which you cannot do that's what's realloc is for. In addition you are reallocating the same size over and over again wich makes litle sense.

Move the allocation to before the loop with a correct allocation, it would look like:

int *a;
//...
t = rand() % (1000 - 10); //assign a positive value to t
a = malloc(t * sizeof *a);
//...

Provided that t is not negative.

Casting malloc is also not a good habit.

Also this assignment:

 a[t] = (-10) + rand() % (-10 + 1);

You are assigning a random value to the same array index over the whole cycle, which by the way represents one past the end of the array bounds (provided that the allocation is corrected), you likely want a[i].

This is a good start, your code will compile and run, now you need to address the logic, which also desperately needs attention. If you are to learn to code you should do this on your own.

I also suggest, as a good method to develop your skills, the reading of a good C book.

anastaciu
  • 23,467
  • 7
  • 28
  • 53