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

This is a discussion on i need help to get the average of 6 subjects by recursion within the C++ Programming forums, part of the General Programming Boards category; hello everbody there... i guess everbody is soo fine this is my first time here in this site i just ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Unhappy 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

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,142
    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>
    instead replace it with
    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.

    now, on to your question.

    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.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    thanks Elkvis for ur replying and for ur advice
    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
    Last edited by shaibon; 12-15-2010 at 08:54 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,142
    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.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    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

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by shaibon View Post
    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....)

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by shaibon View Post
    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.
    Last edited by nimitzhunter; 12-15-2010 at 02:11 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    still i dont got the idea how to solve it
    im so sorry and thanx alot 4 every body

  10. #10
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Average score
    By wiz23 in forum C++ Programming
    Replies: 22
    Last Post: 05-05-2005, 05:38 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21