2

I am completing an assignment that is helping me understand the use of pointers. I feel like I have a good grasp except when it comes to returning the pointer from a function. Here is the code I'm having trouble with:

#include <stdlib.h>
#include <stdio.h>
//#include <time.h>

double *ptr;
double * userallocate(double numbers)
{
    if (numbers < 1000000)
    {
        ptr = malloc(sizeof(double)*numbers); 
        return ptr;
    }
    else
    {
        printf("You have allocated too many numbers... SHAME\n");
        exit(0);
    }
}

double * gendata(double *ptr, double numbers)
{
    int i;
    for (i=0;i<numbers;i++)
        ptr[i] = rand()/RAND_MAX;
    return ptr;
}

void average(double numbers)
{
    int i;
    double sum, average;
    for (i=0;i<numbers;i++)
        sum+=ptr[i];
    average = sum/numbers;
    printf("%f,",sum);
    printf("We have averaged %d random numbers for you.\n The average is %f",(int) numbers,average);
}
int main(int argc, char *argv[])
{
    double num;
    if (argc !=2)
    {
        printf("Not enough arguments... SHAME\n");
        exit(0);
    }

    num = atof(argv[1]);
    
    userallocate(num);
    gendata(ptr,num);
    average(num);
    getchar();
    return 0;
}

The first function userallocate() is meant to create a buffer that contains space for double precision with the size determined by the command line argument. My code compiles but when I try to put random numbers into the buffer and average them I get 0.0000. I think there is a problem with the way I am returning the pointer in userallocate() and passing it to the other functions. Any help would be appreciated!

8
  • 1
    Returning and passing your pointer works, but uses a global variable. This is considered bad practice. Make it a local variable instead - that is, move the double *ptr; from line 5 in your code inside the main function, that is, together with double num; Commented Jan 26, 2017 at 18:05
  • Although @jxh pointed out the averaging problem your comment is more what I'm struggling to understand. When I place double *ptr inside main then I get a compiling error error: ‘ptr’ undeclared (first use in this function) ptr = malloc(sizeof(double)*numbers);as well as the other functions ^ Commented Jan 26, 2017 at 18:12
  • Oh right, you should have it in userallocate too. And also in average (as a parameter). Commented Jan 26, 2017 at 18:14
  • BTW also (unrelated): double sum should be double sum = 0 Commented Jan 26, 2017 at 18:14
  • So in essence: local variables need to be defined for each function they are used in? Even when I'm returning the local variable from userallocate() to main? I guess in my head I thought once its returned to main it would magically jump around to the other functions :-) Commented Jan 26, 2017 at 18:19

1 Answer 1

2

The reason you see 0.0000 has nothing to do with pointers.

rand() returns an int, and RAND_MAX, also an int, will most likely be greater than the value returned by rand(), so most of the time the result of the integer division is 0.

If you want floating point results, you will need to have one of the arguments to the division be a floating point type.

        ptr[i] = rand()/(double)RAND_MAX;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.