Program of Array

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Sorting :

#include <stdio.h>

main()

int i, j, a, n, number[30];

printf("Enter the value of N \n");

scanf("%d", &n);

printf("Enter the numbers \n");

for (i = 0; i < n; ++i)

scanf("%d", &number[i]);

for (i = 0; i < n; ++i)

for (j = i + 1; j < n; ++j)

if (number[i] > number[j])

a = number[i];

number[i] = number[j];

number[j] = a;
}

printf("The numbers arranged in ascending order \n");

for (i = 0; i < n; ++i)

printf("%d\n", number[i]);

Searching :

#include <stdio.h>

main()

{ int num;

int i, keynum, found = 0;

printf("Enter the number of elements ");

scanf("%d", &num);

int array[num];

printf("Enter the elements one by one \n");


for (i = 0; i < num; i++)

scanf("%d", &array[i]);

printf("Enter the element to be searched ");

scanf("%d", &keynum);

for (i = 0; i < num ; i++)

if (keynum == array[i] )

found = 1;

break;

if (found == 1)

printf("Element is present in the array at position %d",i+1);

else

printf("Element is not present in the array\n");

}
Matrix multiplication :

#include<stdio.h>

main()

int r1,r2,c1,c2;

printf("Enter number of rows for First Matrix:\n");

scanf("%d",&r1);

printf("Enter number of columns for First Matrix:\n");

scanf("%d",&c1);

printf("Enter number of rows for Second Matrix:\n");

scanf("%d",&r2);

printf("Enter number of columns for Second Matrix:\n");

scanf("%d",&c2);

if(c1!=r2)

printf("Matrices Can't be multiplied together");

else

{
int m1[r1][c1],m2[r2][c2];

printf("Enter first matrix elements \n");

for(int i=0;i<r1;i++)

for(int j=0;j<c1;j++)

scanf("%d",&m1[i][j]);

printf("Enter Second matrix elements\n");

for(int i=0;i<r2;i++)

for(int j=0;j<c2;j++)

scanf("%d",&m2[i][j]);

int mul[r1][c2];

for(int i=0;i<r1;i++)

{
for(int j=0;j<c2;j++)

mul[i][j]=0;

for(int k=0;k<c1;k++)

mul[i][j]+=m1[i][k]*m2[k][j];

printf("Multiplied matrix\n");

for(int i=0;i<r1;i++)

for(int j=0;j<c2;j++)

printf("%d\t",mul[i][j]);

printf("\n");

}
}

You might also like