You are on page 1of 11

Array Programs

• Display the largest element in the array


#include <stdio.h>

int main()
{
int i, n, arr[10];

printf("Enter total number of elements(1 to 10): ");


scanf("%d", &n);
printf("\n");

// Stores number entered by the user


for(i = 0; i < n; ++i)
{
printf("Enter Number %d: ", i+1);
scanf("%f", &arr[i]);
}

// Loop to store largest number to arr[0]


for(i = 1; i < n; ++i)
{
// Change < to > if you want to find the smallest element
if(arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %d", arr[0]);

return 0;
}

Output
Enter total number of elements(1 to 10): 8

Enter Number 1: 23

Enter Number 2: -34

Enter Number 3: 50

Enter Number 4: 33

Enter Number 5: 55

Enter Number 6: 43

Enter Number 7: 5

Enter Number 8: -66

Largest element = 55

• Find Smallest Element in Array


#include<stdio.h>
int main() {
int a[30], i, num, smallest;

printf("\nEnter no of elements :");


scanf("%d", &num);

//Read n elements in an array


for (i = 0; i < num; i++)
scanf("%d", &a[i]);
//Consider first element as smallest
smallest = a[0];

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


if (a[i] < smallest) {
smallest = a[i];
}
}

// Print out the Result


printf("\nSmallest Element : %d", smallest);

return (0);
}

Output
Enter no of elements : 5
11 44 22 55 99
Smallest Element : 11

• Add two matrices using 2-D array


#include <stdio.h>
int main()
{
int a[2][2], b[2][2], c[2][2];
int i, j;
// Taking input using nested for loop
printf("Enter elements of 1st matrix\n");
for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
printf("Enter a[%d][%d]: ", i+1, j+1);
scanf("%f", &a[i][j]);
}
}

// Taking input using nested for loop


printf("Enter elements of 2nd matrix\n");
for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
printf("Enter b[%d][%d]: ", i+1, j+1);
scanf("%f", &b[i][j]);
}
}

// adding corresponding elements of two arrays


for(i=0; i<2; ++i)
{
for(j=0; j<2; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}

// Displaying the sum


printf("\nSum Of Matrix:");

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


{
for(j=0; j<2; ++j)
{
printf("%d\t", c[i][j]);

if(j==1)
printf("\n");
}
}
return 0;
}

Ouput

Enter elements of 1st matrix

Enter a[1][1]: 2;

Enter a[1][2]: 5;

Enter a[2][1]: 1;

Enter a[2][2]: 2;

Enter elements of 2nd matrix

Enter b[1][1]: 2;

Enter b[1][2]: 0;

Enter b[2][1]: 23;

Enter b[2][2]: 5;
Sum Of Matrix:

4 5

24 7

• Transpose of a matrix
#include <stdio.h>

int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);

// Storing elements of the matrix


printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}

// Displaying the matrix a[][] */


printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}

// Finding the transpose of matrix a


for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}

// Displaying the transpose of matrix a


printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}

return 0;
}

Output

Enter rows and columns of matrix: 2

Enter element of matrix:

Enter element a11: 2


Enter element a12: 3

Enter element a13: 4

Enter element a21: 5

Enter element a22: 6

Enter element a23: 4

Entered Matrix:

2 3 4

5 6 4

Transpose of Matrix:

2 5

3 6

4 4

• Find Trace and Normal of Matrix


[A Trace calculated by adding up the Main Diagonal Elements of the Matrix. The Main Diagonal
Elements are the ones that occur from Top Left of Matrix Down To Bottom Right Corner. A
Normal is the Square Root of all the Elements in the Trace of a Matrix.]

#include<stdio.h>
#include<math.h>

int main ()
{
int arr[5][5];
int i, j, normal, trace = 0, rows, columns, sum = 0, temp = 0;
printf("\nEnter Number of Rows of Matrix:\t");
scanf("%d", &rows);
printf("\nEnter Number of Rows of Matrix:\t");
scanf("%d", &columns);
printf("\nEnter the Elements of Matrix:\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
scanf("%d", &arr[i][j]);
}
}
for(i = 0; i < rows; i++)
{
for(j = 0; j < columns; j++)
{
temp = arr[i][j] * arr[i][j];
sum = sum + temp;
}
}
normal = sqrt(sum);
printf("\nThe Matrix\n");
for(i = 0; i < rows; i++)
{
for(j = 0; j< columns; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
for(i = 0; i < rows; i++)
{
trace = trace + arr[i][i];
}
printf("\nTrace of Matrix:\t%d\n", trace);
printf("\nNormal of Matrix:\t%d\n", normal);
return 0;
}
• Sorting using bubble sort
Bubble Sort algorithm
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements
if they are in wrong order.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm does
not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm
needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

(The following program is for ascending sort)


#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

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


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

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

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


printf("%d\n", array[c]);

return 0;
}

You might also like