728x90
For loop와 1차 Array를 사용해서 점수 나타내기
포스트 난이도: HOO_Intern
# Example Codes
For loop와 1차 배열을 사용해서 여러 학생들의 점수를 나타내고 모든 학생의 평균 점수를 구해보는 예제 코드이다.
아래 예제 코드를 통해 For loop와 1차 배열을 이해할 수 있다.
#include <iostream>
using namespace std;
int main()
{
//Variable definitions
const int SIZE = 5;
int studentGrade[SIZE]; // integer array capable of storing 5 elements
double sum = 0; // accumulator variable
double average = 0;
// For loop to read in the grade of each student and add it to accumulator
for (int i =0; i < SIZE; i ++)
{
cout << "Please Enter the Student "<< i + 1 << "'s Grade: ";
cin >> studentGrade[i];
sum = sum + studentGrade[i];
cout << "Student " << i+1 << ": " << studentGrade[i] << endl;
}
// Output "The average of "
average = sum/SIZE;
// For loop to output the grade of each student and the average
for (int i =0; i < SIZE - 1; i++)
{
// Use a conditional expression inside the loop to determine which grade your outputting
// so that the sentence can be formatted correctly.
cout << "Student " << i + 1 << "'s grade is " << studentGrade[i] << endl;
}
cout << "and the last student' grade is " << studentGrade[4] << endl;
cout << "The averge is "<< average << "." << endl;
return 0;
}
Please Enter the Student 1's Grade: 59
Student 1: 59
Please Enter the Student 2's Grade: 69
Student 2: 69
Please Enter the Student 3's Grade: 79
Student 3: 79
Please Enter the Student 4's Grade: 89
Student 4: 89
Please Enter the Student 5's Grade: 99
Student 5: 99
Student 1's grade is 59
Student 2's grade is 69
Student 3's grade is 79
Student 4's grade is 89
and the last student' grade is 99
The averge is 79.
728x90
댓글