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

Lab7 3623

The document provides code examples for various array operations in C programming. It includes programs to find the sum and average of array elements, print the square of elements, sort arrays in ascending and descending order, search for an element, and calculate mean, median and mode. Other examples demonstrate how to insert and delete elements, remove duplicates, merge two arrays, and perform arithmetic operations on matrices.

Uploaded by

Mohamed Jasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab7 3623

The document provides code examples for various array operations in C programming. It includes programs to find the sum and average of array elements, print the square of elements, sort arrays in ascending and descending order, search for an element, and calculate mean, median and mode. Other examples demonstrate how to insert and delete elements, remove duplicates, merge two arrays, and perform arithmetic operations on matrices.

Uploaded by

Mohamed Jasim
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Assignment – 7

Arrays
1. Write a C program to find the sum and average of elements in an array.
CODE ;- #include<stdio.h>
int main()
{
int a[5]={1,3,4,5,2},i,n,sum=0;
n=5;
for(i=0;i<n;i++)
{
sum+=a[i];
}
printf("The Sum of elements in the array is %d \n",sum);
printf("The Average of elements in the array is %d \n",(sum/n));

return 0;
}

2. Write a C program to print the square of elements.


CODE ;-
#include<stdio.h>
int main()
{
int a[5]={1,3,4,5,2},i,n, b[5];
n=5;
for(i=0;i<n;i++)
{
b[i]= (a[i]*a[i]);
}
printf("The Square of the elements are \n");
for(i=0;i<n;i++)
{
printf(" %d \n",b[i]);
}

return 0;
}

