Thread: passing arrays must be an easier way?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    Unhappy passing arrays must be an easier way?

    Alright guys so I have some homework due tomorrow and I am having trouble with my output function, I could do it this way but its going to be ridiculously long. I have to read from an input file and get a list of students and grades and have 3 arrays and 3 functions, one for reading the information from the file, one for calculation, and one for outputting the information to console.
    file:
    Johnson 85 83 77 91 76
    Aniston 80 90 95 93 48
    Cooper 78 81 11 90 73
    Gupta 92 83 30 69 87
    Blair 23 45 96 38 59
    Clark 60 85 45 39 67
    Kennedy 77 31 52 74 83
    Bronson 93 94 89 77 97
    Sunny 79 85 28 93 82
    Smith 85 72 49 75 63
    --------------------------------

    so this is my code so far, I am asking for help with an easier way to do my output function.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cstdlib> // for system("pause")
    using namespace std;
    
    void read(int& x, int&y);
    void calculate();
    void output();
    
    string students[10];
    double scores[10][5];
    double averages[10];
    double cave;
    
    double studentaveofx0;
    double studentaveofx1;
    double studentaveofx2;
    double studentaveofx3;
    double studentaveofx4;
    double studentaveofx5;
    double studentaveofx6;
    double studentaveofx7;
    double studentaveofx8;
    double studentaveofx9;
    
    
    int main ()
    {
        int x; 
        int y; 
        
        read(x, y);
        calculate();
        
        output();
        
        
        system("pause");
        return 0;
    }
    
    
    void read(int& x, int&y)
    {
    
        ifstream inData;
         
        inData.open("students.txt");
        
        cout << setprecision(2);
        cout << fixed;
        
        for (x = 0; x < 10;  x++)
        {
            inData >> students[x];
            cout << students[x] << " ";
            
            for(y = 0;y < 5;y++)
            {
                inData >> scores[x][y];
                cout << scores[x][y] << " ";
                
            }
            cout << endl;
        }
    
        inData.close();
        
    cout << "**********************************" << endl;
    }
    
    void calculate()
    {
        int x;
        int y;
        double result;
        
        for (x = 0; x < 10; x++)
        {
            for(y = 0;y < 5;y++)
            {
                averages[x] += scores[x][y]; 
          
            }
          }
            // ummmm...... this is crazy... must be an easier way
             studentaveofx0 = averages[0]/y;
             studentaveofx1 = averages[1]/y;
             studentaveofx2 = averages[2]/y;
             studentaveofx3 = averages[3]/y;
             studentaveofx4 = averages[4]/y;
             studentaveofx5 = averages[5]/y;
             studentaveofx6 = averages[6]/y;
             studentaveofx7 = averages[7]/y;
             studentaveofx8 = averages[8]/y;
             studentaveofx9 = averages[9]/y;
             
        
        for (x = 0; x < 10;x++)
        {
                result += (averages[x] / 5);  
        }
        
        cave = result/x;
    }
    
    void output(){
         
         if(studentaveofx0 >= 90 && studentaveofx0 <= 100){
                           cout << students[0] << ": A" << endl;
                           }
         else if(studentaveofx0 >= 80 && studentaveofx0 < 90){
              cout << students[0] << ": B" << endl;
              }
         else if(studentaveofx0 >= 70 && studentaveofx0 < 80){
              cout << students[0] << ": C" << endl;
              }
         else if(studentaveofx0 >= 60 && studentaveofx0 < 70){
              cout << students[0] << ": D" << endl;
              }
         else{
              cout << students[0] << ": F" << endl;
              }
              
         if(studentaveofx1 >= 90 && studentaveofx1 <= 100){
                           cout << students[1] << ": A" << endl;
                           }
         else if(studentaveofx1 >= 80 && studentaveofx1 < 90){
              cout << students[1] << ": B" << endl;
              }
         else if(studentaveofx1 >= 70 && studentaveofx1 < 80){
              cout << students[1] << ": C" << endl;
              }
         else if(studentaveofx1 >= 60 && studentaveofx1 < 70){
              cout << students[1] << ": D" << endl;
              }
         else{
              cout << students[1] << ": F" << endl;
              }
              
         if(studentaveofx2 >= 90 && studentaveofx2 <= 100){
                           cout << students[2] << ": A" << endl;
                           }
         else if(studentaveofx2 >= 80 && studentaveofx2 < 90){
              cout << students[2] << ": B" << endl;
              }
         else if(studentaveofx2 >= 70 && studentaveofx2 < 80){
              cout << students[2] << ": C" << endl;
              }
         else if(studentaveofx2 >= 60 && studentaveofx2 < 70){
              cout << students[2] << ": D" << endl;
              }
         else{
              cout << students[2] << ": F" << endl;
              }
         // how to just increment a variable for printing purposes?
         
         
         cout << "**********************************" << endl;
         cout << "Class Average: " << cave << endl;
         cout << "**********************************" << endl;
         
         }
    please help guys, i dont have allot of time.

    output so far, didnt finish it all... but it outputs correctly so far.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Stay away from global variables. Read Why Global Variables are Bad.

    There is no reason for your declaration of x and y in main and then passing them to your read function. You could simple make a read function which loops until EOF and then read what you need to inside your loop. Take a look at Lesson 3: Loops and Lesson 10: File operations.

    Now as for your averaging function. How would you do it on paper? Think about it and right it out step by step. You have an array that stores the 5 values. You can iterate through the array, summing those all up. You know how many numbers are in the array so you know what you need to divide by to obtain the average. Then you can just store the results into your average array.

    Have you learned about structures yet? If so, grouping your information into one would be the way to go. See Lesson 7: Structures.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    6

    what I have now

    This is what I have now;
    Still wrong but closer...
    if I output my averages from inside calculate they come out right, but I have to output them from the output function which gives me a wrong answer... so the problem is in my output() function. If anyone spots it let me know.


    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <cstdlib> // for system("pause")
    using namespace std;
    
    void read(int& x, int&y);
    void calculate();
    void output();
    
    string students[10];
    double scores[10][5];
    double averages[10];
    double cave;
    
    int main ()
    {
        int x; 
        int y; 
        
        read(x, y);
        calculate();
        
        output();
        
        
        system("pause");
        return 0;
    }
    
    
    void read(int& x, int&y)
    {
    
        ifstream inData;
         
        inData.open("students.txt");
        
        cout << setprecision(2);
        cout << fixed;
        
        for (x = 0; x < 10;  x++)
        {
            inData >> students[x];
            cout << students[x] << " ";
            
            for(y = 0;y < 5;y++)
            {
                inData >> scores[x][y];
                cout << scores[x][y] << " ";
                
            }
            cout << endl;
        }
    
        inData.close();
        
    cout << "**********************************" << endl;
    }
    
    void calculate()
    {
        int x;
        int y;
        double result;
        
        for (x = 0; x < 10; x++)
        {
            for(y = 0;y < 5;y++)
            {
                averages[x] += scores[x][y]; 
          
            }
          }
            
        for (x = 0; x < 10;x++)
        {
                result += (averages[x] / 5);  
        }
        
        cave = result/x;
    }
    
    void output(){
         int x;
         int y;
         
         for(x = 0; x < 10; x++){
               
         if(averages[y]/x >= 90 && averages[y]/x <= 100){
                           cout << students[x] << ": A" << endl;
                           }
         else if(averages[y]/x >= 80 && averages[y]/x < 90){
              cout << students[x] << ": B" << endl;
              }
         else if(averages[y]/x >= 70 && averages[y]/x < 80){
              cout << students[x] << ": C" << endl;
              }
         else if(averages[y]/x >= 60 && averages[y]/x < 70){
              cout << students[x] << ": D" << endl;
              }
         else{
              cout << students[x] << ": F" << endl;
              }
           }
         cout << "**********************************" << endl;
         cout << "Class Average: " << cave << endl;
         cout << "**********************************" << endl;
         
         }

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Well, let's see; you are still using globals which I told you not to. You aren't using your average array the way you should be. You never initialize or increment 'y' in your output function. Are you sure you haven't learned about structures yet? Plus your logic for printing average using x is wrong since x goes up to 9 and you only have 5 values. Not to mention first time through your loop you attempt to divide by 0.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays
    By renvaar in forum C Programming
    Replies: 8
    Last Post: 11-19-2009, 08:37 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Easier way?
    By Vicious in forum Game Programming
    Replies: 9
    Last Post: 05-16-2002, 06:59 PM
  4. passing arrays and passing pointers
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 12:35 PM
  5. passing arrays
    By Sarah in forum C Programming
    Replies: 1
    Last Post: 10-24-2001, 08:02 PM