2016-12-01 69 views
-2

我目前正在研究C++類的程序,該程序將處理並生成一個學期的學生成績單,假定該學生正在學習五個班級(將輸出打印到屏幕上以圖表格式)。學生將爲每門課程輸入三個等級。應該有一個數組用於課程名稱,第一個考試成績,第二個考試成績和第三個考試成績。使用數組C++計算三個年級的平均值

好,夠公平的。那麼我已經設法初始化上面的數組並在下面的代碼中正確地輸出它們。這是我有點困惑的地方。接下來,我必須初始化一個數組,該數組將計算每個課程的三個考試成績的平均值,並初始化每個課程的平均字母等級的數組(並以與考試相同的方式將其打印到屏幕上成績和課程名稱)。也許如果有人能夠闡明如何創建一個可以計算三個年級的平均值的數組,我可以嘗試弄清楚如何爲字母等級製作一個數組。謝謝。

# include iostream 

using namespace std; 
const int SIZE =5; 

void getData(string courseName[], float examOne[], float examTwo[], float examThree[]); 


int main() 
{ 
    // local declaration 
    string courseName[SIZE]; 
    float examOne[SIZE]; 
    float examTwo[SIZE]; 
    float examThree[SIZE]; 

    getData(courseName,examOne,examTwo,examThree); 


    cout<<"\n\n\n"; 

    for (int i = 0; i <SIZE; i++){ 
     cout<< courseName[i]<<" "<<examOne[i]<<" "<<examTwo[i]<<"  "<<examThree[i]<<" "<<total[i]<< endl; 

    } 
    return 0; 
} 



void getData(string courseName[],float examOne[],float examTwo[],float examThree[]){ 

    for (int i = 0; i <SIZE; i++){ 
    cout<<"Enter Course Name: "; 
    cin >> courseName[i]; 
    cout<<"enter First Exam Grade: "; 
    cin>>examOne[i]; 
    cout<<"enter Second Exam Grade: "; 
    cin>>examTwo[i]; 
    cout<<"enter Third Exam Grade: "; 
    cin>>examThree[i]; 

    } 
} 
+0

爲什麼不直接使用存儲數據的數組,然後創建一個sum變量,然後除以數組的大小?在這種情況下,它會是3 – 2016-12-01 01:38:52

+3

數組不計算。代碼確實。數組存儲數據。你不能創建一個計算平均值的數組,因爲數組不能算數。 –

+0

這是真的,謝謝 – red

回答

0

編輯:

我添加了一個函數來顯示學生的平均品位爲字母。

using namespace std;被認爲是不好的做法,有時可能會很危險。檢查this。 聲明SIZE作爲全局變量也被認爲是不好的做法。

#include <iostream> 
#include <string> 

void getData(int sizeArray, std::string courseName[], float examOne[], float examTwo[], float examThree[], float average[]); 
std::string letterGrade(int grade); 
void highestLowestAverageByExam(float tExamArray[], int tSize, float & tHighestGradeExam, float & tLowestGradeExam, float & tAverageGradeExam); 

int main() 
{ 
    // local declaration 
    const int SIZE = 5; 
    std::string courseName[SIZE]; 
    float examOne[SIZE]; 
    float examTwo[SIZE]; 
    float examThree[SIZE]; 
    float average[SIZE]; 

    getData(SIZE, courseName, examOne, examTwo, examThree, average); 

    std::cout << "\n\n\n"; 

    for (int i = 0; i <SIZE; i++) 
    { 
     std::cout << courseName[i] << " " << examOne[i] << " " << examTwo[i] << "  " 
      << examThree[i] << " " << average[i] << " " << letterGrade(average[i]) << std::endl; 
    } 
    std::cout << "\n"; 

    float highestGradeExam = 0; 
    float lowestGradeExam = 0; 
    float averageGradeExam = 0; 

    highestLowestAverageByExam(examOne, SIZE, highestGradeExam, lowestGradeExam, averageGradeExam); 
    std::cout << "Exam number 1: Highest grade = " << highestGradeExam 
     << " Lowest grade = " << lowestGradeExam 
     << " Average grade = " << averageGradeExam << std::endl; 


    return 0; 
} 

void getData(int sizeArray, std::string courseName[], float examOne[], float examTwo[], float examThree[], float average[]) 
{ 
    for (int i = 0; i < sizeArray; i++) 
    { 
     int count = 0; 
     std::cout << "Enter Course Name: "; 
     std::cin >> courseName[i]; 

     std::cout << "enter First Exam Grade: "; 
     std::cin >> examOne[i]; 
     count++; 

     std::cout << "enter Second Exam Grade: "; 
     std::cin >> examTwo[i]; 
     count++; 

     std::cout << "enter Third Exam Grade: "; 
     std::cin >> examThree[i]; 
     count++; 

     average[i] = (examOne[i] + examTwo[i] + examThree[i])/count; 
    } 
} 


std::string letterGrade(int averageGrade) 
{ 
    if (averageGrade >= 0 && averageGrade <= 100) 
    { 
     const int NUMBER_CATEGORIES = 11; 
     const std::string GRADE_LETTER[] = { "F", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-", "A" }; 
     const int LOWEST_GRADE_SCORE[] = { 0, 60, 67, 70, 73, 77, 80, 83, 87, 90, 93 }; 
     int category = 0; 
     while (category < NUMBER_CATEGORIES && LOWEST_GRADE_SCORE[category] <= averageGrade) 
     { 
      category++; 
     } 
     return GRADE_LETTER[category - 1]; 
    } 
    else 
    { 
     return "Average grade is not valid."; 
    } 
} 

void highestLowestAverageByExam(float tExamArray[], int tSize, float & tHighestGradeExam, float & tLowestGradeExam, float & tAverageGradeExam) 
{ 
    tHighestGradeExam = tExamArray[0]; 
    tLowestGradeExam = tExamArray[0]; 
    for (int i = 1; i < tSize; i++) 
    { 
     if (tExamArray[i] > tHighestGradeExam) 
     { 
      tHighestGradeExam = tExamArray[i]; 
     } 
     if (tExamArray[i] < tLowestGradeExam) 
     { 
      tLowestGradeExam = tExamArray[i]; 
     } 
    } 
    tAverageGradeExam = (tHighestGradeExam + tLowestGradeExam)/2; 
} 
+1

謝謝,這真的有助於澄清我的代碼。我看你是如何在輸入課程名稱之前初始化一個計數,以便將課程的總數除以。 – red

+0

不客氣。我編輯了我的答案,並添加了一個函數來顯示學生的平均成績爲字母。 –

+0

再次您好,我運行了您編輯的代碼,並且如預期的那樣完美運行。儘管如此,我仍然試圖弄清楚它是如何工作的。忍受着我,我對C++頗爲陌生,剛剛在教科書/課程中開始使用數組。爲什麼你將數組放在LOWEST_GRADE_SCORE數組中,並把<= grade ......的int等級設置爲0,因爲除了函數之外,我沒有看到int等級在代碼中的其他位置,或者它沒有爲什麼在返回類型中從類別中減去一個; ---> return GRADE_LETTER [category - 1];如果這沒有意義,我很抱歉。 – red