/* ************************************************************************** * Program name : 011_Grade_and_marks (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/06 - first version * ************************************************************************** * Description : (a) input upto 10 the test result * * (b) count the no. of result to each grade * * (c) output results * ************************************************************************** */ #include #include int main() { // part 1 : declaration and set the initial values int index, marks, count_A, count_B, count_C, count_D, count_E, count_U; marks= count_A= count_B= count_C= count_D= count_E= count_U=0; // part 2 : input for (index=1; index<=10; index++) // Note (1) { do { cout << "Enter the test result (-1 means to stop) for student " << index << " : "; cin >> marks; } while (marks<-1 || marks>100); // Note (2) if (marks==-1) break; // Note (3) if (marks>=90) count_A+=1; // Note (4) else if (marks>=80) count_B+=1; else if (marks>=70) count_C+=1; else if (marks>=60) count_D+=1; else if (marks>=50) count_E+=1; else count_U+=1; } // part 3 : output cout << "\n\aSummary of " << index-1 << " test results : "; // Note (5) cout << "\nGarde A is " << count_A; cout << "\nGarde B is " << count_B; cout << "\nGarde C is " << count_C; cout << "\nGarde D is " << count_D; cout << "\nGarde E is " << count_E; cout << "\nGarde F is " << count_U << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) for structure : for (index=1; index<=10; index++) index= 1 -- establish initial value of index to 1 index<=10 -- test if the final value of index has not been reached index++ -- increment the value of index by 1 Examples A: for (i=7; i<= 63; i+= 7) means vary the variable i from 7 to 63 in steps of 7 Examples B: for (i=10; i<= 0; i-= 2) means vary the variable i from 10 downto 0 in steps of -2 (2) while structure : while (marks<-1 || marks>100); Repeat the loop of asking the test results if the marks is out of range ( -1 to 100). (3) Specical flag : if (marks==-1) break; The for loop will be terminated when the valus of the variable index is greater than 10 (as explained in Note (1)) or the inpuuted marks is -1. The break statement causes immediate exit from the for structure at once. (4) assignment operator : count_A+=1; OLD count_A NEW count_A count_A+=1; means count_A= count_A+1 6 6+1 = 7 count_A+=5; means count_A= count_A+5 6 6+5 = 11 count_A-=1; means count_A= count_A-1 6 6-1 = 5 count_A*=2; means count_A= count_A*2 6 6*2 = 12 count_A/=2; means count_A= count_A/2 6 6/2 = 3 count_A%=4; means count_A= count_A%4 6 6%4 = 2 (remainder) (5) The number of valid test results is : index-1. */