EXAMPLES OF ARRAY
#include<stdio.h>
int main()
{
int num[]={34,56,38,57,34,44};
int i;
for(i=0; i<=5; i++){
printf(“address=%u”, &num[i]);
printf(“%d”, num[i]);
}
return 0;
}
write a program to find min and max
#include<stdio.h>
void main()
{
int length =5;
int a[length]={5,3,1,10,2};
int min=a[0];
int max=a[0];
for(int i=1; i<=length-1; i++)
{
if(min>a[i])
{
min=a[i];
}
if(max<a[i])
{
max=a[i];
}
}
printf(“%d %d”, min, max);
}
write a program to find index value of an element
#include<stdio.h>
void main()
{
int a[length]={5,3,1,10,2};
int value=1;
for(int i=1; i<=length-1; i++)
{
if(value==a[i])
{
printf(“index is %d”, i);
break;
}
}
}
WAP to read n elements into an array. Print them in reverse.
#include<stdio.h>
void main()
{
int a[100],n,i;
// read array size.
printf(“Enter array size :”);
scanf(“%d”,&n);
// read elements into array.
printf(“Enter %d elements”,n);
for( i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“array element in reverse \n”);
for(i=n-1;i>=0;i--)
printf(“%5d”,a[i]);
}
Output :
Enter array size : 5
Enter 5 elements10 20 30 40 50
Array elements in reverse 50 40 30 20 10
WAP to read n elements into an array. Find max and min elements.
#include<stdio.h>
void main()
{
int a[100],n,i,max,min;
///read array size.
printf(“Enter array size :”);
scanf(“%d”,&n);
/// Read array elements
printf(“Enter %d elements”,n);
for( i=0;i<n;i++)
scanf(“%d”,&a[i]);
///find max and min
max=min=a[0];
for( i=0;i<n;i++)
{
if( a[i] > max)
max=a[i];
if( a[i] < min )
min=a[i];
}
printf(“Max=%d”,max);
printf(“\nMin=%d”,min);
}
Output :
Enter array size : 6
Enter 6 elements10 8 5 12 15 9
Max=15
Min=5
WAP to read n elements into an array. Sort the array.
#include<stdio.h>
void main()
{
int a[100],n,i,j,t;
/// read array size
printf(“Enter array size :”);
scanf(“%d”,&n);
/// read array elements
printf(“Enter %d elements : “,n);
for( i=0;i<n;i++ )
scanf(“%d”,&a[i] );
/// Sort the array.
for( i=0;i<n;i++ )
{
for( j=i+1;j<n;j++)
{
if( a[i] > a[j] )
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
/// print the result
printf(“Sorted array\n”);
for( i=0;i<n;i++ )
printf(“%5d”,a[i]);
}
Output:
Enter array size: 6
WAP to read n elements into an array. Swap the elements with index.
#include<stdio.h>
void main()
{
int a[100],b[100],n,i;
/// read array size
printf(“Enter array size :”);
scanf(“%d”,&n);
/// read array elements.
printf(“Enter %d elements”,n);
for( i=0;i<n;i++ )
scanf(“%d”,&a[i] );
/// Swap elements with index.
for( i=0;i<n;i++ )
// b[index]=value
// b[value]=index
b[ a[i] ]=i;
/// print the result
printf(“Array after swap index with elements \n”);
for( i=0;i<n;i++ )
printf(“%5d”,b[i]);
}
Output:
Enter array size: 5
Enter 5 elements3 4 0 2 1
Array after swap index with elements
2 4 3 0 1
WAP to find the missing number of an array.
#include<stdio.h>
void main()
{
int a[100],n,i,sum=0,missing;
///read array size
printf(“enter array size :”);
scanf(“%d”,&n);
/// read array elements
printf(“Enter %d elements”,n);
for( i=0;i<n;i++ )
{
scanf(“%d”,&a[i] );
sum=sum+a[i];
}
missing=(n+1)*(n+2)/2-sum;
printf(“Missing number =%d”,missing);
}
Output:
enter array size: 6
Enter 6 elements1 2 3 5 6 7
Missing number =4
WAP to read mxn elements into an arrays. Print it on screen.
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j;
/// read array size
printf(“Enter array size :”);
scanf(“%d%d”,&m,&n);
/// read array elements
printf(“Enter %d elements”,m*n);
for( i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
/// print array.
printf(“Array is\n”);
for( i=0;i<m;i++)
{
for( j=0;j<n;j++ )
printf(“%5d”,a[i][j]);
printf(“\n”);
}
}
Output:
Enter array size: 2 3
Enter 6 elements 1 2 3 4 5 6
Array is
1 2 3
4 5 6
WAP to read mxn elements into an array, find the transpose matrix.
#include<stdio.h>
void main()
{
int a[10][10],t[10][10],m,n,i,j;
///read array size
printf(“Enter array size :”);
scanf(“%d%d”,&m,&n);
/// read array elements
printf(“enter %d elements”,m*n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
///Generate transpose matrix.
for(i=0;i<m;i++)
for(j=0;j<n;j++)
t[j][i]=a[i][j];
/// print transpose matrix.
printf(“Transpose matrix=\n”);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf(“%5d”,t[i][j]);
printf(“\n”);
}
}
Output:
Enter array size: 2 3
enter 6 elements
1 2 3
4 5 6
Transpose matrix=
1 4
2 5
3 6
WAP to print lower diagonal matrix of mxn size.
Enter array size: 4 4
Lower diagonal matrix is
1 0 0 0
1 1 0 0
1 1 1 0
#include<stdio.h>
void main()
{
int a[10][10],m,n,i,j;
///read array size
printf(“Enter array size :”);
scanf(“%d%d”,&m,&n);
if( m!=n )
printf(“Lower diagonal matrix can not be generated”);
else
{
for( i=0;i<m;i++)
for(j=0;j<n;j++)
{
if( i>=j )
a[i][j]=1;
else
a[i][j]=0;
}
printf("Lower diagonal matrix is \n");
for( i=0;i<m;i++ )
{
for(j=0;j<n;j++)
printf(“%5d”,a[i][j]);
printf(“\n”);
}
}
}
Output:
1 1 1 1