Lab7 3623
Lab7 3623
Lab7 3623
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;
}
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;
}
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];
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};
for(i=n-1;i>=pos;i--)
a[i+1]=a[i];
a[pos]=newele;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--;
for(i=0;i<n;i++)
printf("%d \t",a[i]);
return 0;
_______________________-----END----_______________________________________