You are on page 1of 4

Ex.

No: 6

Simple programming for one dimensional and two


dimensional arrays.

ExNo:6.a

Selection Sort

Aim:
To read input list numbers using one dimensional array, to sort the list
and display resulting sorted list using one dimensional array.
Algorithm:
Step1: Start
Step2: Read the number of elements present in the list as size
Step3: Read the element present in the list using for loop statement in a single
dimensional array list
Step 4: Select the smallest element in the list and exchange it with the top most
location of unsorted list and then mark that as sorted.
Step5: Repeat step 4 size times.
Step6: Display the final sorted list
Step7: Stop
Program:
#include<stdio.h>
void main ()
{
int i,j,min,temp,list[20],size;
printf(\n Enter no of elements in list);
scanf(%d,&size);

printf(\n enter the list);


for(i=0;i<size;i++)
scanf(%d,&list[i]);
for(i=0;i<size;i++)
{
min=i;
for(j=i+1;j<size;i++)
if(list[j]<list[min])
min=j;
if(min!=i)
{
temp=list[i];
list[i]=list[min];
list[min]=temp;
}
printf(\n after selection sort.. The sortd list);
for(i=0;i<size;i++)
printf(%d\t,list[i]);
}
Result:
Thus the C program to perform Selection Sort using one dimensional array
is created and output verified successfully.

ExNo:6.b

MATRIX ADDITION

Aim:
To read matrices using two dimensional array, add the matrices and
display the resultant matrix using two dimensional array.
Algorithm:
Step1: Start
Step2: Read the number of rows and columns present in matrix one and
matrix two.
Step3: Input matrix one element according to specified number of rows and
columns using two dimensional array and for loop statement.
Step 4: Input matrix two element according to specified number of rows and
columns using two dimensional array and for loop statement.
Step5: Add matrices one and two and store in the resultant matrix.
Step6: Display the resultant matrix.
Step7: Stop
Program:

#include <stdio.h>
void main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

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


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0 ; d < n; d++)
{
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
}
Result:
Thus the C program to perform matrix addition using two dimensional array
is created and output verified successfully

You might also like