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; Declaration: Code: int AverageGrades(int grade[], int i); Calling: Code: AverageGrades(grade, i);...

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Declaration:
    Code:
    int AverageGrades(int grade[], int i);
    Calling:
    Code:
    AverageGrades(grade, i);
    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.

  2. #17
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    This is what I have now and I still get the undeclared in main. When I declare them then I get another totally different error.

    also would it be(something along the lines of this)

    Code:
    if (grade[i] > 100 || grade[i] < 0)
                 cout << "---"
    To get the program to not take into account negative grades in the average?

    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(int grade[], int i)
    {
       int avg;
       int sum;
    
       avg=sum/10;
    
       cout << "Average Grade: ";
       if (avg<0)
          cout << "---";
       else
          cout << avg;
    
       cout << "%" << endl;
    }
    
    
    int main()
    {
    
       getGrades();
       averageGrades(grade, i);
    
       return 0;
    }

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    grade and i doesn't exist in main (obviously). They're declared in getGrades. Think about that for a while.
    Also, are you coming from C or something? You obviously seem to love declaring variables at top. In C++, this is not necessary.
    For example,
    Code:
     for(i=0;i<10;i++)
    Can be written as:
    Code:
     for(int i = 0; i < 10; i++)
    Also note how AverageGrade doesn't care about "i". What's it for?
    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.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by Elysia
    Also, are you coming from C or something? You obviously seem to love declaring variables at top. In C++, this is not necessary.
    ...
    Also note how AverageGrade doesn't care about "i". What's it for?
    That's the least of his problems. In fact it's barely even a problem.

    Why does GetGrades sum the series of grades, and why does it find the minimum and maximum? It seems like this function can't do one thing well, which is bad. If he is from a C background, he didn't stay long.

    Try implementing functions that do one thing:

    void GetGrades (int grades[] , size_t size);
    double GetAverage (int grades[] , size_t size);
    void FindMinMax (int grades[] , size_t size , size_t *minPos , size_t *maxPos);
    .. etc ....

    If in GetGrades you ensure there are no negative marks, then there wont be negative averages. A good procedural design can isolate all sorts of bugs.

  5. #20
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    I have no background at all in anything hence why I pulled this from the tutorial here and tried to modify it haha. I have been trying to fool around with this language for about 3 weeks now and not getting too far hah. Sorry for the dumb thought process I just really haven't much of a clue what I'm doing still as you can tell by now. Each thing you post really helps though so thank you and please keep it coming haha.

  6. #21
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    So I still don't have a clue I've switched the variables all the ways I can think of and it still won't compile, can I get a major hint? maybe in the most simple form possible

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    What is your current code, and how does this updated code not work?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    You can see functions are individuals performing work. They have to give them the information they need to carry out the task. And they have to hand that back when they're finished.
    You can't just expect individual B to know whatever information individual A carries. And information being variables in this case.
    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.

  9. #24
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    So from getGrades the variables int grade and int sum need to be passed into averageGrades right? Then when I call it in main I just put grade, sum in the () of averageGrades.

    edit: it would just be sum because averageGrades doesn't need grade.
    Last edited by Barkley; 06-09-2010 at 01:20 PM.

  10. #25
    Registered User
    Join Date
    Jun 2010
    Location
    Longview, WA
    Posts
    13
    this is what I have now, now I get an arguments too many compile error.

    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(int sum)
    {
    
       int avg;
    
       avg=sum/10;
    
       cout << "Average Grade: ";
       if (avg<0)
          cout << "---";
       else
          cout << avg;
    
       cout << "%" << endl;
    }
    
    
    int main()
    {
       int sum;
    
       getGrades(sum);
       averageGrades();
    
       return 0;
    }

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    But GetGrades doesn't take any arguments. You haven't told the compiler that it does.
    Also, you seem to not be fully understanding C++ syntax. You might want to read up on functions.
    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.

  12. #27
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by Barkley
    So from getGrades the variables int grade and int sum need to be passed into averageGrades right? Then when I call it in main I just put grade, sum in the () of averageGrades.
    Well that's not good enough, obv. Problem is I don't think you understand exactly how a computer executes a program, so I will explain in simple terms with respect to C .

    When the computer starts a program it calls main first. Programming folk call main the entry point because that is where the program starts. Now think of main as a task master. The job of main is going to be interact with the user and provide the output he wants. The programmer explains what he wants through writing a series of instructions to be followed in top down order, for the most part. When you have a working program you will have functions that do separate things --
    Code:
    void GetGrades (int grades[] , size_t size);
    double GetAverage (int grades[] , size_t size);
    void FindMinMax (int grades[] , size_t size , size_t *minPos , size_t *maxPos);
    main calls these helper functions to do its job. In this program, main might have an average variable to present to the user, which is actually a reduction of the grades array.

    The first helper main will have to hand control over to is here at GetGrades. GetGrades promises that grades will be filled when its done. main is a pretty lazy function, and doesn't want to do this itself, so this promise makes everything pretty copacetic. main gives its helper a pointer to grade's first element and a size variable, and then GetGrades is given the time to perform the task. main has to sit around while GetGrades is working.

    After that, main decides it's time to find an average. main, being lazy, decides to wait on GetAverage to work out the answer:

    Code:
    average = GetAverage (grades , size);
    main pretty much doesn't care how this answer comes about, but knows that GetAverage will need the grades array and its size, and knows that GetAverage returns an answer that fits in main's variable called average. main gives GetAverage time to complete its work, while main waits to assign the answer.

    Then main dispatches the final task to FindMinMax. That function wants memory locations to store its answers, but promises that when it returns the answers will be correct. Thus explains our signature of FindMinMax:
    Code:
    void FindMinMax (int grades[] , size_t size , size_t *minPos , size_t *maxPos)
    Because functions can't return more than one thing of a certain type. The main function is also responsible for these locations when it makes the call:
    Code:
    FindMinMax(grades , size , &mindex , &maxdex);
    After this, main should have all the information it needs to present to the user in average, mindex, maxdex, and grade. These are all variables defined by (initialized in) and local to (belonging to, in the scope of) main. Here main actually does its own work formatting output before it ends the program, or it could dispatch the task again, depending on how you write main. You're the boss!

    Actual programs have a lot more complicated flow charts, but it may be a while before you write anything more complicated than "main calls the foo function". It's all part of learning to know why these designs make sense, and basically how simple you can make it so it's free of bugs AND does what you need. There are plenty of tools at hand to implement a procedural program in C++.

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    GetAverage does not want the size of the array. That's C thinking. GetAverage can easily acquire the size of the array, if it needs it. That's why we use std::tr1::array, not native C arrays.
    FindMinMax does not want memory locations to store its answers - it wants variables to store its answers. We use references instead. Plus GetAverage and FindMinMax will not modify the grades; thus the should be const.
    More proper prototypes would be:
    Code:
    void GetGrades (std::tr1::array<int, 10>& grades);
    double GetAverage (const std::tr1::array<int, 10>& grades);
    void FindMinMax (const std::tr1::array<int, 10>& grades, size_t& MinPos, size_t& MaxPos);
    I'd say you're thinking way too much C. This is the C++ forum. Let's not teach newbies C ways. Let's teach them modern C++ ways instead.
    Last edited by Elysia; 06-09-2010 at 03:48 PM. Reason: Typo
    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.

  14. #29
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Instead of showing off, how about you explain templates and references to the OP, instead of complaining about how I think.

    I'm willing to explain good programming and answer his questions in terms of the things he understands now.
    Last edited by whiteflags; 06-09-2010 at 03:47 PM.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Put simply, I'm not going to explain templates. Just like Accelerated C++, I'm just going to say, this is how you do, and that's enough for now. Know that the syntax of a C++ array is std::tr1::array<T, size> where T is the type the array should hold and size the size of the array.
    std::tr1 is the namespace it lies in. This much I can expect the OP to know or should know.
    The good thing about C++ arrays is several things, but only one matters now and that is size. The C++ array knows its size, which you can get via the .size() member function.
    Plus it has the .at() member function which eliminates those pesky buffer overruns and saves you trouble of undefined behavior.

    References is not something I'm going to explain. That's basic of basics. The OP should be aware of them, or open the book to read about them. Or read a tutorial. Without references or basic understanding of pointers, you're not going anywhere. It's simply pointless to code functions if you don't know them.
    And if one understand pointers, I would assume one knows references. But not necessarily vice versa.
    Last edited by Elysia; 06-09-2010 at 03:55 PM.
    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.

Page 2 of 3 FirstFirst 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-27-2009, 12:00 AM
  2. Reading negative numbers from command line
    By Ace2187 in forum C Programming
    Replies: 3
    Last Post: 12-06-2009, 01: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, 01:07 AM
  4. Negative Numbers
    By Quantrizi in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2003, 01:48 AM
  5. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 01:20 PM

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