Thread: Lining up results?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    25

    Lining up results?

    Hi Guys,

    Have a program which works, and uses a two-dimensional array to calculate and output marks for a course - and also calculate the average.

    Now got it to work, but the columns are all-over the place when it runs. Tried messing around with setw, but I just can't get it right! Can someone please help!

    Numbers.cpp
    Code:
    //Numbers.cpp
    // A program that uses a 5 by 6 array of numbers
    #include "numbers.H"
    #include <cstdlib>
    int main ()
    {
      course numbers;
      numbers.get_marks();
      numbers.display_marks();
      system ("pause");
      
    }
    Numbers.h

    Code:
     
    // Numbers.h
    // The Object Class Numbers
    
    # include <iostream>
    using namespace std;
    # include <iomanip>
    using namespace std;
      const int no_of_modules=4;
      const int no_of_students=6;
      
    class course
      {
      public:
        int get_marks();
        int display_marks();
      protected:
        int marks [no_of_modules][no_of_students];
      };
    
    int course::get_marks()
    {
      char terminator;
      for (int module_index=0; module_index <
      no_of_modules; ++ module_index)
        {
        for (int student_index=0; student_index < no_of_students; ++ student_index)
          {
          cout <<"Enter a mark for module no: "
          << module_index << " student no: "
          << student_index << " : ";
          cin >> marks [module_index]
          [student_index];
          }
        }
        cin.get(terminator);
    }
    
    int course::display_marks()
    {
      int module_total;
      cout <<
        "     0     1     2     3     4     5"
        << "     Average"  <<endl;
      for (int module_index=0; module_index <
      no_of_modules; ++ module_index)
      {
      module_total=0;
      cout <<"Module No: " <<module_index
           << "       "; 
      for (int student_index=0; student_index < no_of_students; ++ student_index)
           {
           cout << setw (6) <<marks[module_index][student_index];
           module_total += marks [module_index][student_index];     
           cout << setw (7) << module_total / no_of_students << endl;
           }
       }
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to take the output of the average out of the inner loop
    Code:
    int course::display_marks() {
      int module_total;
      cout <<    "     0     1     2     3     4     5     Average"  <<endl;
      for (int module_index=0; module_index <  no_of_modules; ++ module_index) {
         module_total=0;
         cout <<"Module No: " <<module_index  << "       "; 
         for (int student_index=0; student_index < no_of_students; ++ student_index)  {
             cout << setw (6) <<marks[module_index][student_index];
             module_total += marks [module_index][student_index];     
         }
         cout << setw (7) << module_total / no_of_students << endl;
       }
    }
    Kurt
    Last edited by ZuK; 12-19-2005 at 07:30 AM.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    25
    Agggh,

    Cheers Zuk!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Incorrect results from fmod()?
    By karlthetruth in forum C Programming
    Replies: 4
    Last Post: 04-11-2008, 09:12 AM
  2. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  3. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM