WEEK 8:
Matrix problems, String operations, Bubble sort
i) Addition of two matrices
ii) Multiplication two matrices
iii) Sort array elements using bubble sort
iv) Concatenate two strings without built-in functions
v) Reverse a string using built-in and without built-in string functions
8i) Addition of two matrices
PROGRAM:
/* C Program to perform the addition of two matrices*/
#include<stdio.h>
#include <conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j;
clrscr();
printf("Enter the order of 1st matrix ");
scanf("%d%d",&m,&n);
printf("Enter the order of 2nd matrix ");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("Enter the elements of 1st matrix " );
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of 2nd matrix ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("\n Addition of Two Matrices is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("\n Wrong order ");
getch();
return 0;
}
(Leave 13 lines space for output)
8 ii) Multiplication two matrices
PROGRAM:
/* C Program to perform the multiplication of two matrices*/
#include<stdio.h>
#include <conio.h>
int main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter the order of 1st matrix ");
scanf("%d%d",&m,&n);
printf("Enter the order of 2nd matrix ");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("Enter elements of 1st matrix " );
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of 2nd matrix ");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("\n Multiplication of Two Matrices is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("\n Wrong order ");
return 0;
}
(Leave 14 lines space for output)
8 iii) Sort array elements using Bubble Sort
Program:
/* C program to sort the array elements using Bubble Sort*/
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[30], i, j, n, temp;
clrscr();
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:", n);
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=0; i<n-1; i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Array elements after sorting using Bubble Sort technique:\n");
for(i=0; i<n; i++)
printf("%d\t", arr[i]);
getch();
return 0;
}
(Leave 10 lines space for output)
8 iv) Concatenate two strings without using built-in functions
Program:
/* C program to Concatenate two strings without using built-in functions */
#include<stdio.h>
#include<conio.h>
int main()
{
char str1[25],str2[25];
int i=0,j=0;
clrscr();
printf("Enter First String:");
gets(str1);
printf("Enter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
printf("Concatenated String is %s",str1);
getch();
return 0;
}
(Leave 6 lines space for output)
8v) Reverse a string using built-in and without built-in string functions
a) C Program to reverse the given string using built in function
Program:
/* String Reverse with using strrev( ) fucntion*/
#include<stdio.h>
#include<conio.h>
int main ()
{
char a[30] ;
clrscr();
printf ("Enter any string:");
gets (a);
strrev (a);
printf("Reverse of the given string = %s",a);
getch ();
return 0;
}
(Leave 4 lines space for output)
b) C Program to reverse the given string without using built in function
Program:
/* String Reverse without using strrev( ) fucntion*/
#include <stdio.h>
#include<conio.h>
int main()
{
char s[20], r[20];
int begin, end, count = 0;
clrscr();
printf("Enter any string:");
gets(s);
while (s[count] != '\0')
count++;
end = count - 1;
for (begin = 0; begin < count; begin++)
{
r[begin] = s[end];
end--;
}
r[begin] = '\0';
printf("Reverse of the given string is %s:", r);
getch();
return 0;
}