⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 studentgrades.cpp

📁 C++大学简明教程,解压就行没有密码。希望对大家有用
💻 CPP
字号:
// Tutorial 14: StudentGrades.cpp
// This application computes each student's grade average and
// the class average for ten students.
#include <iostream> // required to perform C++ stream I/O
#include <iomanip>  // required for parameterized stream manipulators
#include <string>   // required to access string functions

using namespace std; // for accessing C++ standard library members

// define constants
const int TESTS = 3; // number of tests per student
const int STUDENTS = 10; // maximum students stored

// function prototypes
void getStudentGrades( int [][ TESTS ] );
void displayGrades( int [][ TESTS ] );

// function main begins program execution
int main()
{
   // define two-dimensional array to store student grades
   int studentGrades[ STUDENTS ][ TESTS ] = { 0 };

   getStudentGrades( studentGrades ); // input grades

   // format floating-point numbers
   cout << fixed << setprecision( 1 );

   // display each student's average grade
   displayGrades( studentGrades );

   return 0; // indicates successful termination

} // end function main

// prompt user for and input grades
void getStudentGrades( int studentGrades[][ TESTS ] )
{
   int grade = 0; // test grade

   // repeat until all grades have been entered
   for ( int student = 0; student < STUDENTS; student++ )
   {
      cout << "\nEnter grades for student " << student + 1 << endl;

      // obtain each test grade
      for ( int test = 0; test < TESTS; test++ )
      {
         // prompt user for and input grade
         cout << "Enter grade on test " << test + 1 << " (0-100): ";
         cin >> grade;

         // prevent invalid input
         while ( grade < 0 || grade > 100 )
         {
            cout << "\nError: Please enter grade in the range 0-100"
                 << endl;

            // prompt user for and input grade
            cout << "Enter grade " << test + 1 << ": ";
            cin >> grade;
         } // end while

         studentGrades[ student ][ test ] = grade; // set grade
      } // end inner for

   } // end outer for

} // end function getStudentGrades

// display student grades and averages
void displayGrades( int studentGrades[][ TESTS ] )
{
   double studentTotal = 0.0; // sum of a student's test grades
   double testTotal = 0.0; // sum of grades on a particular test
   double classTotal = 0.0; // class's total grades

   // display a header
   cout << "\nGrade summary" << endl;
   cout << "-------------" << endl;
   cout << left << setw( 14 ) << "Student number"
        << right << setw( 8 ) << right << "Test 1"
        << setw( 8 ) << "Test 2"
        << setw( 8 ) << "Test 3"
        << setw( 9 ) << "Average" << endl;

   // display the grades and averages for each student
   for ( int student = 0; student < STUDENTS; student++ )
   {
      // display student number
      cout << left << setw( 14 ) << student + 1;

      // sum each row of the array
      for ( int test = 0; test < TESTS; test++ )
      {
         int grade = studentGrades[ student ][ test ]; // get grade
         cout << right << setw( 8 ) << grade; // display grade

         // add grade to student's total
         studentTotal += grade; 
         classTotal += grade; // add test grade to test total
      } // end inner for

      // calculate and display student average
      cout << right << setw( 9 ) << studentTotal / TESTS << endl;

      studentTotal = 0.0; // reset student total
   } // end outer for

   // display test averages
   cout << "\n" << left << setw( 14 ) << "Test averages";

   // display the average grades on each test
   for ( int test = 0; test < TESTS; test++ )
   {
      // add each student's grade to the test total
      for ( int student = 0; student < STUDENTS; student++ )
      {
         testTotal += studentGrades[ student ][ test ];
      } // end inner for

      // calculate and display test average
      cout << right << setw( 8 ) << testTotal / STUDENTS;

      testTotal = 0.0; // reset test total
   } // end for

   cout << endl; // insert newline

   // calculate and display the class average
   double classAverage = classTotal / ( STUDENTS * TESTS );
   cout << "\nClass average: " << classAverage << endl << endl;
} // end function displayGrades

/**************************************************************************
 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 * Pearson Education, Inc. All Rights Reserved.                           *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -