Tuesday, June 15, 2010

C programs on array

Program to  crerate an C array and then to insert a value within the array.

#include < stdio.h >
void insert(int a[],int n)
{
int i,loc,temp,val;
printf("\nEnter the location where you want to insert(1 to %d):",n);
scanf("%d",&loc);
printf("\nEnter the value:");
scanf("%d",&val);
temp=a[loc-1];
a[loc-1]=val;
for(i=loc;i<=n;i++)

{
loc=a[i];
a[i]=temp;
temp=loc;
}
}
void main()
{
int arr[20],i,n;
clrscr();
printf("\nHow many values you want to insert(within 19):");
scanf("%d",&n);
for(i=0;i < n;i++)

{
printf("\nValue=");
scanf("%d",&arr[i]);
}
insert(arr,n);
printf("\nAfter insertion\n");
for(i=0;i < =n;i++)

{
printf("\nValue=%d",arr[i]);
}
getch();
}

C Program on deletion of element from array


#include < stdio.h >
void del(int a[],int n)
{
int i,loc;
printf("\nEnter the location which you want to delete:");
scanf("%d",&loc);
for(i=loc-1;i < n;i++)

{
a[i]=a[i+1];
}
}
void main()
{
int arr[20],i,n;
clrscr();
printf("\nHow many values you want to insert(within 19):");
scanf("%d",&n);
for(i=0;i < n;i++)

{
printf("\nValue=");
scanf("%d",&arr[i]);
}
del(arr,n);
printf("\nAfter deletion\n");
for(i=0;i < n-1;i++)

{
printf("\nValue=%d",arr[i]);
}
getch();
}

Two-Dimensional or 2 d Arrays

2 d array is actually array of arrays. To declare a two-dimensional array variable, specify each additional index using another set of square brackets. For example, to declare a two-dimensional integer array named arr, the syntax is:int arr [x][y];
This means that an array of size 'x' will be created, while within each index there will be another array of size 'y'.
Consider the following example:-
Marks obtained in three subjects by three students is

Roll no. Math Physics Chemistry

1 78 65 60
2 87 56 65
3 77 78 70

If we were asked to store the marks obtained by Roll no.1 ,then we can simply declare an one dimensional array of size 4.Then the Roll no and three subject marks can be stored in the array variable. But here the case is different. We have to store the values in the tabular form in a variable. Here comes the utility of two-dimensional array. We have to declare a 3 by 4 array.

#include < stdio.h >
void main ()
{
int result [3][4];
int i, j;
clrscr ();
for (i=0;i < 3;i++)

{
j=0;
printf ("\nEnter the Roll number:-");
scanf ("%d", &result [i][j]);
for (j=1;j < 4;j++)

{
printf ("\nEnter the marks of subject no.%2d\n", j);
scanf ("%d", &result [i][j]);
}
}
clrscr ();
printf ("The stored values are:\n");
for (i=0;i < 3;i++)

{
for (j=0;j < 4;j++)

{
printf ("%4d", result [i][j]);
}
printf ("\n");
}
getch ();
}


C Program on temperature of cities


The daily maximum temperature of 4 cities for 5 dates are recorded during the month of January. Write a program to find the day and city corresponding to highest temperature and lowest temperature

