140k views
5 votes
Program to store temperature of two different cities for a week and display it.#include using namespace std; const int CITY = 2; const int WEEK = 7; int main() { int temperature[CITY][WEEK]; cout << "Enter all temperature for a week of first city and then second city. \\"; // Inserting the values into the temperature array for (int i = 0; i < CITY; ++i) { for(int j = 0; j < WEEK; ++j) { cout << "City " << i + 1 << ", Day " << j + 1 << " : "; cin >> temperature[i][j]; } } cout << "\\\\Displaying Values:\\"; // Accessing the values from the temperature array for (int i = 0; i < CITY; ++i) { for(int j = 0; j < WEEK; ++j) { cout << "City " << i + 1 << ", Day " << j + 1 << " = " << temperature[i][j] << endl; } } return 0; }

User Daiki
by
4.2k points

1 Answer

3 votes

Answer:

Your solution is correct. Let me explain how it works so that it will be clearer for you

Step-by-step explanation:

Two constant values are defined for city and week

The 2D temperature array is declared taking the city as rows and week as columns, rows will hold the cities and columns will hold the temperature for the day of the city

A nested for loop is created to initialize all the elements in the array using cin function to assign values accordingly

When the first loop is done, another nested for loop is created to output the all the elements in the array

User Ziv Gabovitch
by
5.1k points