Pages

Ads 468x60px

Saturday, 2 June 2012

C PROGRAM FOR FINDING SMALLEST NUMBER AND LARGEST NUMBER USING TWO DIMENSION MATRIX


 C PROGRAM FOR FINDING SMALLEST NUMBER AND LARGEST NUMBER USING TWO DIMENSION MATRIX



For the following question, use array bus[ ][ ] in the code. By referring to thegiven Figure, the rows represent the bus routes and the columns represent
the days that the buses run. The numbers in the slots of the array show thenumber of passengers that were on a given route on a given day.
int bus[4][5];
a. Read the data into the array by receiving the data by columns. A
portion 
b. Print the number of passengers as shown in the Figure.
c. Print the total passengers for Mon.
d. Print the total number of passengers for the route stored in row 0.
e. Print the maximum number of passengers for the route stored in
row 3.
f. Print the minimum number of passengers for the Thu.
g. Print the average number of passengers for all days and all routes.
h. Find the row with the largest number of passengers and print that
number and the index.
The following are sample answers for g and h.



CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,r,c,a[10][10],largest=0,smallest,z=1,t;
clrscr();
printf("Enter the r and c value:");
scanf("%d%d",&r,&c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
}
largest=0;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(largest {
largest=a[i][j];
}
}
}
t=a[1][1];
a[1][1]=smallest;
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
if(smallest>a[i][j])
{
smallest=a[i][j];
}
else
{
z++;
}
if(z==(r*c))
{
smallest=t;
}
}
}
printf("largest elemement=%d\n",largest);
printf("smallest element=%d\n",smallest);
getch();
}

0 comments:

Post a Comment

Total Pageviews