I am writing a program to read the test scores of ten students into an array and then print the test scores with the corresponding letter grade and comment.
I think the for loop I have in main should be in the function readTestScores but it is not running correctly when I have it there.
This is what I've done so far:
(Some of the code is commented out so I could try running the program without any errors)
Does the for loop go in main or the function readTestScores()?
The instruction for #2 is: function void readTestScores(double scoreList[], int size) that receives an array to hold students' test scores and an integer value n and then reads n test scores into that array.
for you to read n variables your function definitely need a loop there, your main would just call
the function once with the array passed as an argument
After entering in the 10 test scores, I am getting an error for bad access at the end of the main function.
How can I pass the scores from array scoreList to function printTestResults() to print the test scores with the corresponding letter grades and comments?
in addation, i see you only need one array not two
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
double scoreList[]{};///initialize size as adviced above
///your calls in main should be like
readTestScores(scoreList, size); ///where size is already defined above
printTestResults(scoreList, size);
///define your print score function properly here are some pointers
///1.the function should use a loop to iterate through the array upto size times
///2.on each iteration call the func getchargrade passing it the value stored
/// on that index, store the return value in a variable.
// - use that variable to call getcomment and store the result
// - print the three values on one line.
// score ---- chargrade ----- comment
// 3.continue the loop till the end of the array
On line 11 you define "size", but never give it a value or initialize it, so it contains garbag. Then on line 18 you send that garbage to the "readTestScores" function. Luckily you do not use "size" in the function so it does not matter at this point. You also pass "size" to the "printTestResults" function, but never use it.
The "printTestResults" function has potential, but does not work with what you have. The function receives two parameters, but neither are used. The function should step through the array. In the 'cout" statement you could call the "getLetterGrade" and "printComment" functions to not only print the numerical score from the array, but use the array for "getLetterGrade" and use "getLetterGrade" for the comment. One possible solution:
When I compiled the program I found problems at the beginning of main. The "scoreList" array did not have a size inside the []. The same for "testList" which is never used.
Line 19 is using the wrong array. "testList" is empty and never receives any values.
I changed "size" to constexprint SiZE{ 10 }; and then used it in lines 12 and 13 between the []s.
My beginning of main looks like this:
1 2 3 4 5 6 7
int main()
{
mstd::SetScreen();
constexprint SIZE{ 10 };
double scoreList[SIZE]{ 90, 80, 60, 26, 91, 81, 75, 86, 95, 100 }; // <--- Used for testing.
double testList[SIZE]{}; // <--- Never used.
This runs fine in Xcode but I get these errors when compiling:
Program4.cpp: In function `int main()':
Program4.cpp:23: error: `constexpr' undeclared (first use this function)
Program4.cpp:23: error: (Each undeclared identifier is reported only once for each function it appears in.)
Program4.cpp:23: error: expected `;' before "int"
Program4.cpp:24: error: `size' undeclared (first use this function)
Program4.cpp:24: error: expected primary-expression before "double"
Program4.cpp:24: error: expected `;' before "double"
Program4.cpp:30: error: `scoreList' undeclared (first use this function)
It appears that you're compiling with a pre-C++11 compiler. You should really see if you can compile to one of the most current standards preferably C++14. if that's not possible replace constexpr with const.