C Program to Store Temperature of Two Cities for a Week and Display It

Here we will write a C program to store display temperature of two cities for week. The prerequisite concept, what is a two-dimensional array. Using this example we will learn how to take input from the user and display it for multidimensional array.

For multidimensional array many for loops needed. If we are working with one-dimensional array (simple array) then we require one for loop. If we are dealing with two-dimensional or 2D array then we need two for loop.

A Two-dimensional array is the array of a one-dimensional array. The first loop represents a one-dimensional array and the second loop represents an array of the one-dimensional array that is a two-dimensional array. For a three-dimensional array, three loops will be needed.

Store & Display Temperature of Two Cities For a Week

We will take two constant integer variables CITY and WEEK. These variables will be constant for this program. So, the value of CITY and WEEK can’t change within the program. A constant integer variable is not mandatory but we are writing program only for 2 city and 7 days. Here, 2 and 7 are constant that’s why we used const int.

#include<stdio.h>
const int CITY=2;
const int WEEK=7;

int main()
{
   int temperature[CITY][WEEK];
   int i ,j;

   /*Take input from user*/
   for(i=0;i<CITY;i++)
   {
       for(j=0;j<WEEK;j++)
       {
           printf("City[%d], Day[%d]: ", i+1, j+1);
           scanf("%d", &temperature[i][j] );
       }
       printf("\n");
   }

   /*Display output*/
   printf("Displaying Values:\n\n");

   for(i=0;i<CITY;i++)
   {
       for(j=0;j<WEEK;j++)
       {
           printf("City[%d], Day[%d]=%d\n", i+1, j+1, temperature[i][j]);
       }
       printf("\n");
   }

   return 0;
}

Output:-

City[1],Day[1]: 25
City[1],Day[2]: 28
City[1],Day[3]: 30
City[1],Day[4]: 37
City[1],Day[5]: 30
City[1],Day[6]: 27
City[1],Day[7]: 26

City[2],Day[1]: 15
City[2],Day[2]: 18
City[2],Day[3]: 20
City[2],Day[4]: 27
City[2],Day[5]: 20
City[2],Day[6]: 17
City[2],Day[7]: 16

Displaying Values:

City[1],Day[1]=25
City[1],Day[2]=28
City[1],Day[3]=30
City[1],Day[4]=37
City[1],Day[5]=30
City[1],Day[6]=27
City[1],Day[7]=26

City[2],Day[1]=15
City[2],Day[2]=18
City[2],Day[3]=20
City[2],Day[4]=27
City[2],Day[5]=20
City[2],Day[6]=17
City[2],Day[7]=16

To take input from the user we need 2 for loops. Using scanf() function value will be stored in the temperature variable. After taking input from the user we need to display it. This time also we will use two for loops.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

More C programming examples on multidimensional array

1 thought on “C Program to Store Temperature of Two Cities for a Week and Display It”

Leave a Comment

Your email address will not be published. Required fields are marked *