Answer:
The sum of the temperatures is: 336
Step-by-step explanation:
2-Dimensional Array:
A two dimensional array is defined as
int temp [3] [3]
This is how a two dimensional array is defined in C++ programming language.
Here we have defined a 2-D array named temp which can store 3*3 = 9 int values
These 9 values are stored in 3 rows and 3 columns as below:
1, 2, 3
4, 5, 6
7, 8, 9
We are given the temperature for the month of April 2017 recorded by the Zambia metro-logical department.
24.0, 17.5, 33.0, 36.5, 36.0, 22.5, 12.5, 44.0, 15.5, 33.0, 41.5, 23.5
The given temperature values are 12 therefore, we would need a matrix of 3 rows and 4 columns ( 4 rows and 3 columns is also valid)
3*4 = 12 values
These 12 values will be arrange as below:
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
C++ Program:
#include <iostream>
using namespace std;
int main()
{
// initialize the number of rows and columns
int row = 3;
int col = 4;
// initialize variable sum to store the sum of temperature values
int sum = 0;
// initialize 2d array with given temperature values
float temp[row][col] = { {24.0, 17.5, 33.0, 36.5}, {36.0, 22.5, 12.5, 44.0}, {33.0, 41.5, 23.5, 15.5} };
// we need two for loops to keep track of rows and columns
for(int i = 0; i < row; ++i)
{
for(int j = 0; j < col; ++j)
{
// to display individual temperature values
cout<< "temp[" << i << "][" << j << "] = " << temp[i][j] <<endl;
// to sum the elements of the 2d array
sum+=temp[i][j];
}
}
// to display the sum of temperature values
cout<<"The sum of the temperatures is: "<<sum<<endl;
return 0;
}
Output:
temp[0][0] = 24
temp[0][1] = 17.5
temp[0][2] = 33
temp[0][3] = 36.5
temp[1][0] = 36
temp[1][1] = 22.5
temp[1][2] = 12.5
temp[1][3] = 44
temp[2][0] = 33
temp[2][1] = 41.5
temp[2][2] = 23.5
temp[2][3] = 15.5
The sum of the temperatures is: 336
Note:
The index start at 0 not 1 that is why you see rows from 0 to 2 and columns from 0 to 3.