H12 FCP Assignement 5
H12 FCP Assignement 5
1)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,num1,sum;
float avg;
printf("enter the size of arrays u want to make\n");
scanf("%d",&size);
sum=0;
int ar1[size],ar2[size],ar3[size];
printf("enter elemts for first array\n");
for(num1=0;num1<size;num1++)
{
scanf("%d",&ar1[num1]);
}
printf("enter the elemnts of the second array\n");
for(num1=0;num1<size;num1++)
{
scanf("%d",&ar2[num1]);
}
for(num1=0;num1<size;num1++)
{
ar3[num1]=ar1[num1]+ar2[num1];
}
printf("the elmemnts of the resulting array are:\n");
for(num1=0;num1<size;num1++)
{
printf("%d ",ar3[num1]);
sum=sum+ar3[num1];
}
avg=sum/size;
printf("the average of elemts in resultant array is %f",avg);
return 0;
}
2)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,c,d,store1,store2,swap;
printf("enter hte size of the array\n");
scanf("%d",&size);
int ary[size];
printf("enter the elements of the array\n");
for(c=0;c<size;c++)
{
scanf("%d",&ary[c]);
}
store2=0;
c=store2;
do
{
store1=0;
for(d=store2;d<size;d++)
{
if(ary[c]>=ary[d])
{
store1++;
}
else
{
continue;
}
}
if(store1==(size-store2))
{
swap=ary[c];
ary[c]=ary[store2];
ary[store2]=swap;
//printf("%d ",ary[store2]);to include printing inside loop//
store2++;
c=store2;
}
else
{
c++;
}
}while(c<size);
for(d=0;d<size;d++)
{
printf("%d ",ary[d]);//to check correctedness of code//
}
return 0;
}
3)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,oddn,evenn,c;
printf("enter the size of the array\n");
scanf("%d",&size);
int ary[size];
oddn=0;
evenn=0;
printf("enter the elements of the array\n");
for(c=0;c<size;c++)
{
scanf("%d",&ary[c]);
if(ary[c]%2==0)
{
evenn++;
}
else
{
oddn++;
}
}
printf("number of even numbers : %d\nnumber of odd numbers : %d",evenn,oddn);
return 0;
}
4)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,c,d,lgst,smlst,store,swap,store2;
printf("enter the size of the array\n");
scanf("%d",&size);
int ary[size];
printf("enter the elements of the array\n");
for(c=0;c<size;c++)
{
scanf("%d",&ary[c]);
}
c=0;
do
{
store=0;
store2=0;
for(d=0;d<size;d++)
{
if(ary[c]!=ary[d])
{
if(ary[c]>ary[d])
{
store++;
}
}
else
{
store2++;
}
}
if((store+store2)==size)
{
lgst=c;
}
else if(store==0)
{
smlst=c;
}
c++;
}while(c<size);
swap=ary[lgst];
ary[lgst]=ary[smlst];
ary[smlst]=swap;
for(c=0;c<size;c++)//to check correctedness//
{
printf("%d ",ary[c]);
}
return 0;
}
//if there r two largest or smallest numbers the swapping cannot be controlled//
5)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,c,killpos;
printf("enter the size of the array\n");
scanf("%d",&size);
int ary[size];
printf("enter the elements of the array\n");
for(c=0;c<size;c++)
{
scanf("%d",&ary[c]);
}
printf("enter the position of element you want to delete\n");
scanf("%d",&killpos);
killpos--;
if(killpos<size)
{
for(c=0;c<(size-1);c++)
{
if(c<killpos)
{
printf("%d ",ary[c]);
}
else
{
ary[c]=ary[c+1];
printf("%d ",ary[c]);
}
}
}
else
{
printf("imvalid position");
}
return 0;
}
6)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,c,npos,nelm,store1,store2;
printf("enter the size of the array\n");
scanf("%d",&size);
int ary[size+1];
printf("enter the elements of the array\n");
for(c=0;c<size;c++)
{
scanf("%d",&ary[c]);
}
ary[size+1]=0;
printf("enter the position number from start and element u want to add in the
array\n");
scanf("%d%d",&npos,&nelm);
npos--;
if(npos>=0 && npos<=(size+1))
{
c=0;
do
{
if(c<npos)
{
}
else if(c==npos)
{
store1=ary[npos];
}
else
{
store2=ary[c];
ary[c]=store1;
store1=store2;
}
c++;
}while(c<(size+1));
ary[npos]=nelm;
for(c=0;c<(size+1);c++)
{
printf("%d ",ary[c]);
}
}
else
{
printf("invalid position\n");
}
return 0;
}
7)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int size,d,c,modsum;
printf("enter the size of the array\n");
scanf("%d",&size);
int ary[size];
printf("enter the elements of the array\n");
for(c=0;c<size;c++)
{
scanf("%d",&ary[c]);
}
modsum=0;
for(c=0;c<size;c++)
{
for(d=0;d<size;d++)
{
modsum+=(ary[d]%ary[c]);
}
}
printf("%d",modsum);
return 0;
}
8)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int row,c,d,e;
printf("enter the rows and column number of a matrix\n");
scanf("%d",&row);
int m1[row][row],m2[row][row];
for(d=0;d<row;d++)
{
printf("enter the elements of the %d row from left to right\n",(d+1));
for(c=0;c<row;c++)
{
scanf("%d",&m1[d][c]);
}
}
printf("enter another matrix of %d rows by %d columns\n",row,row);
for(d=0;d<row;d++)
{
printf("enter the elements of the %d row from left to right\n",(d+1));
for(c=0;c<row;c++)
{
scanf("%d",&m2[d][c]);
}
}
int m3[row][row];
printf("resultant matrix:\n");
for(c=0;c<row;c++)
{
for(d=0;d<row;d++)
{
m3[c][d]=0;
e=0;
do
{
m3[c][d]+=m1[c][e]*m2[e][d];
e++;
}while(e<row);
printf("%d ",m3[c][d]);
}
printf("\n");
}
return 0;
}
9)
#include<stdio.h>
#include<stdlib.h>
int sidejoincheck(int store1,int store2);
int main()
{
int size,e,f,store1,store2,store3;
printf("enter the size of the array\n");
scanf("%d",&size);
int ary[size];
printf("enter the elements of the array\n");
for(e=0;e<size;e++)
{
scanf("%d",&ary[e]);
}
store3=0;
e=0;
f=size-1;
do
{
store1=ary[e];
store2=ary[f];
if((sidejoincheck(store1,store2))==0)
{
}
else
{
store3++;
}
e++;
f--;
}while(e<f);
if(store3==0)
{
printf("the array elements are in palindrom oreder\n");
}
else
{
printf("the array elementd are not in palindrom order\n");
}
return 0;
}
}
else
{
check++;
}
c++;
d--;
}while(c<d);
return check;
}
10)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m,n,c;
printf("enter first array size\n");
scanf("%d",&m);
int ary1[m];
printf("enter the elements of this array\n");
for(c=0;c<m;c++)
{
scanf("%d",&ary1[c]);
}
printf("enter second array size\n");
scanf("%d",&n);
int ary2[n];
printf("enter the elements of this array\n");
for(c=0;c<n;c++)
{
scanf("%d",&ary2[c]);
}
int ary3[m+n];
for(c=0;c<(m+n);c++)
{
if(c<m)
{
ary3[c]=ary1[c];
}
else
{
ary3[c]=ary2[c-m];
}
}
printf("now here is the new array sorted in descending order:\n");
int store1,store2,swap,d,size;
store2=0;
c=store2;
size=m+n;
do
{
store1=0;
for(d=store2;d<size;d++)
{
if(ary3[c]>=ary3[d])
{
store1++;
}
else
{
continue;
}
}
if(store1==(size-store2))
{
swap=ary3[c];
ary3[c]=ary3[store2];
ary3[store2]=swap;
printf("%d ",ary3[store2]);
store2++;
c=store2;
}
else
{
c++;
}
}while(c<size);
return 0;
}
11)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int row,column,c,d;
printf("enter row and column number for a two dimentional array\n");
scanf("%d%d",&row,&column);
int ary1[row][column],ary2[row][column];
printf("enter the elements for the first 2D array\n");
for(c=0;c<row;c++)
{
printf("enter elements of %d row from left to right\n",c+1);
for(d=0;d<column;d++)
{
scanf("%d",&ary1[c][d]);
}
}
printf("enter the elements for the second 2D array\n");
for(c=0;c<row;c++)
{
printf("enter elements of %d row from left to right\n",c+1);
for(d=0;d<column;d++)
{
scanf("%d",&ary2[c][d]);
}
}
int ary3[row][column];
printf("the subtraction array of these two arrays:\n");
for(c=0;c<row;c++)
{
for(d=0;d<column;d++)
{
ary3[c][d]=ary2[c][d]-ary1[c][d];
printf("%d ",ary3[c][d]);
}
printf("\n");
}
return 0;
}