# i need help to get the average of 6 subjects by recursion

• 12-15-2010
shaibon
i need help to get the average of 6 subjects by recursion
hello everbody there... i guess everbody is soo fine
this is my first time here in this site i just finished registered right now

i have faced a small problem in my little code, my code is about using recursion in c++ i want to make a small code for students that have 6 subjects
i want to get the sum of those subjects and the average

i have solve how to do the sum of those 6 subjects , but my problem is how to get the average of those subjects

and this is my code
im using turbo c++

Code:

```#include <iostream.h> int subject(int x){         int z, sum = 0 , avg = 0;         if(x<1){                   avg = sum / 6;                   return avg;                 }         else{                 cin>>z;                 sum = subject(x-1) + z;                 return sum;                 } } void main(){         cout<<subject(6); }```

so i guess that everbody will help me
and thanx alot
• 12-15-2010
Elkvis
if I were you, the first thing I would do is use the standard font/formatting for any future posts. the big, off-color, centered text is just annoying.

the following line is deprecated, so you shouldn't use it.
Code:

`#include <iostream.h>`
Code:

`#include <iostream>`
Next, get a modern compiler. turbo C++ does not support the current standard, to say nothing of the new emerging standard for C++, and in fact will probably not compile the code change I suggested.

Visual Studio 2010 Express
MinGW - Minimalist GNU for Windows

both of these are excellent choices for windows development.

I can only guess at what you mean, but I suspect that you want to find the sum and average of the scores of the 6 classes. the sum is very straightforward - you simply add them all together. the average is nearly as simple. you take the sum and divide it by the number of items - in this case, 6.
• 12-15-2010
shaibon
i know that about #include <iostream> and i must use it instead of #include <iostream.h>
and i will use it

and now about my question i did as u say that i divide the sum of 6 classes by 6
but i got a different result ' like when i entered 6 classes there marks are 50 the sum of 6 classes will be 300 and then i divide it by 6 i must got 50 but the result that i got it it was 9
i think that the program do like this 336/36 = 9.33333'
but in my program when i want to get the sum i got it very nce and there is no problem but when i divide the sum by 6 it confusing and give me another result

like this : if(x<1){
avg = sum / 6;
return avg;
}
else{
cin>>z;
sum = subject(x-1) + z;
return sum/6; // here i divide it by 6 and i got 9
}

so i didnt find a solution to fix that
and thanks alot for ur helping

im waitin 4 another help
• 12-15-2010
Elkvis
if you want to be able to handle fractional numbers, you will need to use the float or double data types instead of int. these can handle numbers with decimal points in them, and should solve your problem.
• 12-15-2010
tabstop
This is a very ... interesting ... recursion setup. Let's say you type 50 for each score and see what happens:
Code:

```subject(6) called   z = 50 read in   subject(5) called     z = 50 read in     subject(4) called       z = 50 read in       subject(3) called         z = 50 read in         subject(2) called           z = 50 read in             subject(1) called             z = 50 read in             subject(0) called               sum = 0, so avg = sum/6 = 0               0 returned             sum = returnvalue + z = 0+50 = 50             50 returned           sum = returnvalue + z = 50+50 = 100           100 returned         sum = returnvalue + z = 100+50 = 150         150 returned       sum = returnvalue + z = 150+50 = 200       200 returned     sum = returnvalue + z = 200+50 =250     250 returned   sum = returnvalue + z = 250+50 = 300   300 returned subject(6) = 300```
You can't put your division in the middle if you haven't even added things up yet.
• 12-15-2010
shaibon
thanx tabstop and i got like this as u write loool
i dont know when i must to divide the sum of this classes
i dont know where i must add the division code

so i need yours help
so plz help me to fix this code

and i got an idea also like this:

Code:

```#include <iostream.h> int subject(int x){         int z, sum = 0 , avg = 0;         if(x<1){                   return 0;                 }         else{                 cin>>z;                 sum = subject(x-1) + z;                 return sum;                 } } void main(){         cout<<subject(6); }```
here only i will got the sum thats equal 300 if i entered six classes (50)

so plz everybody help me as soon as possible
and thanx alot
• 12-15-2010
tabstop
Quote:

Originally Posted by shaibon
i dont know when i must to divide the sum of this classes
i dont know where i must add the division code

You're not supposed to know. You're supposed to figure it out by thinking about it. So think about the problem, and when it would make sense to divide (and for that matter, what it would make sense to divide by....)
• 12-15-2010
nimitzhunter
Quote:

Originally Posted by shaibon
thanx tabstop and i got like this as u write loool
i dont know when i must to divide the sum of this classes
i dont know where i must add the division code

so i need yours help
so plz help me to fix this code

and i got an idea also like this:

Code:

```#include <iostream.h> int subject(int x){         int z, sum = 0 , avg = 0;         if(x<1){                   return 0;                 }         else{                 cin>>z;                 sum = subject(x-1) + z;                 return sum;                 } } void main(){         cout<<subject(6); }```
here only i will got the sum thats equal 300 if i entered six classes (50)

so plz everybody help me as soon as possible
and thanx alot

your code does add up. Use main with "int" instead of void. and also you need to include the namespace std to use cout and cin.

Code:

```#include <iostream> int subject(int x) {   int z, sum = 0 , avg = 0;   if(x<1){     return 0;   }   else{     std::cin>>z;     sum = subject(x-1) + z;     return sum;   } } int main() {   std::cout<<subject(6) << std::endl;   return 0; }```

if you want to do division, you need either type double or float. Using int will division will only get you the truncated answer.
and change the font please, bold text don't look that good.
• 12-15-2010
shaibon
still i dont got the idea how to solve it
im so sorry and thanx alot 4 every body
• 12-15-2010
VirtualAce
Recursive functions are well documented and well researched. They have specific criteria that I would think if your assignment is to write one you should have covered at some point.