Thread: Grade Average passing in functions:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Grade Average passing in functions:

    My average is failing but I played and played with it and I still keep crashing.
    Code:
     #include <iostream>
    
    using namespace std;
    
    void getScore(int score[], int NumGrades)
    {
        
        cout<<"How many grades do you need to enter?"<<endl;
        cin >> NumGrades;
        
        cout<<"Enter the Students Grade(s)"<<endl;
        for (int i=0; i<NumGrades; i++) {
        cin >> score[i];
        }
    }
    void printGrade()
    {
        int numGrade=NULL;
        int grades[numGrade];
        int average=0;
        int total=0;
        
        getScore(grades, numGrade);
        
        for (int i=0; i<numGrade; i++) {
        total+= grades[i];
        }
        average = total/numGrade;
        
        cout<<"The students average was a "<<average<<endl;
    }
    
    int main(int argc, const char * argv[])
    {
        
        printGrade();
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I did some more testing and now seen I am not passing getscore() to printGrade(). I just tried to print out grades in the printGrade function using a for statement and nothing.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are allocating array of size 0 and expect it to be able to store some values?

    use std::vector instead and resize it before trying to populate it with values
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I dont event know what std::vector is. Looks like I have some reading to do.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can use an array if you want, although it's more C++y to use a vector.

    Your problem is that you don't pass back numGrades from getScore.
    * Take numGrades out of the getScore's arguments.
    * Make getScore return an int.
    * Declare numGrades as a local variable of getScore.
    * And return it at the end of the function.

    Also:
    * You should probably make average a double and cast one of the variables in its calculation to a double to ensure floating-point math.
    * Don't use NULL when you mean 0.
    * In fact, in C++, don't use NULL at all.
    Even for pointers, 0 is fine.
    For C++11 (which you should probably be using unless otherwise directed) use nullptr.
    However, since you're assigning to an int, you simply mean 0.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I highly recommend learning how to use vectors. They are quite awesome and seem to be pretty fast too.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Thank you. I will start to vigorously read about them.

    How different is C++11 from C++?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's quite different. C++11 added lots of containers, and new syntax, including: a new kind of for loop (range based), lambdas, initializer lists for containers, auto declarations, and all sorts of other things.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It adds new containers too, such as std::array.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, C++11 is exactly the same as (standard) C++, since standard C++ currently means C++11, by definition. Perhaps you meant to ask how is C++11 different from C++03.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. average grade program
    By fyu2 in forum C Programming
    Replies: 3
    Last Post: 05-25-2012, 01:11 AM
  2. Help with finding the highest average grade
    By Rockie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2011, 11:55 AM
  3. Grade program average problem
    By Zaz in forum C Programming
    Replies: 1
    Last Post: 11-20-2009, 07:11 PM
  4. Student Roster & Grade Average
    By Surge in forum C Programming
    Replies: 10
    Last Post: 12-12-2006, 07:32 AM
  5. Drop Lowest Grade then Average
    By cmut in forum C Programming
    Replies: 4
    Last Post: 09-27-2002, 10:19 PM