0
0
Cprogramming~30 mins

Common array operations - 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 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 integer array with exactly 7 elements representing daily temperatures.
Create an integer variable to hold the sum of temperatures.
Use a for loop with the variable i to iterate over the array elements.
Calculate the average temperature as a float.
Find the highest temperature using a variable called max_temp.
Print the average temperature and the highest temperature using printf.
💡 Why This Matters
🌍 Real World
Analyzing temperature data helps in weather forecasting and understanding climate patterns.
💼 Career
Knowing how to work with arrays and loops is essential for many programming jobs, especially in data processing and embedded systems.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temperatures with exactly 7 elements: 23, 25, 19, 22, 24, 20, 21.
C
Need a hint?

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

2
Create a sum variable
Create an integer variable called sum and set it to 0 to hold the total of temperatures.
C
Need a hint?

Initialize sum to zero before adding temperatures.

3
Calculate the sum and find the max temperature
Use a for loop with the variable i from 0 to 6 to add each element of temperatures to sum. Also, create an integer variable max_temp initialized to the first element of temperatures. Inside the loop, update max_temp if the current temperature is higher.
C
Need a hint?

Use for (int i = 0; i < 7; i++) to loop through the array.

4
Calculate average and print results
Create a float variable called average and set it to sum / 7.0f. Then use printf to print the average temperature with one decimal place and the highest temperature as an integer.
C
Need a hint?

Use float average = sum / 7.0f; to get a decimal average.

Use printf("Average temperature: %.1f\n", average); to print one decimal place.