Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Working with the Array | Variables and Data Types
C++ Introduction

bookChallenge: Working with the Array

Reminder how to access an array by index

main.cpp

main.cpp

copy
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.

Task

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

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 7
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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?

close

Awesome!

Completion rate improved to 3.85

bookChallenge: Working with the Array

Swipe to show menu

Reminder how to access an array by index

main.cpp

main.cpp

copy
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.

Task

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

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 7
single

single

some-alt