[go: up one dir, main page]

0% found this document useful (0 votes)
19 views11 pages

Lab Programs Unit 3,4,5

The document contains a series of C programs demonstrating various operations on arrays, including reading and printing elements, calculating sums, finding the largest and smallest elements, inserting and deleting elements, searching using linear and binary search, and sorting using bubble and selection sort. Additionally, it includes programs for handling two-dimensional arrays, such as reading elements, matrix addition, and matrix multiplication. Each program is structured with standard input and output operations, showcasing fundamental programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Lab Programs Unit 3,4,5

The document contains a series of C programs demonstrating various operations on arrays, including reading and printing elements, calculating sums, finding the largest and smallest elements, inserting and deleting elements, searching using linear and binary search, and sorting using bubble and selection sort. Additionally, it includes programs for handling two-dimensional arrays, such as reading elements, matrix addition, and matrix multiplication. Each program is structured with standard input and output operations, showcasing fundamental programming concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercise-6

1.program to read array elements from keyboard and print


array elements

#include<stdio.h>
int main() {
int i
int marks [6];
printf(“enter array elements”);
for(i=0;i<=5;i++)
{
scanf("%d",&marks[i]);
}
//printing elements of array with loop
for(i=0;i<=5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}

2. program to find sum of array elements


#include <stdio.h>
int main()
{
int arr[100],n,i ,sum = 0;
printf("enter size of array:");
scanf("%d", &n);
printf("enter the elements of array:");
for ( i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for ( i = 0; i < n; i++)
{
sum =sum+ arr[i];
}
printf("%d", sum);
return 0;
}
3. program to find largest element of an array
#include<stdio.h>
int main()
{
int a[5],i,large;
printf("enter array elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
large=a[0];
for(i=1;i<5;i++)
{
if(a[i]>large)
{
large=a[i];
}
}
printf("largest element in array =%d",large);
}

4. program to find smallest element of an array


#include<stdio.h>
int main()
{
int a[5],i,small;
printf("enter array elements");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<5;i++)
{
if(a[i]<small)
{
small=a[i];
}
}
printf("small=%d",small);
}

5. Program to insert an element in array (compile time)


#include<stdio.h>
int main()
{
int i, p, num;
int a[50]={10,20,30,40,50,60,70,80,90,100};
printf("enter position to insert ");
scanf("%d",&p);
printf("enter number to insert");
scanf("%d",&num);
for(i=9;i>=p;i--)
{
a[i+1]=a[i];
}
a[p]=num;
for(i=0;i<11;i++)
{ printf(" %d ",a[i]);
}
}

6. Program to insert element in array (run time)


#include<stdio.h>
int main()
{
int a[5],i,p,num;
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("enter the position");
scanf("%d",&p);
printf("enter the number to be inserted");
scanf("%d",&num);
for(i=4;i>=p;i--)
{
a[i+1]=a[i];
}
a[p]=num;
for(i=0;i<=5;i++)
{
printf("%d",a[i]);
}
}

7. Program to delete an element from an array


#include<stdio.h>
int main()
{
int a[5]={52,64,68,19,22};
int i,position;
printf("enter the position of element to delete:");
scanf("%d",&position);
for(i=position;i<4;i++)
{
a[i]=a[i+1];
}
for(i=0;i<4;i++)
{
printf(" %d ", a[i]);
}
}

8. program to search an element using linear search


#include<stdio.h>
int main()
{
int a[5]={23,56,7,30,26};
int i,key,found=0,position;
printf("enter key value:");
scanf("%d",&key);
for(i=0;i<5;i++)
{
if(key==a[i])
{
found=1;
position=i;
break;
}
}
if(found==1)
printf("key is found in index %d",position);
else

printf("key not found");

}
9. program to find a element from an array using binary search
#include <stdio.h>
int main() {
int i,n, key, pos=0, mid = 0, low = 0;
printf("enter n value");
scanf("%d", &n);
int a[n];
int high=n-1;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("enter key");
scanf("%d", &key);
while (low <=high)
{
mid = (low + high) / 2;
if (key==a[mid])
{
pos = mid;
break;
}
else if (key <a[mid]) {
high = mid-1;
}
else {
low= mid+1;
}
}
printf("element found at position %d", pos);
return 0;
}

10. program to sort elements in ascending order using bubble


sort
#include<stdio.h>
int main()
{
int n,i,j,temp;
printf("enter size of array");
scanf("%d",&n);
int a[n];
printf("enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
} } }
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

11. Program to sort elements of an array using selection sort


#include<stdio.h>
int main()
{
int n,i,j,temp,min;
printf("enter size of array");
scanf("%d",&n);
int a[n];
printf("enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

Exercise-7
12. program to read array elements from keyboard and print
array elements(2 Dimensional array)
#include<stdio.h>
int main()
{
int a[10][10],i,j;
printf("Enter elements into A matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Elements of matrix A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d",a[i][j]);
}
printf("\n");
}
return 0;
}
13. program for matrix addition
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],d[2][2],r,c;
printf("eneter elements of A matrix ");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
scanf("%d",&a[r][c]);
}
}
printf("enter elements of B matrix");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
scanf("%d",&b[r][c]);
}
}
printf("resultant matrix of c:\n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
d[r][c]=a[r][c]+b[r][c];
}
}
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d\t",d[r][c]);
}
printf("\n");
}
}

14. program for matrix multiplication


#include <stdio.h>
int main()
{
int a[2][2],b[2][2],d[2][2],r,c,k;
printf("eneter elements of A matrix ");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
scanf("%d",&a[r][c]);
}
}
printf("enter elements of B matrix");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
scanf("%d",&b[r][c]);
}
}
printf("resultant matrix of c:\n");
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
d[r][c]=0;
for(k=0;k<2;k++)
{
d[r][c]=d[r][c]+a[r][k]*b[k][c];
}
}
}
for(r=0;r<2;r++)
{
for(c=0;c<2;c++)
{
printf("%d\t",d[r][c]);
}
printf("\n");
}
}

You might also like