0% found this document useful (0 votes)
39 views12 pages

Day 4

The document contains 6 programming problems involving arrays, matrices, strings, and other basic data structures in C. The problems cover tasks like reversing arrays, removing duplicate elements, matrix operations, calculating standard deviation, transposing matrices, and analyzing/reversing strings. Menu-driven programs and functions are used to modularize the code. Basic loops and conditionals are used to iterate through arrays/matrices and implement the required logic.

Uploaded by

meghnabagchi23
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)
39 views12 pages

Day 4

The document contains 6 programming problems involving arrays, matrices, strings, and other basic data structures in C. The problems cover tasks like reversing arrays, removing duplicate elements, matrix operations, calculating standard deviation, transposing matrices, and analyzing/reversing strings. Menu-driven programs and functions are used to modularize the code. Basic loops and conditionals are used to iterate through arrays/matrices and implement the required logic.

Uploaded by

meghnabagchi23
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/ 12

1.

Write a program in C to reverse the content of the elements of


an integer array. Take integer array from user.

#include<stdio.h>

#include<conio.h>

int main()

int size,i,temp;

printf("Enter the size of array=\n");

scanf("%d",&size);

int arr[size];

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

printf("Enter the %d element\t",i+1);

scanf("%d",&arr[i]);

int rev[size],j=0;

for(i=size-1;i>=0;i--)

rev[j]=arr[i];

j++;

printf("Array after reversing \n");

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

printf("%d\n",rev[i]);

}
2. Write a program in C to read n number of values in an array.
After that display the total number of duplicate elements in
the array. Then copy the elements except the duplicate
elements of that array into another array and display this
array in reverse order.

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,count=0,k;
printf("Enter size of array=\n");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter the %d element\t",i+1);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
count++;
for(k=j;k<n-1;k++)
{
arr[k]=arr[k+1];
}
n--;
j--;

}
}
}
printf("Number of duplicates=%d\n",count);
int rev[n];
j=0;
for(i=n-1;i>=0;i--)
{
rev[j]=arr[i];
j++;
}
printf("Array after reversing \n");
for(i=0;i<n;i++)
{
printf("%d\n",rev[i]);
}
}
3. Write a menu driven program for accepting values in two
square matrices of 3x3 dimension and generate their sum,
difference and product.

#include<conio.h>
#include<stdio.h>
void sum(int ar[3][3],int br[3][3])
{
int cr[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cr[i][j]=ar[i][j]+br[i][j];

}
printf("\n SUM OF MATRICES\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d\t",cr[i][j]);
printf("\n");
}
}
void diff(int ar[3][3],int br[3][3])
{
int cr[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cr[i][j]=ar[i][j]-br[i][j];

}
printf("\n DIFFERENCE OF MATRICES\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d\t",cr[i][j]);
printf("\n");
}
}
void mult(int ar[3][3],int br[3][3])
{
int cr[3][3],i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cr[i][j]=0;
for(k=0;k<3;k++)
{
cr[i][j]+=ar[i][k]+br[k][j];
}
}
}
printf("\n MULTIPLICATION OF MATRICES\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("\t%d\t",cr[i][j]);
printf("\n");
}

int main()
{
int m1[3][3],m2[3][3];
int ch,cont,i,j;
printf("Enter the elements of first 3*3 matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element [%d][%d]:",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
printf("Enter the elements of second 3*3 matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter element [%d][%d]:",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
do
{
printf("1. Find sum of matrices\n");
printf("2.Find difference of matrices\n");
printf("3.Find product of matrices\n");
printf("Enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: sum(m1,m2);
break;
case 2: diff(m1,m2);
break;
case 3:mult(m1,m2);
break;
default: printf("Wrong choice");
}
printf("Do you want to continue(1/0)");
scanf("%d",&cont);

}while(cont!=0);

}
4. Write a C program which takes some numbers and computes
the standard deviation of them using formula:
x̄ = ( Σ xi ) / n. sS √[Σ(x-x̄ )²/n].

#include<stdio.h>
#include<math.h>
float sdev(int arr[],int size)
{
int i=0;
float dev=0,avg=0;
for(i=0;i<size;i++)
avg+=arr[i];
avg=avg/size;
for(i=0;i<size;i++)
dev+=(arr[i]-avg)*(arr[i]-avg);
dev/=size;
float stdev=sqrt(dev);
return stdev;
}
int main()
{
int size,i;
printf("\n Enter number of values: ");
scanf("%d",&size);
int arr[size];
for(i=0;i<size;i++)
{
printf("%d element:\t",i+1);
scanf("%d",&arr[i]);
}
printf("\n The standard deviation of the given values is:%0.2f",sdev(arr,size));
}
5. Write a C program which accepts a matrix and prints its
transpose.

#include<stdio.h>
#include<conio.h>
int main()
{
int row,col,i,j;
printf("Enter number of rows of matrix:\n");
scanf("%d",&row);
printf("Enter number of columns of matrix:\n");
scanf("%d",&col);
int m[row][col],tr[row][col];
printf("Enter the elements of matrix:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("Enter element [%d][%d]:",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
tr[i][j]=m[j][i];
}
}
printf("The transpose of matrix is:\n");
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
printf("\t%d\t",tr[i][j]);
}
printf("\n");
}
}
6. Write a C program which accepts a string from user
(i) Counts the number of characters in string
(ii) Counts the total number of words in string
(iii) Prints the reverse of string

#include<stdio.h>
#include<conio.h>

int main()
{
char str[100];
int i=0,count=0,len;
printf("Enter string:\n");
scanf("%[^\n]%*c",&str);
while(str[i]!='\0')
{
count++;
i++;
}
printf("Number of characters:%d\n",count);
len=count;
i=0,count=0;
while(str[i]!='\0')
{
if(str[i]==' ')
{
count++;
}
i++;
}
printf("Number of words:%d\n",count+1);
i=0;
printf("Reverse string:\n");
for(i=len-1;i>=0;i--)
{
printf("%c",str[i]);
}

You might also like