C++ 1) Array testGrades contains NUM_VALS test scores. Write a for loop that set
ID: 3935968 • Letter: C
Question
C++
1) Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int testGrades[NUM_VALS];
int i = 0;
int sumExtra = -9999; // Assign sumExtra with 0 before your for loop
testGrades[0] = 101;
testGrades[1] = 83;
testGrades[2] = 107;
testGrades[3] = 90;
/* Your solution goes here */
cout << "sumExtra: " << sumExtra << endl;
return 0;
}
2) Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:
Note that the last element is not followed by a comma, space, or newline.
#include <iostream>
using namespace std;
int main() {
const int NUM_VALS = 4;
int hourlyTemp[NUM_VALS];
int i = 0;
hourlyTemp[0] = 90;
hourlyTemp[1] = 92;
hourlyTemp[2] = 94;
hourlyTemp[3] = 95;
/* Your solution goes here */
cout << endl;
return 0;
}
Explanation / Answer
Answer:
1)
for(i = 0;i < NUM_VALS; i++) {
if(testGrades[i] > 100)
sumExtra = sumExtra + (testGrades[i]-100);
}
2)
for ( i = 0; i < NUM_VALS; i++)
{
cout<<hourlyTemp[i];
if(i < NUM_VALS -1)
{
cout<<", ";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.