2017-01-19 5 views
-1

그래서 내 코드는 내가 원하는대로 컴파일되고 작동합니다. 이것은 기본적으로 칠판입니다. 사용자는 배정 횟수와 배정 당 가중치를 백분율로 입력합니다. 그런 다음 한 명 또는 여러 명의 학생에 대한 평균 점수를 계산할 것인지 선택할 수 있습니다.가중 평균 합산 방법 C++

여기 내 문제는 일부 오류 검사를하고 싶었 기 때문에 무게의 합계를 얻으려고했지만 인쇄 할 때 나 대신 마지막 무게가 나옵니다. 합계 ... 나는 100을 얻으려고 노력하고있다. 누군가 내가 엉망인 곳을 말해 줄 수 있니?

멍청한 질문을 용서해주십시오 ... 이것은 처음으로 C++로 프로그램을 작성한 것이므로이 점에 대해 많이 이해하지 못합니다 ---> [i].

#include <iostream> 
using namespace std ; 

int main(int argc, char **argv) 
{ 
    const int maxSize = 100 ; 
    float weight [ maxSize ] ; 
    float grades [ maxSize ] ; 

    int numAssignments ; 

    cout << "    WELCOME TO BLACKBOARD ! " << endl ; 
    cout << " (This program was created for the calculation grades) " << endl ; 
    cout << " " << endl ; 
    cout << " To start, enter number of assignments: " ; 
    cin >> numAssignments ; 

    cout << " thanks! " ; 
    cout << " Now, enter weight of assignments " << endl ; 
    out << " (total sum of the weights should be 100) " << endl ; 

    float sum = 0.0 ; 
    int i = 0 ; 
    for (int i = 0; i < numAssignments; i++) 
    { 
     cout << " Assignment " << i + 1 << " : " ; 

     cin >> weight [i] ; 
     weight[i] /= 100 ; 

     sum += weight[i] * 100 ; 

    } 
cout<<"sum is: "<< sum<<endl; 

편집 항목 :

은 내가 전체 평균을 얻을 잘못하고 있었는지 알아 냈어. 이 블록에서 는 :

float sum = 0.0 ; 
    int i = 0 ; 
    for (int i = 0; i < numAssignments; i++) 
    { 
     cout << " Assignment " << i + 1 << " : " ; 

     cin >> weight [i] ; 

     float perc ; 
     perc = weight[i] /= 100 ; 

     sum += perc * 100 ; 

    } 
cout<<"sum is: "<< sum<<endl; 

는 내가 원하는 결과를 얻을 수있는 저렴한 방법 이죠하지만 벡터와 제안하는 방법을 이해하는데 실제로 매우 유용했다 :

float sum = 0.0 ; 
    int i = 0 ; 
    for (int i = 0; i < numAssignments; i++) 
    { 
     cout << " Assignment " << i + 1 << " : " ; 

     cin >> weight [i] ; 
     weight[i] /= 100 ; 

     sum += weight[i] * 100 ; 

    } 
cout<<"sum is: "<< sum<<endl; 

나는 다른 변수라는 퍼크를 추가 그것도 해결할 수 있습니다.

+1

If 당신은 배열과 그것들을 사용하는 법을 알지 못한다. 나는 당신에게 [좋은 초보자를 찾는다.] 제안한다. (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)) 읽다. –

+0

http://www.cplusplus.com/doc/tutorial/arrays/ – jamek

+1

컴파일 오류를 수정하면 [재현 할 수 없습니다] (http://ideone.com/w9tRaF). (코드를 복사하여 붙여 넣기하지 말고 다시 입력 한 경우, 실제 코드에'='또는'= +'가있는 것으로 의심됩니다.이 코드의 코드는'+ ='입니다.) – molbdnilo

답변

0

여기 내 고정 코드를 벡터로 업데이트했습니다. 그것은 컴파일하고 내가 원하는 것을 대부분합니다 ... 수학에서 빠지다는 사실을 제외하고 학생의 학년 당 총 무게의 바닥에 간단한 평균값을 줄 수는 없습니다 ...

#include <iostream> 
    #include <string> 
    #include <vector> 
    using namespace std ; 

    int main(int argc, char **argv) 
    { 
     const int maxSize = 100 ; 

     vector <float> weight (maxSize) ; 


     cout << "    WELCOME TO BLACKBOARD ! " << endl ; 
     cout << " " << endl ; 
     cout << " (This program was created to calculate grades) " << endl ; 
     cout << " " << endl ; 

     // Here is where the user inputs the number of assignments 
     cout << " To start, enter number of assignments: " ; 
     int numAssignments ; 
     cin >> numAssignments ; 

     cout << " " << endl ; 
     cout << " Now, enter weight (in percent) of assignments: " << endl ; 
     cout << " " << endl ; 

     // Loop to input the weighted value per assignment 
     float sum = 0.0 ; 
     float num ; 
     for (int i = 0; i < numAssignments; i++) 
     { 
      cout << " Assignment " << i + 1 << " : " ; 

      cin >> num ; 
      weight.push_back(num/100) ; 

      sum += num ; 



     } 
     cout << " Total Weight is: "<< sum << endl ; 

     vector <float> grades (maxSize) ; 

     // Input number of students 

     cout << endl ; 
     cout << " Please, enter number of students graded : " ; 
     int numStud ; 
     cin >> numStud ; 

     // Loop for the calculation of average grade per student 


     for (int j = 0 ; j < numStud ; j++) 
     { 
      cout << endl ; 
      cout << " Enter grades for student " << j + 1 << ": " << endl ; 
      float numGrade ; 
     for (int k = 0 ; k < numAssignments ; k++) 
      { 
       cout << " Grade for assignment " << k + 1 << ": " ; 

       cin >> numGrade ; 

       grades.at(numGrade) ; 
      } 

      float totalWeight = 0.0 ; 
      totalWeight += num * numGrade ; 

      char letterGrade ;  
      if (totalWeight >= 90) 
      letterGrade = 'A' ; 
      else if (totalWeight >= 80) 
      letterGrade = 'B' ; 
      else if (totalWeight >= 70) 
      letterGrade = 'C' ; 
      else if (totalWeight >= 60) 
      letterGrade = 'D' ; 
      else 
      letterGrade = 'F' ; 

      cout << " weight average is : " << totalWeight << endl ;  
      cout << " Letter grade is : " << letterGrade << endl ; 
     } 

     return 0; 
    }