Arrays Programs

 C Program to calculate the sum of all elements of an array .
```#include<stdio.h>
int main( ) {
int i,sum=0;
int a[5];
printf("Enter 5 elements : ");
for(i=0; i < 5; i++) {
scanf("%d",&a[i]);
}
for(i=0; i < 5; i++) {
sum = sum +a[i];
}
printf("SUM = %d",sum);
return 0;
}```
OUTPUT :
```Enter 5 elements : 10 20 30 25 35
SUM = 120```
 C Program to find largest number of an array .
```#include<stdio.h>
int main( ) {
int i,max;
int a[5];
printf("Enter 5 elements : ");
for(i=0; i < 5; i++) {
scanf("%d",&a[i]);
}
max = a[0];
for(i=0; i < 5-1; i++) {
if( a[i+1] > a[i] )
max = a[i+1];
}
printf("Largest Number = %d",max);
return 0;
}```
OUTPUT :
```Enter 5 elements : 10 12 15 23 20
Largest Number = 23```
 C Program to find smallest element of an Array .
```#include<stdio.h>
int main( ) {
int i,min;
int a[5];
printf("Enter 5 elements : ");
for(i=0; i < 5; i++) {
scanf("%d",&a[i]);
}
min = a[0];
for(i=0; i < 5-1; i++) {
if( a[i+1] < a[i] )
min = a[i+1];
}
printf("Smallest Number = %d",min);
return 0;
}```
OUTPUT :
```Enter 5 elements : 10 12 15 23 20
Smallest Number = 10```
 C Program to find the second smallest element of an array .
```#include< stdio.h>
int main() {
int a[50],n,i,j=0,min,smin;
printf("Enter Array Size: ");
scanf("%d",&n);
printf("Enter %d element: ",n);
for(i=0;i< n;i++)
scanf("%d",&a[i]);
min=a[0];
for(i=1;i< n;i++){
if(min > a[i]) {
min = a[i];
j = i;
}
}
smin=a[n-j-1];
for(i=1;i< n;i++){
if(smin > a[i] && j != i)
smin =a[i];
}
printf("2nd smallest:%d",smin);
return 0;
}```
OUTPUT :
```Enter Array Size: 5
Enter 5 element: 1 2 3 4 5
2nd smallest:2```
 C Program to find the second largest element of an array .
```#include< stdio.h>
int main() {
int a[50],n,i,j=0,max,smax;
printf("Enter Array Size: ");
scanf("%d",&n);
printf("Enter %d element: ",n);
for(i=0;i< n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=1;i< n;i++) {
if(max < a[i]) {
max=a[i];
j = i;
}
}
smax=a[n-j-1];
for(i=1;i< n;i++) {
if(smax < a[i] && j != i)
smax = a[i];
}
printf("2nd Largest: %d",smax);
return 0;
}```
OUTPUT :
```Enter Array Size: 5
Enter 5 element: 1 2 3 4 5
2nd Largest: 4```
 C Program to search an element in an array .
```#include<stdio.h>
int main( ) {
int i,search,flag = 0;
int a[5];
printf("Enter 5 elements : ");
for(i=0; i < 5; i++) {
scanf("%d",&a[i]);
}
printf("Enter no. to search : ");
scanf("%d",&search);
for(i=0; i < 5; i++) {
if( a[i] == search ) {
flag = 1;
break;
}
}
if(flag == 1) {
printf("Fount at %d", (i+1));
}
else {
printf("Not Fount");
}
return 0;
}```
OUTPUT :
```Enter 5 elements : 10 12 15 23 20
Enter no.to search : 15
Found at 3 ```

 C Program to reverse an array. .
```#include<stdio.h>
int main( ) {
int i, j;
int orig[5], rev[5];
printf("Enter array elements : ");
for (i = 0; i < 5 ; i++){
scanf("%d", &orig[i]);
}
for (i=4, j=0; i>=0; i--,j++){
rev[j]=orig[i];
}
printf("Reverse array is : ");
for (i = 0; i < 5; i++){
printf("%d  ", rev[i]);
}
return 0;
}
```
OUTPUT :
```Enter the array elements : 1 2 3 4 5
Reverse array is : 5  4  3  2  1```
 C Program to perform addition of two matrix .
