EXERCISE-7
1. Write a program in C to search an element in a row wise and column wise sorted matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10], r, c, i, j, ele;
printf("Enter matrix size:");
scanf("%d%d",&r,&c);
printf("Enter %d X %d matrix elements:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
printf("Enter search element:");
scanf("%d",&ele);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(mtrx[i][j]==ele)
break;
}
if(j!=c)
break;
}
if(i!=r && j!=c)
printf("%d element is found at %d-row and %d-column.\n",ele,i,j);
else
printf("%d element is not found.\n",ele);
}
2. Write a program in C to print individual characters of string in reverse order.
SOURCE CODE:
#include<stdio.h>
#include<string.h>
int main(){
char str[50];
int i,len;
printf("Enter string:\n");
gets(str);
len = strlen(str);
printf("String in reverse order:\n");
for(i=len-1;i>=0;i--)
printf("%c",str[i]);
return 0;
}
OUTPUT:
Enter string:
hello world
String in reverse order:
dlrow olleh
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10], r, c, i, j, ele;
printf("Enter matrix size:");
scanf("%d%d",&r,&c);
printf("Enter %d X %d matrix elements:\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
printf("Enter search element:");
scanf("%d",&ele);
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(mtrx[i][j]==ele)
break;
}
if(j!=c)
break;
}
if(i!=r && j!=c)
printf("%d element is found at %d-row and %d-column.\n",ele,i,j);
else
printf("%d element is not found.\n",ele);
}
OUTPUT:
Enter matrix size:3 3
Enter 3 X 3 matrix elements:
1 2 3
4 5 6
7 8 9
Enter search element:5
5 element is found at 1-row and 1-column.
2. Write a program in C to print individual characters of string in reverse order.
SOURCE CODE:
#include<stdio.h>
#include<string.h>
int main(){
char str[50];
int i,len;
printf("Enter string:\n");
gets(str);
len = strlen(str);
printf("String in reverse order:\n");
for(i=len-1;i>=0;i--)
printf("%c",str[i]);
return 0;
}
OUTPUT:
Enter string:
hello world
String in reverse order:
dlrow olleh
No comments