0% found this document useful (0 votes)
4 views25 pages

ICE-103 - Assignment 3 (Homework)

The document contains a series of C programming exercises focused on array manipulation, including reading, printing, summing elements, finding largest values, inserting and deleting elements, reversing arrays, and performing operations on matrices. Each exercise is accompanied by a sample code solution. The assignment is managed by instructor Sajeeb Saha and was completed by student Tahsin Farhan.

Uploaded by

tahsinfarhanar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views25 pages

ICE-103 - Assignment 3 (Homework)

The document contains a series of C programming exercises focused on array manipulation, including reading, printing, summing elements, finding largest values, inserting and deleting elements, reversing arrays, and performing operations on matrices. Each exercise is accompanied by a sample code solution. The assignment is managed by instructor Sajeeb Saha and was completed by student Tahsin Farhan.

Uploaded by

tahsinfarhanar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ICE-103 Structured Programming Instructor :Sajeeb Saha

ICE
ASSIGNMENT-
3

TIMING Nov 23, 2025 to Nov 30, 2025

STATUS COMPLETED

OWNER Tahsin Farhan


Exercise 1:
Write a C program to read elements in an array and print
array:

Ans: #include<stdio.h>
int main()
{
int i,j;
int a[10];
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<10;i++)
{
printf("%d ",a[i]);
}
return 0;
}

Exercise 2
Write a C program to read elements in an array and find the
sum of all elements of the array.

Ans: #include<stdio.h>
int main()
{
int i,j,sum=0;
int a[10];
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for (i=0;i<10;i++)
{
sum+=a[i];
}
printf ("Sum is %d",sum);
return 0;
}

Exercise 3
Write a C program to find the largest and second largest
element in an array.

Ans: #include<stdio.h>
int main()
{
int i,j,largest=0,secondlargest=0;
int a[10];
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
largest=a[0];
for (i=0;i<10;i++)
{
if(largest<a[i])
{
largest=a[i];
}
}
printf("%d is largest\n",largest);
for(i=0;i<10;i++)
{
if(secondlargest<a[i]&&a[i]!=largest)
{
secondlargest=a[i];
}
}
printf("%d is second largest",secondlargest);
return 0;
}

Exercise 4
Write a C program to insert an element in array at specified
position.

Ans:
#include<stdio.h>
int main()
{
int i,j,pos=0,val=0;
int a[11];
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the position and value=");
scanf("%d %d",&pos,&val);
for (i=9;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
for (i=0;i<=10;i++)
{
printf("%d ",a[i]);
}

return 0;
}

Exercise 5
Write a C program to delete an element from an array at
specified position. How to
remove an element from array at given position.

Ans:
#include<stdio.h>
int main()
{
int i,j,pos;
int a[10];
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the position=");
scanf("%d",&pos);
for (i=pos;i<10;i++)
{
a[i]=a[i+1];
}

for (i=0;i<9;i++)
{
printf("%d ",a[i]);
}

return 0;
}

Exercise 6
Write a C program to read elements in an array and find its
reverse.

Ans:
#include<stdio.h>
int main()
{
int i,j,temp=0;
int a[10];
for (i=0;i<10;i++)
{
scanf("%d",&a[i]);
}

for (i=9;i>=0;i--)
{
printf("%d ",a[i]);
}

return 0;
}

Exercise 7
Write a program in C to count the total number of duplicate
digits in a number using an array.

Ans:
#include<stdio.h>
int main()
{
int i,j,num,count=0,digit;
int a[10]={0};
scanf("%d",&num);
while (num!=0)
{
digit=num%10;
a[digit]++;
num=num/10;
}

for (i=0;i<10;i++)
{
if(a[i]>1)
{
count++;
}
}
printf("Duplicate value is %d",count);

return 0;
}

Exercise 8
Write a program in C to print all unique digits in a number
using an array.

And: #include <stdio.h>

int main() {
int num, digit;
int a[10] = {0};

printf("Enter a number: ");


scanf("%d", &num);
while (num > 0) {
digit = num % 10;
a[digit]++;
num /= 10;
}

printf("Unique digits: ");


for (int i = 0; i < 10; i++) {
if (a[i] == 1) {
printf("%d ", i);
}
}

return 0;
}

Exercise 9:
Write a program in C for a 2D array of size 3x3 and print the
matrix.

Ans:
#include<stdio.h>
int main()
{
int A[10][10],i,j,row,col;
printf("Enter the value of row and col\n");
scanf("%d %d",&row,&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("A[%d][%d] =",i,j);
scanf("%d",&A[i][j]);
}
}
printf("A matrix is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}

Exercise 10:
Write a program in C for adding two matrices of the same
size.

Ans:
#include<stdio.h>
int main()
{
int A[10][10],B[10][10],result[10][10],i,j,r1,c1,r2,c2;
printf("Enter the value of A matrix row and col\n");
scanf("%d %d",&r1,&c1);
printf("Enter the value of B matrix row and col\n");
scanf("%d %d",&r2,&c2);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("A[%d][%d] =",i,j);
scanf("%d",&A[i][j]);
}
}
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("B[%d][%d] =",i,j);
scanf("%d",&B[i][j]);
}
}
for (i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
result[i][j]=A[i][j]+B[i][j];
}
}

