Get rid off negative averages

This is a discussion on Get rid off negative averages within the C++ Programming forums, part of the General Programming Boards category; So I pulled this from the C++ tutorial here and played around with it to make it into a program ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13

    Get rid off negative averages

    So I pulled this from the C++ tutorial here and played around with it to make it into a program that averages ten grades. How ever when I input all negative grades it gives a negative average. How would I and where would I input an if statement(not sure if this is what I would use) that would say basically if the average is less than zero than display ---%.

    Thanks for your help I'm pretty new at all of this.
    Code:
    int main()
    {
       int grade[10];
       int i,sum=0, avg=0;
       int max=0,min=100;
    
       for(i=0;i<10;i++)
       {
          cout << "Grade " << i+1 << ": ";
          cin >> grade[i];
       }
       for(i=0;i<10;i++)
    
       {
    
          sum=sum+grade[i];
    
          if(grade[i]>max)
    
          {
    
             max=grade[i];
    
          }
    
          if(grade[i]<min)
    
          {
    
             min=grade[i];
    
          }
    
       }
    
       avg=sum/10;
     
    
       cout << "Average Grade: " << avg << "%" << endl;
    
       return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Example.
    Code:
    if(avg < 0){
    }
    else{
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    Code:
    cout << "Average Grade: ";
    if (avg<0)cout << "---";
    else cout << avg;
    cout << "%" << endl;
    would I do something like this?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    Minus that being a bit ugly the concept is good.
    Code:
    cout<<"Average Grade: ";
    if(avg < 0){
        cout<<"---";
    }//if
    else{
        cout<<avg;
    }//else
    
    cout<<"%"<<endl;
    Woop?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    Ok great I'll edit that, I also have another question if I still have your attention. How would I split this program up into functions? I tried this but when I enter all my grades I always get 0. Did I split them up right?
    Code:
    int getGrades()
    
    {
          int grade[10];
          int i,sum=0, avg=0;
          int max=0,min=100;
    
          for(i=0;i<10;i++)
          {
             cout << "Grade " << i+1 << ": ";
             cin >> grade[i];
          }
          for(i=0;i<10;i++)
    
          {
    
             sum=sum+grade[i];
    
             if(grade[i]>max)
    
             {
    
                max=grade[i];
    
             }
    
             if(grade[i]<min)
    
             {
    
                min=grade[i];
    
             }
    
          }
    }
    
    int averageGrades()
    {
    
       avg=sum/10;
    
       cout << "Average Grade: ";
       if (avg<0)
           cout << "---";
       else 
           cout << avg;
       
      cout << "%" << endl;
    }
    
    int main()
    {
       getGrades();
       averageGrades();
    
       return 0;
    }
    Last edited by Barkley; 06-08-2010 at 02:22 PM.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    I'm surprised that compiles. The concept you are missing is that variables declared in one function do not exist in another function, even if you use the same names. If you declare two variables with the same name in two separate functions, they will reside in two separate memory locations; i.e., they will not be the same variable.

    You have to either 1) use global variables (which is generally frowned upon) or 2) learn to pass relevant data between your functions.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    I thought I copied the latest of what I had but I declared the variables in both functions thus thats why I was getting zeros, so how do I pass variables then? It goes in the () right?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Read some documentation on it. It will explain things more thoroughly than I will right now (a little busy). Something like:
    Cprogramming.com Tutorial: Functions

    And I'm sure you can use Google to find other literature on function use. If you have questions after that, feel free to ask.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    The average of a bunch of negative numbers is a negative number. What's the problem?

    If you want to report that something is wrong, report that the individual grades are negative, not that the overall average is negative.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    You know I was thinking about that, how would I do that though? In other words how would I make the program not take into account the value of -1?

    And I will read up on it thanks!

  11. #11
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    Check the value before you process it; if it's negative, skip it, or do whatever kind of error processing you would like.

    Since you are using for loops, it might be a good chance to look into the 'continue' keyword and try using that.

  12. #12
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    So from what I read about passing variables it needs to be in the () of the function that I am passing it too. So in my program would it be
    Code:
    averageGrades(int grade[], int i)
    edit: Actually I need to pass the avg and the sum right?
    Grade is an array so do I include the brackets?

    also when I do this I get an "arguement too few compiler" error so that must not be right
    Last edited by Barkley; 06-08-2010 at 07:26 PM.

  13. #13
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    so when I pass grade, avg, sum I get a compile error saying expected primary expression before int

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    When calling a function, you put the names of the variables you want to pass inside the (). Don't include type or other stuff.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    I don't declare the type? As in the "int" are you sure? When I do this it just gives me a not declared compiler error. Then when I delcare it in the actual function and in main it still gives me the not delcared in this cope error.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [question]unsigned number with negative value?
    By learn in forum C Programming
    Replies: 2
    Last Post: 12-26-2009, 11:00 PM
  2. Reading negative numbers from command line
    By Ace2187 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 12:21 PM
  3. Can we input negative numbers in the output windiw
    By hitesh1511 in forum C Programming
    Replies: 1
    Last Post: 08-22-2006, 12:07 AM
  4. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 12:48 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM

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