Question

Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless...

Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless it asking for.

/*
Write a program designed to get ages and heights from the user,
then find the average age, average height, and average age/height ratio.
*/

#include <stdlib.h>
#include <stdio.h>
#define MAXNUM 50

typedef struct person
{
int age;
double height;
} Person;

int getData(Person people[], int max)
/*
Get the data from the user and put it in an array of type Person.
You must use a while loop like we did for the exponent lab. Have the user enter
the age prior to entering the loop (negative number or 0 to quit)
Test to be sure the age is > 0, and also test to be sure the data won't overflow the array.
Think about what you need to do inside of the loop.
If there is too much data, print a message to the user and return to the main program
with the current count and the structs that have already been stored in the array.
Returns the number of people read as the value of the function.
*/

void getAverages(Person people[], double *aveAge, double *aveHeight, double *aveRatio, int numPeople)
/*
Calculate the averages here.
You will loop through the array just like you have done in the past in order to include all
numPeople in your calculations
To find the average age/height ratio, take each persons age and divide it by their height,
then calculate the average of all of those values.
*/

void printAverages(double aveAge, double aveHeight, double aveRatio)
/*
3 lines of code to print your results!
Be sure to format it nicely
*/

void main(void)
{
//declare your variables here

// call function getData
// do some debug prints here to be sure you have the correct count and data
// call function getAverages
// call function printAverages
}

/*
A few notes:
1. You will need some local variables in your functions.
2. Hint: This line might be valuable to you: scanf("%d",&people[count].age)
3. What should you do in your main program if getData returns with no data? What would happen
if you called getAverages when numPeople = 0?
4. When you are testing, you might want to make MAXNUM small, maybe 3 or 4. You can
increase it when you have finished testing.
5. When you call getData from your main program, you must capitalize MAXNUM in the
function call.
*/
0 0
Add a comment Improve this question Transcribed image text
Answer #0

// do comment if any problem arises

//code

/*

Write a program designed to get ages and heights from the user,

then find the average age, average height, and average age/height ratio.

*/

#include <stdlib.h>

#include <stdio.h>

#define MAXNUM 50

typedef struct person

{

    int age;

    double height;

} Person;

int getData(Person people[], int max)

/*

Get the data from the user and put it in an array of type Person.

You must use a while loop like we did for the exponent lab. Have the user enter

the age prior to entering the loop (negative number or 0 to quit)

Test to be sure the age is > 0, and also test to be sure the data won't overflow the array.

Think about what you need to do inside of the loop.

If there is too much data, print a message to the user and return to the main program

with the current count and the structs that have already been stored in the array.

Returns the number of people read as the value of the function.

*/

{

    printf("Negative number of 0 to quit\n");

    printf("\nEnter age: ");

    // no of people

    int n = 0;

    scanf("%d", &people[n].age);

    while (people[n].age > 0)

    {

        // overflow

        if (n == MAXNUM)

        {

            printf("Maximum limit reached!\n");

            return n;

        }

        printf("Enter height: ");

        scanf("%lf", &people[n++].height);

        printf("\nEnter age: ");

        scanf("%d", &people[n].age);

    }

    return n;

}

void getAverages(Person people[], double *aveAge, double *aveHeight, double *aveRatio, int numPeople)

/*

Calculate the averages here.

You will loop through the array just like you have done in the past in order to include all

numPeople in your calculations

To find the average age/height ratio, take each persons age and divide it by their height,

then calculate the average of all of those values.

*/

{

    *aveAge = 0;

    *aveHeight = 0;

    // iterate over people

    for (int i = 0; i < numPeople; i++)

    {

        *aveAge += people[i].age;

        *aveHeight += people[i].height;

    }

    *aveHeight /= numPeople;

    *aveAge /= numPeople;

    *aveRatio = (*aveAge) / (*aveHeight);

}

void printAverages(double aveAge, double aveHeight, double aveRatio)

/*

3 lines of code to print your results!

Be sure to format it nicely

*/

{

    printf("\nAverage age: %.2lf\n", aveAge);

    printf("Average height: %.2lf\n", aveHeight);

    printf("Average ratio: %.2lf\n", aveRatio);

}

int main()

{

    //declare your variables here

    Person people[MAXNUM];

    double aveAge, aveHeight, aveRatio;

    // call function getData

    int numPeople = getData(people, MAXNUM);

    // no data

    if (numPeople == 0)

        return 0;

    // do some debug prints here to be sure you have the correct count and data

    // call function getAverages

    getAverages(people, &aveAge, &aveHeight, &aveRatio, numPeople);

    // call function printAverages

    printAverages(aveAge, aveHeight, aveRatio);

}

Output:

Negative number of 0 to quit Enter age: 19 Enter height: 150 Enter age: 20 Enter height: 160 Enter age: -4 Average age: 19.50

Add a comment
Know the answer?
Add Answer to:
Please do it like someone wouldve done it as a beginer programer. Dont use pointer unless...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as...

    Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its size to pointers; depending on...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

    please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

  • Hi. Could you help me write the below program? Please don't use any header other than...

    Hi. Could you help me write the below program? Please don't use any header other than iostream, no Chegg class, no argc/argv. Nothing too advanced. Thank you! For this assignment you are implement a program that can be used to compute the variance and standard deviation of a set of values. In addition your solution should use the functions specified below that you must also implement. These functions should be used wherever appropriate in your solution. With the exception of...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • C++ please Possible algorithms – (1) use and modify the algorithm from program 2 if possible;...

    C++ please Possible algorithms – (1) use and modify the algorithm from program 2 if possible; (2) use unique value array; (3) use flag array (flags used to avoid counting a number more than 1 time); (4) compute frequency distribution (use unique and count arrays); (5) use count array to store the frequency of a number. No global variable are permitted in this assignment. Do not change the code provided only write the missing code. Write the missing C++ statements...

  • Change your C++ average temperatures program to: In a non range-based loop Prompt the user for...

    Change your C++ average temperatures program to: In a non range-based loop Prompt the user for 5 temperatures. Store them in an array called temperatures. Use a Range-Based loop to find the average. Print the average to the console using two decimals of precision. Do not use any magic numbers. #include<iostream> using namespace std; void averageTemperature() // function definition starts here {    float avg; // variable declaration    int num,sum=0,count=0; /* 'count' is the int accumulator for number of...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT