Вы находитесь на странице: 1из 7

Laboratory 2

1. Questions
1. Write a program to read and perform addition and multiplication of two
matrices of order m * n, add them and display the resultant matrix using
functions.
2. Write a program to read a string and check for palindrome without using string
related function (a string is palindrome if its half is mirror by itself eg: abcdcba).
3. Write a program to perform binary search. Use recursion.

2. Introduction
In this lab the student will learn about conditional statements, how to use printf and scanf
functions, how to use header files and will get to know how the loop works.

3. Algorithm
1.
I. Definr variable.
II. Use scanf inbuilt function to take input for matrix.
III. Use sum and multiply function for adding and multiplying matrixes.

2.
a. Definr variable .
b. Use scanf inbuilt function to take input for string.
c. Use for loop to check if the given string is palindrome or not.

3.
a. Initialise the required variables.
b. Create a function binarysearch before the main.
c. Take input for elements a of the list from the user, and the element to be searched
by the user.
d. Then perform the search operation by taking midpoint, if the element is In lesser
position then it will do left operation as shown in program if the position is to the right
then if will do right operation as shown in program.
e. If condition is satisfied it will show the position.
4. Program
1) Perform addition and multiplication matrix and store the resultant matrix using
functions
#include <stdio.h>
#include <stdlib.h>
void sum(int a[12][12],int b[12][12],int ,int);
void multiply(int a[12][12],int b[12][12],int ,int );
void main(int argc, char** argv) {
int a[12][12],b[12][12];
int i,j,k,c,r;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st 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]);
}
printf("\n Enter elements of 2nd 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", &b[i][j]);
}
printf("The 1st matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The 2nd matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
sum(a,b,r,c);
multiply(a,b,r,c);
}
void multiply(int a[12][12],int b[12][12],int r,int c)
{

int mul[12][12],i,j,k;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]=mul[i][j]+a[i][k]*b[k][j];
}
}
}

printf("The product of two matrices\n");


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

void sum(int a[12][12],int b[12][12],int r,int c)


{
int sum[12][12],i,j;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
printf("\n The Sum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
{
printf("\n\n");
}
}
}
2) Program to read a string and check for palindrome without using string related
function (a string is palindrome if its half is mirror by itself eg: abcdcba).

#include <stdio.h>
#include <string.h>

int main(){
char string1[20];
int i, length;
int flag = 0;

printf("Enter a string:");
scanf("%s", string1);

length = strlen(string1);

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


if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}

if (flag) {
printf("%s is not a palindrome\n", string1);
}
else {
printf("%s is a palindrome\n", string1);
}
return 0;
}

3) Program to perform binary search. Use recursion.

#include <stdio.h>
int binarysearch(int a[], int low, int high, int x) {
int mid = (low + high) / 2;
if (low > high) return -1;
if (a[mid] == x) return mid;
if (a[mid] < x)
return binarysearch(a, mid + 1, high, x);
else
return binarysearch(a, low, mid-1, x);
}
int main(void) {
int a[100], i;
int len, pos, search_item;
printf("Enter the length of the array\n");
scanf("%d", &len);
printf("Enter the array elements\n");
for (i=0; i<len; i++)
scanf("%d", &a[i]);
printf("Enter the element to search\n");
scanf("%d", &search_item);
pos = binarysearch(a,0,len-1,search_item);
if (pos < 0 )
printf("Cannot find the element %d in the array.\n", search_item);
else
printf("The position of %d in the array is %d.\n", search_item, pos+1);
return 0;
}

5. Presentation of results
1) Output for addition and multiplication of matrix using functions
2) Check palindrome or not

3) Binary search using recursion:


6. Conclusion

Here we learned a program to read and perform addition and multiplication of


two matrices of order m * n, add them and display the resultant matrix using
functions and to read a string and check for palindrome without using string
related function and a program to perform binary search. Use recursion.

Вам также может понравиться