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 | Section
C++ Introduction
Section 1. Chapter 16
single

single

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

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 1. Chapter 16
single

single

Ask AI

expand

Ask AI

ChatGPT

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

some-alt