display list of cities using 2 dimensional array in c
Answers
Answer:
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. First for loop represents one-dimensional array and the second for loop represents an array of the one-dimensional array that is a two-dimensional array. For three-dimensional array three for loop will be needed.
Store display temperature of two cities for week
#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
Explanation
We take two constant integer variables CITY and WEEK. These are 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.
To take input from user we need 2 for loops. Using scanf() function value will be stored in the temperature variable. After taking input from user we need to display it. This time also we will use two for loops.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
More C programming examples on multidimensional array
Passing multidimensional array to Function
Find Largest and smallest in the 2D array with their position
Matrix Operations
C programming examples on One-dimensional array
Sum of Array elements
Sort a list of an array element
Search an Array element
Find Smallest and largest Array element with their position
Share this:
Share
Save
More
Post navigation
Previous PostNext Post
Leave a Reply
Search
Recent Posts
Oracle Constraint
Oracle Rename Constraint
Drop Constraint In Oracle
Disable Constraint In Oracle
How to Check Oracle Database Version
How to Find SID in Oracle
How To Check Database Size in Oracle
EMP DEPT and SALGRADE Tables Queries
Oracle PL/SQL IF Statement
Top Posts
C++ Program to Find the Sum and Average of Three Numbers
Two Dimensional (2D) Array of Strings in C
Different Methods to Read and Write String in C
C Program to find Grade of a Student Using Switch Statement
C Program to Find Sum and Average of 3 Numbers
C Program to Convert Lowercase Character to Uppercase Character
C++ Program to Add Subtract Divide Multiply of Two Numbers
C Program for Addition Subtraction Multiplication Division using Function
Fibonacci Series in C Using Function
Learn
Java Tutorial
Java Examples
JDBC
C Tutorial
C Examples
C Quiz
C++ Examples
Company
Home
Recent
Blog
About Us
Contact Us
Legal
Disclaimer
Privacy Policy
Terms & Conditions
Contribute
Write an Article
Support Us