0
0
C++programming~30 mins

Common array operations in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Common array operations
📖 Scenario: You are working with a list of daily temperatures recorded over a week. You want to perform some common operations on this list to understand the data better.
🎯 Goal: Build a simple C++ program that stores daily temperatures in an array, calculates the average temperature, finds the highest temperature, and prints the results.
📋 What You'll Learn
Create an array with exactly 7 integer temperature values
Create a variable to hold the sum of temperatures
Use a for loop with index variable i to iterate over the array
Calculate the average temperature as a float
Find the highest temperature using a variable maxTemp
Print the average and highest temperature exactly as shown
💡 Why This Matters
🌍 Real World
Arrays are used to store collections of data like temperatures, sales numbers, or scores. Knowing how to process arrays helps analyze and summarize data.
💼 Career
Many programming jobs require working with arrays and loops to handle lists of information efficiently.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temperatures with these exact values: 23, 25, 20, 22, 24, 26, 21
C++
Need a hint?

Use the syntax int arrayName[size] = {values}; to create the array.

2
Set up sum and max variables
Create an integer variable sum and set it to 0. Create an integer variable maxTemp and set it to the first element of temperatures.
C++
Need a hint?

Initialize sum to 0 and maxTemp to the first temperature value.

3
Calculate sum and find max temperature
Use a for loop with index variable i from 0 to 6 to add each temperature to sum. Inside the loop, update maxTemp if temperatures[i] is greater than maxTemp.
C++
Need a hint?

Loop from 0 to 6, add each temperature to sum, and update maxTemp if needed.

4
Calculate average and print results
Create a float variable average and set it to sum divided by 7.0. Then print the average temperature with the text "Average temperature: " and the highest temperature with the text "Highest temperature: ".
C++
Need a hint?

Divide sum by 7.0 to get a float average. Use std::cout to print the results.