printf("A matrix is\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("B matrix is\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
printf("Result matrix is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",result[i][j]);
}
printf("\n");
}
}

Exercise 11:
Write a program in C for the multiplication of two square
matrices.

Ans:
#include<stdio.h>
int main()
{
int A[10][10],B[10][10],result[10][10],multiply=0,k,i,j,r1,c1,r2,c2;
printf("Enter the value of A row and col\n");
scanf("%d %d",&r1,&c1);
printf("Enter the value of B row and col\n");
scanf("%d %d",&r2,&c2);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("A[%d][%d] =",i,j);
scanf("%d",&A[i][j]);
}
}
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("B[%d][%d] =",i,j);
scanf("%d",&B[i][j]);
}
}
for (i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
multiply+=A[i][k]*B[k][j];
}
result[i][j]=multiply;
multiply=0;
}
}

printf("A matrix is\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
printf("B matrix is\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
printf("Result matrix is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d ",result[i][j]);
}
printf("\n");
}

}
Exercise 12:
Write a program in C to find the transpose of a given matrix.

Ans:
#include<stdio.h>
int main()
{
int A[10][10],trans[10][10],i,j,row,col;
printf("Enter the value of row and col\n");
scanf("%d %d",&row,&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("A[%d][%d] =",i,j);
scanf("%d",&A[i][j]);
}
}
printf("A matrix is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
trans[i][j]=A[j][i];
}
}
printf("The transpose matrix is\n");
for(i=0;i<col;i++)
{
for(j=0;j<row;j++)
{
printf("%d ",trans[i][j]);
}
printf("\n");
}

Exercise 13:
Write a program in C to find the sum of the right diagonals
of a matrix.

Ans:
#include<stdio.h>
int main()
{
int i,j,A[10][10],row,col,sum=0;
printf("Enter the value of row and col\n");
scanf("%d %d",&row,&col);
printf("Enter the materials of A matrix\n");
for (i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("A[%d][%d]=",i,j);
scanf("%d",&A[i][j]);
}
}
for (i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
for (i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i+j==row-1)
sum+=A[i][j];
}
}
printf("The sum of right diagonal is= %d\n",sum);
return 0;
}

Exercise 14:
Write a program in C to check whether a given matrix is an identity
matrix.

Ans:
#include<stdio.h>
int main()
{
int A[10][10],I[10][10],i,j,row,col,flag=0;
printf("Enter the value of row and col\n");
scanf("%d %d",&row,&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("A[%d][%d] =",i,j);
scanf("%d",&A[i][j]);
}
}
printf("A matrix is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
while(((A[i][j]!=1)&&(i==j))||((A[i][j]!=0)&&(i!=j)))
{
flag=1;
break;
}

}
if(flag==1)
printf("This is not identity matrix\n");
else
printf("This is identity matrix");
return 0;
}

Exercise 15:
Write a program in C to search for an element in a 2D
matrix.

Ans:
#include<stdio.h>
int main()
{
int A[10][10],i,j,row,col,pos,flag=0,fi,fj;
printf("Enter the value of row and col\n");
scanf("%d %d",&row,&col);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("A[%d][%d] =",i,j);
scanf("%d",&A[i][j]);
}
}
printf("Enter the position\n");
scanf("%d",&pos);
printf("A matrix is\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("A[%d][%d]=%d ",i,j,A[i][j]);
}
printf("\n");
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
if(A[i][j]==pos)
{
fi=i;
fj=j;
flag=1;
break;
}
}

}
if(flag==1)
{
printf("Value found at A[%d][%d]",fi,fj);
}
else
printf("Value not found\n");
return 0;

You might also like