Qns:
Write a C++ program that reads from the command line a set of game scores.
Your program should do the followings:
• Determine the number of game scores in the command line using the
argc variables.
• Display the number of game scores in the command line.
• Find the highest game score.
• Display the highest scores.
For example, suppose the following games scores are entered in the command
line:
92 73 63 97 52 34 41 24 46 17
The, the program should display the following output:
Number of scores : 10
The highest score is 97
The below is the code i was given as a guide!
But i dont understand why is there a int num?
and why is there a check if num > max?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "Number of scores : "
<< (argc - 1) << endl;
int max = 0;
int num;
for (int i=1; i<argc; i++) {
num = atoi(argv[i]);
if (num > max)
max = num;
}
cout << "The highest score is "
<< max << endl;
return 0;
}
| |