3. Write a C program to sort the elements in an array in ascending and descending order.
CODE:-
#include<stdio.h>
int main()
{
int a[5],temp;
printf("enter 5 numbers:\n");
for(int i=0;i<5;i++)
scanf("%d",&a[i]);
for(int i=0;i<5;i++)
{
for(int j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("the ascending order of elements:");
for(int i=0;i<5;i++)
printf("%d\t",a[i]);
printf("\n the descending order of elements:");
for(int i=4;i>=0;i--)
printf("%d\t",a[i]);
return 0;
}

4. Write a C program to search for an element in an array


CODE - #include<stdio.h>
int main()
{
int a[5]={1,3,4,5,2},i,n,b;
n=5;
printf("Enter a number to check if it is there in the array");
scanf("%d",&b);
for(i=0;i<n;i++)
{
if(a[i]==b)
printf("The number is there in the array in the index %d position \n",i);
if(a[i]!=b)
printf("The number is NOT there in the array in the index %d position \n",i);
}

return 0;
}

5. Write a C program to find the mean, median, mode of the data in an array.
CODE - #include<stdio.h>
int main()
{
int a[5]={1,3,4,3,1},i,j,n,x,b[5],k,c,mode,max;
n=5;
float sum,mean;
//Mean
for(i=0;i<n;i++)
{
sum+=a[i];
}
mean=(sum/n);
printf("The Mean of elements in the array is %f \n",mean);

//Mode
for (i = 0; i < n - 1; i++)
{
mode = 0;
for (j = i + 1; j < n; j++)
{
if (a[i] == a[j]) {
mode++;
}
}
if ((mode > max) && (mode != 0)) {
k = 0;
max = mode;
b[k] = a[i];
k++;
}
else if (mode == max) {
b[k] = a[i];
k++;
}
}
for (i = 0; i < n; i++)
{
if (a[i] == b[i])
c++;
}
if (c == n)
printf("\nThere is no mode");
else
{
printf("\nMode\t= ");
for (i = 0; i < k; i++)
printf("%d ",b[i]);
}
printf("\n");

//Median
if(n/2==0)
x=n+1/2;
if(n/2!=0)
x=n/2;
printf("The Median of elements in the array is %d",a[x]);

return 0;
}

6. Write a C program to print odd numbers and even numbers in an array and their count.
CODE -
#include <stdio.h>
void main()
{
int n;
printf("Enter number of elements in the array: ");
scanf("%d", &n);

int arr[n];

//Take n elements as input from the user


printf("Enter %d elements in the array: ",n);
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

//Print all the even numbers


printf("Even numbers in the array are: ");
for(int i=0;i<n;i++)
{
if(arr[i]%2==0)
printf("%d ", arr[i]);
}

//print all the odd numbers


printf("\nOdd numbers in the array are: ");
for(int i=0;i<n;i++)
{
if(arr[i]%2==1)
printf("%d ", arr[i]);
}
}

7. Write a C program to find the largest, the smallest, the second largest, the second smallest element
in an array.
CODE -
#include <stdio.h>

int main()
{
int n,small,large,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the array elements :");
int a[n];
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++)
{
int temp;
for(int j=i+1; j<n ;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];

if(a[i]<small)
small=a[i];
}
printf("\nThe smallest element is %d\n",small);
printf("\nThe largest element is %d\n",large);
printf("The second smallest element is %d",a[n-2]);
printf("\n");
printf("The second largest element is %d",a[1]);
return 0;
}
8. Write a C program to insert and delete an element from an array.

CODE -

#include<stdio.h>

int main()

int a[15]={23,4,6,15,22,18,8,12,16,12};

int newele=50, pos=5, i, n=10;

for(i=n-1;i>=pos;i--)

a[i+1]=a[i];

a[pos]=newele;n++;

printf("Array after insertion \n");

for(i=0;i<n;i++)

printf("%d \t",a[i]);

n=11;

for(i = pos;i<n;i++)
{

a[i]=a[i+1];

n--;

printf("\n Array after deletion \n");

for(i=0;i<n;i++)

printf("%d \t",a[i]);

return 0;

9. Write a C program to remove duplicates from an array.


CODE -
#include<stdio.h>
int main()
{
int a[10],temp,j,i,b[10],n=10;
printf("enter 10 numbers:\n");
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Array after removal of duplicates");
j=0;
for(i=0;i<n;i++)
{
if(a[i]!=a[i+1])
{
b[j]=a[i];
j++;
}
}
for(i=0;i<j;i++)
{
printf("\n %d \t ",b[i]);
}
return 0;
}
10 & 12
Write a C program to merge two arrays to create a single array.
CODE - #include<stdio.h>
int main()
{
int i,j,m,n,l;
printf("How many elements u want to have in array 1\n");
scanf("%d",&n);
printf("How many elements u want to have in array 2\n");
scanf("%d",&m);
l=(m+n);
int a[l],b[m];
printf("enter %d numbers:\n",n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter %d numbers:\n",m);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);
j=n;
for(i=0;i<m;i++)
{
a[j]=b[i];
j++;
}
printf("The merged array is:\n");
for(i=0;i<l;i++)
{printf("%d \n",a[i]);}
return 0;
} \n",a[i]);}
return 0;
}
11. Write C program to perform all arithmetic operations on matrices
CODE -
#include<stdio.h>
int main()
{
int a[2][2], b[2][2] , c[2][2],d[2][2];
printf("\n Enter the Elements of Matrix 1 ");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("\n Enter the Elements of Matrix 2 ");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=(a[i][j]+b[i][j]);}
}
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
d[i][j]=(a[i][j]-b[i][j]);}
}
printf("\n The Sum of the Matrices A and B is ");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
printf("%d\t",c[i][j]);
}
printf("\n The Difference of the Matrices A and B is ");
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
printf("%d\t",d[i][j]);
}
int e[30][30];
printf("Product of two matrices is :\n");
for(int i=0;i<2;i++)
{
printf("|");
for(int j=0;j<2;j++)
{
e[i][j]=0;
for(int k=0;k<2;k++)
{
e[i][j]+=a[i][k]*b[k][j];
}
printf("%2d ",e[i][j]);
}
printf("|\n");
}
return 0;

_______________________-----END----_______________________________________

You might also like