2014-09-23 36 views
0

我的目標是拿(3)學生,給他們每個(3)的成績,然後每個學生的這些成績的平均值以及字母等級。這部分運作良好。我需要的最後一部分是取這些(3)平均成績,加上它們,然後再取平均值作爲班級平均成績。C++ |一個數組,switch語句?需要一些指導

我應該怎樣才能讓班級平均?陣列?切換語句?我問的原因是因爲我真的不知道。我完全無能爲力,因爲我如何才能做到這一點。

#include <iostream> 
#include <ostream> 
#include "stdafx.h" 
using namespace std; 

// start the program 
int main() { 
    // LOCAL VARIABLES 
    // int student1, student2, student3;  //the (3) students -- NOT EVEN SURE 
    // I NEED THIS 
    int all_students; // number of students 
    double grade1, grade2, grade3; // the (3) grade variable declarations 
    double grade_total; // holds the sum of all grades 
    double grade_avg; // holds the average of the sum of grades 

    cout << "Enter the number of students in the class: "; // number of students 
                  // request 
    cin >> all_students; 

    all_students = 1; // initialize counter for the loop 

    // need to create a loop for all three students and input for 3 
    // grades/student 
    while (all_students <= 3) // start the loop for the students 
    { 
     cout << "\n\nPlease enter (3) numeric grades for student " 
      << all_students << ":"; // enters the 3 grades for student 
     cin >> grade1 >> grade2 >> 
      grade3; // the grades are stored in these 3 variables 

     grade_total = grade1 + grade2 + 
         grade3; // sum is the 3 sets of graders added together 
     grade_avg = grade_total/3; // avg. is the sum divided by 3 

     // displays the output for the student grades, grade average, and letter 
     // grade 
     cout << "Student " << all_students << " grades are \n" << grade1 << "\n" 
      << grade2 << "\n" << grade3 << "\n" 
      << "with an average of: " << grade_avg 
      << ", which is letter grade: "; // need this line to also read the 
              // letter grade for the average 
              // grade 

     if (grade_avg >= 90) // 90+ gets an A 
      cout << "A"; 
     else if (grade_avg >= 80) // 80-89 gets a B 
      cout << "B"; 
     else if (grade_avg >= 70) // 70-79 gets a C 
      cout << "C"; 
     else if (grade_avg >= 60) // 60-69 gets a D 
      cout << "D"; 
     else // anything less than a 60% is an F 
      cout << "F"; 

     // increases counter! Incrementer. 
     all_students++; // Yes, I want to increment the count after the loop. 

     //****************************************************************// 
     //  I need to figure out how to create an array to hold  // 
     //  each student average in order to calculate the overall // 
     //  class average. Or use a switch statement? Advice?  // 
     //****************************************************************// 
     //****************************************************************// 
     //****************************************************************// 
    } 
} 

回答

1

最簡單的方法是有一個正在運行的平均

float classAvg = 0; 
const unit NUM_STUDENTS = 3; 
while (all_students <= NUM_STUDENTS)  //start the loop for the students 
{ 
    ... 
    classAvg += grade_avg; 
} 

classAvg /= NUM_STUDENTS 
cout<<"Class average is " << classAvg; 

我還建議讓該數字年級字母等級轉換成一個功能的部分。這將使您的代碼更加模塊化和更清晰。沿着線的東西:

void displayLetterGrade(float grade) 
{ 
     if (grade >= 90) // 90+ gets an A 
      cout << "A"; 
     else if (grade >= 80) // 80-89 gets a B 
      cout << "B"; 
     else if (grade >= 70) // 70-79 gets a C 
      cout << "C"; 
     else if (grade >= 60) // 60-69 gets a D 
      cout << "D"; 
     else // anything less than a 60% is an F 
      cout << "F"; 
} 
+0

我同意的平均值的averge。我會剛剛返回一個字符串/字符,但我不想更改他的代碼太多。我會更新示例以反映您的評論。 – ventsyv 2014-09-23 17:54:44

+0

太棒了。我會重做我的代碼以反映您的建議。謝謝。我會回來看看其他人是否還有其他建議。我感謝你的時間。 – 2014-09-23 17:58:14

+0

因此,在添加void函數時,編譯器告訴我:錯誤C2601:'displayLetterGrade':本地函數定義是非法的。它告訴我,在(浮動等級)之後{<---應該有一個';',這是沒有意義的。不知道該怎麼做。 – 2014-09-24 15:44:20

1

您需要創建的同時與學生的最大數,那麼(在它的端部)while循環外的陣列中的每個計算的平均分配到陣列中的一個條目。

然後while循環外,寫一個for循環來計算陣列中的元件,其是每個學生

+0

這也有道理。雖然聽起來好像我可以通過走另一條路線來簡化它。無論如何,我可能需要嘗試2或3種不同的選項,所以也請您給予指導。歡迎您撥打 – 2014-09-23 17:59:44

+0

,告訴我們它何時有效 – OmarGW 2014-09-23 18:09:16