Two- Dimensional Array in C | Two-dimensional array to store and print values in C

preview_player
Показать описание
Two-dimensional array to store and print values
// C program to store temperature of two cities of a week and display it.
#include opentag stdio.h closetag
#include opentag conio.h closetag
const int CITY = 2;
const int WEEK = 7;
void main()
{
int temperature[CITY][WEEK];
// Using nested loop to store values in a 2d array
for (int i = 0; i lessthan CITY; ++i)
{
for (int j = 0; j lessthan WEEK; ++j)
{
printf("City %d, Day %d: ", i + 1, j + 1);
scanf("%d", &temperature[i][j]);
}
}
printf("\nDisplaying values: \n\n");
// Using nested loop to display vlues of a 2d array
for (int i = 0; i lessthan CITY; ++i)
{
for (int j = 0; j lessthan WEEK; ++j)
{
printf("City %d, Day %d = %d\n", i + 1, j + 1, temperature[i][j]);
}
}
getch();
}

Output

City 1, Day 1: 33
City 1, Day 2: 34
City 1, Day 3: 35
City 1, Day 4: 33
City 1, Day 5: 32
City 1, Day 6: 31
City 1, Day 7: 30
City 2, Day 1: 23
City 2, Day 2: 22
City 2, Day 3: 21
City 2, Day 4: 24
City 2, Day 5: 22
City 2, Day 6: 25
City 2, Day 7: 26

Displaying values:

City 1, Day 1 = 33
City 1, Day 2 = 34
City 1, Day 3 = 35
City 1, Day 4 = 33
City 1, Day 5 = 32
City 1, Day 6 = 31
City 1, Day 7 = 30
City 2, Day 1 = 23
City 2, Day 2 = 22
City 2, Day 3 = 21
City 2, Day 4 = 24
City 2, Day 5 = 22
City 2, Day 6 = 25
City 2, Day 7 = 26
Рекомендации по теме
welcome to shbcf.ru