0% found this document useful (0 votes)
18 views

1a B C Lab Program Solution

Uploaded by

sujay15042005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

1a B C Lab Program Solution

Uploaded by

sujay15042005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

a) Write a program to accept 3 integers and find the maximum among 3 numbers using
functions and pointers.
#include <stdio.h>
void large(int *,int *, int *);
int main() {
int x,y,z;
printf("Enter three integers\n");
scanf("%d%d%d",&x,&y,&z);
large(&x,&y,&z);
return 0;
}
void large(int *a, int *b, int *c)
{
if(*a>*b && *a>*c)
printf("%d is greater",*a);
else if(*b>*a && *b>*c)
printf("%d is greater",*b);
else
printf("%d is greater",*c);
}
Output
Enter three integers
9
10
11
11 is greater

1.b) write a program using pointer for searching the desired element from the array using
pointer.
#include <stdio.h>
void search(int *,int *,int *);
int main() {
int num,i,a[10],key;
printf("Enter the number of elements to the array\n");
scanf("%d",&num);
printf("Enter the elements to the array\n");
for(i=0;i<num;i++)
scanf("%d",&a[i]);
printf("Enter element to be search\n");
scanf("%d",&key);
search(a,&num,&key);

1
return 0;
}
void search(int *arr,int *n,int *ele)
{
int i, flag=0;
for(i=0;i<*n;i++)
{
if(*arr==*ele)
{
flag=1;
printf("Element %d found at position %d",*ele,i+1);
break;
}
arr++;
}
if(flag==0)
printf("Element not found");
}
Output1
Enter the number of elements to the array
5
Enter the elements to the array
1
2
3
4
5
Enter element to be search
5
Element 5 found at position 5

Output2
Enter the number of elements to the array
3
Enter the elements to the array
5
9
4
Enter element to be search
3
Element not found

2
1.c) write a program to find maximum element in each row of the matrix using pointers.
#include <stdio.h>
void maximum(int (*a)[10],int *r,int*c)
{
int i=0,j,max=0,res[20];
for(i=0;i<*r;i++)
{
for(j=0;j<*c;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
res[i]=max;
max=0;
}
printf("Maximum element in each row is:\n");
for(i=0;i<*r;i++)
{
printf("%d\n",res[i]);
}

int main() {
int arr[10][10],m,n,i,j;
printf("Enter the number of rows and coloumns\n");
scanf("%d%d",&m,&n);
printf("Enter the elemnts to the matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&arr[i][j]);
}
maximum(arr,&m,&n);
return 0;
}
Output
Enter the number of rows and coloumns
2
3

3
Enter the elemnts to the matrix
1
2
3
4
5
6
Maximum element in each row is:
3
6

You might also like