#include < stdio.h >
void main()
{
int temp[5][4],maxc,minc,maxd,mind,maxt,mint,i,j;
clrscr();
for(i=0;i < 5;i++)

{
for(j=0;j < 4;j++)

{
if(j==0)
printf("\nEnter temp. for Calcutta on %d January\t",i+1);
else if(j==1)
printf("\nEnter temp. for Madras on %d January\t",i+1);
else if(j==2)
printf("\nEnter temp. for Mumbai on %d January\t",i+1);
else if(j==3)
printf("\nEnter temp. for Delhi on %d January\t",i+1);
scanf("%d",&temp[i][j]);
}
}
clrscr();
puts("The recorded temperature:-\n");
puts("Calcutta Madras Mumbai Delhi");
for(i=0;i < 5;i++)

{
for(j=0;j < 4;j++)

{
printf("%10d",temp[i][j]);
}
printf("\n");
}
puts("**************");
for(i=0;i < 5;i++)

{
for(j=0;j < 4;j++)

{
if((i==0)&&(j==0))
{
maxt=temp[i][j];
maxd=i;
maxc=j;
mint=temp[i][j];
mind=i;
minc=j;
}
else
{
if(maxt < temp[i][j]) { maxt=temp[i][j]; maxd=i+1; maxc=j+1; } if(mint > temp[i][j])
{
mint=temp[i][j];
mind=i+1;
minc=j+1;
}
}
}
}
printf("\nThe maximum temperature is :\t%d",maxt);
printf("\nAnd the corresponding date is \t%d",maxd);
printf("\nCity number\t%d",maxc);
printf("\nThe minimum temperature is :\t%d",mint);
printf("\nAnd the corresponding date is \t%d",mind);
printf("\nCity number\t%d",minc);
getch();
}


program on student's result using 2 d array


The annual examination results of 10 students are tabulated as follows
Roll No. sub1 sub2 sub3
--------------------------------
Write a program to read data and determine the following
@Total marks obtain by each student
@The highest marks in each subject with Roll number
@The student who obtained the highest total marks

#include < stdio.h >

void main()
{
int result[10][5],i,j,k,total,maxr,maxm;
clrscr();
for(i=0;i < 10;i++)

{
total=0;
for(j=0;j < 4;j++)

{
if(j==0)
puts("Enter Roll number\t");
else if(j==1)
puts("Enter 1st subject marks\t");
else if(j==2)
puts("Enter 2nd subject marks\t");
else if(j==3)
puts("Enter 3rd subject marks\t");
scanf("%d",&result[i][j]);
if(j!=0)
total+=result[i][j];
}
result[i][j]=total;
}
clrscr();
puts("Mark sheet");
printf("\n%6s%6s%6s%6s%6s\n","Roll","1st","2nd","3rd","Total");
for(i=0;i < 10;i++)

{
for(j=0;j<5;j++)

{
printf("%6d",result[i][j]);
}
printf("\n");
}
puts("Press any key....");
getch();
puts("Roll number with total");
printf("\n%6s%6s\n","Roll","Total");
for(i=0;i < 10;i++)

{
for(j=0;j < 5;j++)

{
if((j==0)||(j==4))
printf("%6d",result[i][j]);
}
printf("\n");
}
for(i=1;i < 4;i++)

{
for(j=0;j < 10;j++)

{
if(j==0)
{
maxm=result[j][i];
maxr=result[j][0];
}
else if(result[j][i]>maxm)
{
maxm=result[j][i];
maxr=result[j][0];
}
}
if(i==1)
printf("\nMaximum marks in sub1 is %d, Roll number %d",maxm,maxr);
else if(i==2)
printf("\nMaximum marks in sub2 is %d, Roll number %d",maxm,maxr);
else if(i==3)
printf("\nMaximum marks in sub3 is %d, Roll number %d",maxm,maxr);
}
for(i=0;i < 10;i++)

{
for(j=0;j < 5;j++)

{
if(j==4)
{
if(i==0)
{
maxm=result[i][j];
maxr=result[i][0];
}
else if(result[i][j]>maxm)
{
maxm=result[i][j];
maxr=result[i][0];
}
}
}
}
printf("\nMaximum total is %d and Roll number %d",maxm,maxr);
getch();
}

To display a 5 by 5 array with the following output

Upper left traingle with +1
Lower left traingle with -1
Right to left diagonal with 0

#include < stdio.h >
void main()
{
int result[5][5],i,j;
clrscr();
for(i=0;i < 5;i++)

{
for(j=0;j < 5;j++)

{
if(i+j==4)
result[i][j]=0;
else if(i+j < 4) result[i][j]=1; else result[i][j]=-1; } } for(i=0;i < 5;i++)

{
for(j=0;j < 5;j++)

{
printf("%2d",result[i][j]);
}
printf("\n");
}
getch();
}


This page on 2-D Array using C Language. Any Question ? Put Your comments

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner