I need help solving this!

Write a full program that will read from the user 24 full numbers that represent the income of a firm for each month for a 24 month period. Then counts and displays on the screen 8 values representing the averages of each quarter. For example, if the input is 10, 2, 30, 4, 20, 15, 100, 12, 2, 40, 50, 90, 35, 85, 30, 56, 22 then the output would be 14, 13, 38, 60, 50, 45, 47, 32

This one should be solved with functions and I can't do that. Can someone help me?
Print what you have and we can help you chop it up into functions.
do you know how to write a function?
do you understand the relationship between a pointer and an array?

something like this ... (this is the crudest, simplest thing I can give you to get started ignoring the concepts of vectors and so on). It is hardcoded to know that you want the average of 3 items, which is sort of bad practice, but you can make it cleaner if you want. I am just giving you a starting place.

1
2
3
4
5
6
7
8
9
10
11
double Q_Avg(int * ip)
{
    //quarter is 3 months, each element is 1 month, so..
     return (ip[0] + ip[1] + ip[2])/3.0;    
}
...

cout << "Q1 = " << Q_Avg(data) << endl;
cout << "Q2 = " << Q_Avg(&(data[3])) << endl; /0,1,2 were the first 3 months, 3,4,5 is q2..
.. etc?


//the above can be done in a loop for 0, 3, 6, 9, 12, and the first one becomes &(data[0]) where 0 is the loop counter that is being incremented by 3s ... I spelled it out above so you can SEE what it is doing better.
Last edited on
int main (){
int i, nr;

for (i=0; i<=24; i++) {
cout<<"Write the incomes of month "<<i+1<<" : ";
cin>>nr;
}
}

---- this is what I have done so far. I know how to read the numbers, I just need help on solving how to count and display on the screen 8 values representing the averages of each quarter. Can you do this?
what are you counting?

Understand what I gave you, and see if you can work it from there. Its very nearly all you need...

with this change:

int i, nr[24];

for (i=0; i<=24; i++) {
cout<<"Write the incomes of month "<<i+1<<" : ";
cin>>nr[i];
}
}

and nr = data in my code snip.
Last edited on
Well it says that numbers are given from users. So first I need to read them and than I will need to do the math the the function so I can give the output for each quarter.
yes. I see that. You have code (that I changed slightly) to read the 24 values, so that part is about done now. I gave you code to do the math and write the results. Did you read my post with the function example??

throw us a bone ... do you know about arrays? If not we can do something else, as that is what I used. Do you know about pointers? Do you know about functions or is this your first? I don't know what we have to work with here.
Last edited on
double Q_Avg (int * ip) {
return (ip[0] + ip[1] +ip[2])/3
};


int main (){
int i, nr[24];

for (i=0; i<=24; i++) {
cout<<""Write the incomes of month "<<i+1<<" : ";
cin>>nr[i];
}
cout<<"Average of the "<< i+1<<" Quarter is : "<<Q_Avg(data); / I just need to add the other quarters now. Is this right or not?

}
Solved it thank you Jonnin.
NP. be sure you understand it, as well.... ask if you don't.

-- you took the decimal off the 3, which may cause you to get integer division which loses the precision of your average. leave the 3. or 3.0 on there if you want decimal averages.

if you DO want integer averages, convert the type of the function from double to int, just change that word.
Last edited on
I tested it it's working perfect. Thank you one more time. I also have one more if you can check it this one is pretty hard for me.
Topic archived. No new replies allowed.