0

Currently I am trying to fill an array of size num with random values. To do this, I need to create two functions:

1: Write a function (*createdata) that allocates a dynamic array of num double values, initialising the values to 0.0.

2: Write a different function (gendata) that will populate an array of double values with random values generated using the rand() function.

Here is my attempt at writing how the functions operate (outside main() ):

double *createdata(int num)
  {
         int i = 0;

         double *ptr;

         ptr = (double *)malloc(sizeof(double)*num);     

         if(ptr != NULL)
         {
           for(i = 0; i < num; i++)
           {
                 ptr[i] = 0.0;       
           }
         } 
  }  

double gendata(int num)
  {

         createdata(num); 

         int j = 0;

         for(j = 0; j < num; j++)
           {
                 ptr[j] = rand();       
           }               
  }

However, I know that there is something certainly wrong with the above.

What I would like is that once I have called both functions in main, I will have generated an array of size num that is filled with random numbers.

  • A tiny hint: there is no return statement in any of your functions. – Howard Dec 08 '12 at 17:36
  • @Howard If I add return(ptr) at the end of my *createdata function would that do the trick? –  Dec 08 '12 at 17:41
  • 1
    Your `gendata()` function does not match the specification _populate an array of `double` with random values_; it matches the specification _generate and populate an array of `double` with random values_. You can revise the specification or the code; on the whole, revising the code will lead to a more useful function. You then don't need to return anything from `void gendata(double *array, int num)`. – Jonathan Leffler Dec 08 '12 at 18:52

4 Answers4

2

You have to pass the allocated pointer to the gendata function, because in it's current form, ptr is unknown. Does that even compile?

Example:

double *createdata(int num)
{
    int i = 0;
    double *ptr;

    ptr = (double *)malloc(sizeof(double)*num);

    if(ptr != NULL)
    {
        for(i = 0; i < num; i++)
        {
            ptr[i] = 0.0;
        }
    }
    return ptr;
}

And:

double gendata(int num)
{
    double *ptr = createdata(num);
    int j = 0;

    if(ptr != NULL)
    {
        for(j = 0; j < num; j++)
        {
            ptr[j] = rand();
        }
    }
}

Also note I'm checking for return NULL again from malloc.

Other hints others might say here:

  • Don't cast the return of malloc.
  • Don't forget to free the pointer after you use it entirely.
  • You're not returning anything in your function gendata, but you declared it as double. Use void instead if you're not going to return anything.

But you're probably gonna need to return the pointer from it anyways, so that you can use it later in other functions, such as main.

EDIT: So, this would be like this, as an example:

double *gendata(int num)
{
    double *ptr = createdata(num);
    int j = 0;

    if(ptr != NULL)
    {
        for(j = 0; j < num; j++)
        {
            ptr[j] = rand();
        }
    }
    return ptr;
}

And in your main:

int main(void)
{
    double *data;
    data = gendata(100);
    printf("%g\n", data[5]);

    // When you're done, call free
    free(data);
    return 0;
}
Community
  • 1
  • 1
Toribio
  • 3,963
  • 3
  • 34
  • 48
  • Thanks for your answer. Ok, now I'm inside my main(), and to test my functions I want to print out some value from the array that I have generated. So inside main(), I call the function ( gendata(numvals); ) then I try to print the second value from the array ( printf( "%lf", ptr[1] ); ) however this isn't working. What do I need to change? –  Dec 08 '12 at 17:55
  • If you know you shouldn't cast the return of `malloc`, why are you doing it? – user93353 Dec 08 '12 at 18:01
  • @user93353 Because it's not relevant to the question. It's just a hint, and I also don't support it very much. – Toribio Dec 08 '12 at 18:03
0

You need to return the array allocated in the createdata() function, otherwise you can't use it in gendata(), or anywhere else.

I'm not sure why gendata() is declared as returning a double, either.

JasonD
  • 16,464
  • 2
  • 29
  • 44
  • Hi Jason, how would I edit my code so that occurs? (I'm only getting acquainted with pointers at the moment!). –  Dec 08 '12 at 17:40
  • Flávio Toribio's answer shows what I mean, I suggest reading that. – JasonD Dec 08 '12 at 17:42
0

Well, I'm not quite familiar with how malloc works, although it looks like that will work fine, so I can't evaluate that, but the only issues I can see here are this:

You aren't returning ptr in your createdata function. So when you try to access ptr in your gendata function, there is no ptr array in scope. That will give you an error. Also, gendata function does not even return anything.

Look at this code, this should work:

double *createdata(int num)
{
     int i = 0;

     double *ptr;

     ptr = (double *)malloc(sizeof(double)*num);     

     if(ptr != NULL)
     {
       for(i = 0; i < num; i++)
       {
             ptr[i] = 0.0;       
       }
     } 
     return ptr; // return the pointer
  }  

double *gendata(int num) // you forgot the '*' here
{

     double *ptr = createdata(num); 

     int j = 0;

     for(j = 0; j < num; j++)
       {
             ptr[j] = rand();       
       }     
     return ptr; // i added this so you obviously return the pointer 
  }

I believe this will work, here's an example of using it:

int main()
{
    int c;
    double *data = gendata(8);
    for(c = 0; c < 8; c++)
    {
        printf("%f\n", data[c]);
    }
}
wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
Name
  • 2,037
  • 3
  • 19
  • 28
0

In createdata(), you forgot to actually return the pointer to the newly allocated array at the end:

return ptr;

Also, your gendata() function needs to work on an already existing array, not generate its own. You should add it as an argument. Also, it doesn't need to return anything. So:

void gendata(double ptr[], int num)
{
    int j = 0;
    if (ptr != NULL) {
        for(j = 0; j < num; j++) {
            ptr[j] = rand();
        }
    }
}

However, you can simplify the above and reduce the nesting level:

void gendata(double ptr[], int num)
{
    int j = 0;
    if (ptr == NULL)
        return;

    for(j = 0; j < num; j++) {
        ptr[j] = rand();
    }
}

Note that double ptr[] actually means the same as double* ptr, but the [] syntax makes it more apparent that what the function wants is a pointer to an array of doubles rather than a pointer to a single double.

So now in order to create an array and then randomize it, you would do:

double* array = createdata(N); // Create an array of N doubles.
gendata(array, N); // Randomize it.
Nikos C.
  • 50,738
  • 9
  • 71
  • 96