Challenge: Working with the Array
Reminder how to access an array by index
main.cpp
12345678910#include <iostream> int main() { int myArray[3] = { 67, 23, 87 }; std::cout << myArray[0] << std::endl; std::cout << myArray[1] << std::endl; std::cout << myArray[2] << std::endl; }
Note
Index counting starts from zero, making the first element in a list or array the zeroth element.
Swipe to start coding
You have a student with grades in 4 subjects. Your task is to calculate the studentβs average grade across all subjects.
- The function
calculateAverage
takes an array of 4 integers representing the studentβs grades. - Inside
calculateAverage
, add up all the grades in the array. - Then, divide the total by 4.0 (as a
double
) to get the average grade.
Example
{80, 90, 75, 95} => 85.0
{100, 100, 100, 100} => 100.0
{70, 80, 75, 76} => 75.25
Solution
solution.cpp
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you give an example of accessing an array element by index?
What happens if I try to access an index that doesn't exist?
Can you explain how negative indices work in arrays?
Awesome!
Completion rate improved to 3.85
Challenge: Working with the Array
Swipe to show menu
Reminder how to access an array by index
main.cpp
12345678910#include <iostream> int main() { int myArray[3] = { 67, 23, 87 }; std::cout << myArray[0] << std::endl; std::cout << myArray[1] << std::endl; std::cout << myArray[2] << std::endl; }
Note
Index counting starts from zero, making the first element in a list or array the zeroth element.
Swipe to start coding
You have a student with grades in 4 subjects. Your task is to calculate the studentβs average grade across all subjects.
- The function
calculateAverage
takes an array of 4 integers representing the studentβs grades. - Inside
calculateAverage
, add up all the grades in the array. - Then, divide the total by 4.0 (as a
double
) to get the average grade.
Example
{80, 90, 75, 95} => 85.0
{100, 100, 100, 100} => 100.0
{70, 80, 75, 76} => 75.25
Solution
solution.cpp
Thanks for your feedback!
single