Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:26:55+00:00 2026-06-15T14:26:55+00:00

Currently I am trying to fill an array of size num with random values.

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:26:56+00:00Added an answer on June 15, 2026 at 2:26 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am really having a hard time trying to fill my two dimensional array
Currently I'm trying to fill a 3x3 square with random x's and o's to
I am trying to create a web app that will size itself to fill
Currently, I'm trying to fill a dictionary in Python but I think what I'm
Im currently trying to downlaod a audio track from a WCF, i need some
I am currently trying to write some code that will accept some FTP details,
I'm currently trying this: using DocumentFormat.OpenXml.Packaging; using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(fileNameDocx as string, true))
I'm currently trying to get the most popular productID from my MSSQL Database. This
I'm trying to fill an array with words inputted by user. Each word must
I'am currently trying to fill out a predefined Form with itextsharp. All works well

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.