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

  • Home
  • SEARCH
  • 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 3285924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:19:18+00:00 2026-05-17T20:19:18+00:00

I need to write a simple program that ask the user to insert 4

  • 0

I need to write a simple program that ask the user to insert 4 double type variable in a struct variable data.

   struct Data  
    {
      double a;
      double b;
      double c;
      double average;
    };
struct Data *ptr_name;
int i;

First, ask user the size:

 printf("Please enter the size:");
 scanf("%d", &size);

Then, use the malloc. (I dont know how to use it…)

something like this… ptr_name = ()malloc();

and then use the for loop to get the a, b, c from user.

for(i = 0; i < size; i++)
{
 //dont know how to put the staement..
}

finally, print everything out, including the average.

for(i = 0; i < size; i++)
    {
     //same as above...
    }

That is pretty much all, I am learning struct type and malloc now, can’t understand by browsing web… help, thanks.

  • 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-05-17T20:19:18+00:00Added an answer on May 17, 2026 at 8:19 pm

    The call to malloc should be:

    ptr_name = malloc (sizeof (struct Data) * size);
    

    The following functions read/write instances of struct Data from/to the console:

    static struct Data 
    read_from_console ()
    {
      struct Data d;
    
      d.a = 0.0f; d.b = 0.0f; d.c = 0.0f; d.average = 0.0f;
      printf ("Enter values separated by comma: (a, b, c): ");
      fflush (stdout);
      if (scanf ("%lf, %lf, %lf", &d.a, &d.b, &d.c) != 3)
        {
          printf ("Invalid input\n");
          exit (1);
        }
      else
        d.average = (double) ((d.a + d.b + d.c) / 3.0f);
      return d;
    }
    
    static void
    print_to_console (struct Data* d)
    {
      printf ("a=%f, b=%f, c=%f, average=%f\n", d->a, d->b, d->c, d->average);
      fflush (stdout);
    }
    

    You can call them from the loops inside the main function:

    int
    main ()
    {
      struct Data *ptr_name;
      int count;
      int i;
    
      printf ("Please enter size: ");
      fflush (stdout);
      if (scanf ("%d", &count) != 1)
        {
          printf ("Invalid input\n");
          return 1;
        }
      ptr_name = malloc (sizeof (struct Data) * count);
    
      for (i = 0; i < count; ++i)
        ptr_name[i] = read_from_console ();
    
      for (i = 0; i < count; ++i)
        print_to_console (&ptr_name[i]);
    
      return 0;
    }
    

    A sample interaction:

    > Please enter size: 2
    > Enter values separated by comma: (a, b, c): 12.00, 12.45, 13.00
    > Enter values separated by comma: (a, b, c): 5.4, 5.00, 5.1
    a=12.000000, b=12.450000, c=13.000000, average=12.483333
    a=5.400000, b=5.000000, c=5.100000, average=5.166667
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a simple program that records all the input from parallel
I need to write a simple program for work that does the following: read
I'm trying to write a simple C++ program that uses Berkeley DB for storage.
I need to write a program that uses matrix multiplication to rotate an image
I am trying to write a simple program that lets me overlay a dot
I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I need to write a simple command-line application in Java. It would be nice
I need write an update statement that used multiple tables to determine which rows
I need to write a program used internally where different users will have different
I need to write a Java Comparator class that compares Strings, however with one

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.