```#include<stdio.h>
int main( ) {
int r=3, c=3, i, j ;
int m1[3][3], m2[3][3],m3[3][3];
printf("Enter elements of mat1: ");
for(i=0; i < r; i++){
for(j=0; j < c; j++){
scanf("%d", &m1[i][j]);
}
}

printf("Enter elements of mat2: ");
for(i=0; i < r; i++){
for(j=0 ; j < c; j++){
scanf("%d", &m2[i][j]);
}
}

printf("Sum of Matrix :\n");
for(i=0; i < r; i++) {
for(j=0 ; j < c; j++) {
m3[i][j] = m1[i][j]+m2[i][j];
printf("%d  ", m3[i][j]);
}
printf("\n");
}
return 0;
}```
OUTPUT :
```Enter the elements of mat1 :
1 2 3
2 3 4
1 5 4
Enter the elements of mat2 :
6 5 3
3 4 5
6 5 3
Sum of Matrix :
7  7   6
5  7   9
7  10  7```
 C Program to Subtract two matrix .
```#include<stdio.h>
int main( ) {
int r=3, c=3, i, j ;
int m1[3][3], m2[3][3],m3[3][3];
printf("Enter elements of mat1: ");
for (i = 0; i < r; i++){
for (j = 0; j < c; j++){
scanf("%d", &m1[i][j]);
}
}

printf("Enter elements of mat2: ");
for (i = 0; i < r; i++){
for (j = 0 ; j < c; j++){
scanf("%d", &m2[i][j]);
}
}

printf("Difference of Matrix :\n");
for (i = 0; i < r; i++) {
for (j = 0 ; j < c; j++) {
m3[i][j] = m1[i][j]-m2[i][j];
printf("%d  ", m3[i][j]);
}
printf("\n");
}
return 0;
}```
OUTPUT :
```Enter elements of mat1 :
6 5 3
3 4 5
6 5 3
Enter elements of mat2:
1 2 3
2 3 4
1 5 4
Difference of Matrix :
5  3  0
1  1  1
5  0  -1```
 C Program to multiply two Matrix .
```#include<stdio.h>
int main( ) {
int r1=3, c1=2, r2=2, c2=4;
int i, j, k, sum = 0;
int m1[3][2], m2[2][4], m3[3][4];
printf("Enter elements of mat1: ");
for (i = 0; i < r1; i++){
for (j = 0; j < c1; j++){
scanf("%d", &m1[i][j]);
}
}
printf("Enter elements mat2: ");
for (i = 0; i < r2; i++){
for (j = 0; j < c2; j++){
scanf("%d", &m2[i][j]);
}
}

for (i=0; i < r1; i++){
for (j=0; j < c2; j++){
for (k=0; k < r2; k++){
sum = sum + m1[i][k]*m2[k][j];
}
m3[i][j] = sum;
sum = 0;
}
}
printf("Product of Matrix : \n");
for (i=0; i < r1; i++) {
for (j=0; j < c2; j++){
printf("%d  ", m3[i][j]);
}
printf("\n");
}
return 0;
}```
OUTPUT :
```Enter elements of mat1 :
1 3
3 4
2 3
Enter elements of mat2 :
1 2 3 4
3 4 5 6
Product of Matrix :
10  14  18  22
15  22  29  36
11  16  21  26```
 C Program to display the Transpose of Matrix .
```#include<stdio.h>
int main( ){
int r=3, c=2, i, j;
int mat[3][2], trans[2][3];
printf("Enter elements of mat: ");
for (i=0; i < r; i++){
for(j=0; j < c; j++){
scanf("%d",&mat[i][j]);
}
}
for (i=0; i < r; i++){
for( j=0 ; j < c ; j++ ){
trans[j][i] = mat[i][j];
}
}
printf("Transpose of Matrix: ");
for (i = 0; i < c; i++){
for (j = 0; j < r; j++){
printf("%d  ",trans[i][j]);
}
printf("\n");
}
return 0;
}```
OUTPUT :
```Enter elements of Mat :
1 2
3 4
5 6
Transpose of Matrix :
1  3  5
2  4  6```

#### CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved