C Programming Assignment 03

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

NAME : TRINETRA BANERJEE

CLASS ROLL : 58 , SEC : G


UNIVERSITY ROLL NO : 10930621054
STREAM : AIML
C PROGRAM ASSIGNMENT 03
Q1 ) Write a c program that will perform Matrix
addition .

• ALGORITHM :
• 1 . Start
• 2 . We first define three matrix .
• 3 . Read the matrix .
• 4 . Start a loop for getting the row elements of first and second matrix .
• 5 . Inside it again start a loop for getting the coloumn of first and second matrix .
• 6 . We store their corrosponding addition in sum[c][d]=first[c][d]+second[c][d]; into sum[c][d] .
• 7 . At the end of the loop the result of addition will be stored in ' Sum ' .
• 8 . Stop .
Start

FLOWCHART :
Declare
a[100],b[100],
sum[100]

Read
a[100],b[100]
,sum[100]

I = I+1 J=j+1
i= 0 , j = 0

No No

i<r J<c Sum[I][j]=a[I][j]+b[I][j]

Yes Yes

Stop Print sum .


C CODE :
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]); }
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j]; }
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
REMARKS :

• There are many other ways also to solve this problem .


Q 2 ) Write a c program that will perform transpose of a matrix .

ALGORITHM :

1 . Start
2 . First declare two matrix of order m×n .
3 . Then we read the first matrix from the user .
4 . We use the second matrix to store the transpose of the matrix .
5 . Declare two variable and initialize them to 0 .
6 . After this we start a loop of i till it reaches n
which gave us the column indexes and inside it a
loop of j which gives us the elements of the row.
7 . Stop .
Start

FLOWCHART :

Declare a[10][10]transpose[10][10]

Read a[10][10]transpose[10][10]

Declare
variable I , j

I=0;
J=0.

Yes Yes
No
Stop Print the I<r J<c Transpose[I][j]=a[I][j]
matrix
#include <stdio.h>
C CODE :
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for ( i = 0; i < r; ++i){
for ( j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j +
1);
scanf("%d", &a[i][j]);
}
}
printf("\nEntered matrix: \n");
for ( i = 0; i < r; ++i){
for ( j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
}
for ( i = 0; i < r; ++i) {
for ( j = 0; j < c; ++j) { transpose[i][j] =
a[i][j];
}
}
printf("\nTranspose of the matrix:\n");
for ( i = 0; i < c; ++i) {
for ( j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
}
REMARKS :

● There are many other ways also to solve this problem .


Q 3 ) Write a c program that will perform matrix multiplication .

ALGORITHM :

1 . Start
2 . Enter the value of m and n order of first matrix .
3 . Enter the value of p and q order of second matrix .
4 . Create a matrix of size a[m][n] and b[p][q].
5 . Enter the element of matrices row wise using loops.
6 . If number of columns of first matrix is not equal to the number of rows of second matrix, print matrix
multiplication is not possible and exit. If not, proceed to next step.
7 . Create a third matrix, c of size m x q to store the product.
8 . Set a loop from i=0 to i=m.
9 . Set an inner loop for the above loop from j=0 to j=q.
10 .Initilize the value of element (i, j) of new matrix to 0.
11 .Set an inner loop inside the above loop from k=0 to k=p.
12 . Using the add and assign operator (+=) store the value of a[i][k] * b[k][j] in the third matrix, c[i][j].
13 . Print the third matrix.
14 . Stop.
C CODE :
#include<stdio.h>
int main(){
int a[10][10],b[10][10],c[10][10],n,i,j,k;
printf("Enter the value of N (N <= 10): ");
scanf("%d",&n);
printf("Enter the elements of Matrix-A: \n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of Matrix-B: \n " );
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
c[i][j]=0;
for(k=0;k<n;k++) {
c[i][j]=a[i][k]*b[k][j];
}
}
}
printf("The product of the two matrices is: \n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
printf("%d\n",c[i][j]);
}
printf("\n");
}
return 0;
}
REMARKS :

● There are many other ways also to solve this problem .


Q 6 ) Write a c program that will perform ascending and descending order sorting of number
using function .

ALGORITHM :
1 . Start
2 . First print the array size .
3 . Use nested loop for comparing other values .
4 . Use temporary variable for storing the replacing value .
5 . Use loop for printing array data after sorting .
6 . Compare other array elements .
7 . Print the data
8 . Return zero status to the system .
9 . Stop .
#include <stdio.h>
#include<conio.h>
C CODE :
int main(){
int a[100],n,i,j;
printf("Array size: ");
scanf("%d",&n);
printf("Elements: ");
for(i=0;i<n;i++) {
scanf("%d",&a[i]);
}
for ( i = 0; i < n; i++) {
for ( j = 0; j < n; j++) {
if (a[j] > a[i]) {
int tmp = a[i] ;
a[i] = a[j];
a[j] = tmp;
}
}
}
printf("\n\nAscending : ");
for ( i = 0; i < n; i++) {
printf(" %d ", a[i]);
}
for ( i = 0; i < n; i++) {
if (a[j] < a[i]){
int tmp = a[i];
a[j] = tmp;
}
}
}
printf("\n\nDescending : ");
for ( i = 0; i < n; i++) {
printf(" %d ", a[i]);
}
return 0;
}
Remarks :

• There are many other ways also to solve this problem .


Q 7 ) Write a c program that will identify duplicate / redundant
numbers .

• ALGORITHM :
• 1 . Start
• 2 . Initialize array .
• 3 . Print the size of array .
• 4 . Set i = 0 until i<no .
• 5 . Set j = i + 1 until j<no .
• 6 . If ( arr[I] = arr[j] ) then print arr[j] .
• 7 . Return 0 to the system .
• 8 . Stop .
C CODE :

#include<stdio.h>
#include<conio.h>
void main() {
int i,arr[20],j,no;
printf("Enter size of array: ");
scanf("%d",&no);
printf("Enter any %d elements in array: ",no);
for(i=0;i<no;i++) {
scanf("%d",&arr[i]);
}
printf("Duplicate elements are: ");
for(i=0; i<no; i++) {
for(j=i+1;j<no;j++) {
if(arr[i]==arr[j]) {
printf("%d\n",arr[j]);
}
}
}
getch();
}
REMARKS :

• There are many other ways also to solve this problem .


Q 8 ) Write a c program that will perform searching of a number ( in a
single dimensional array / multidimensional array ) .

• ALGORITHM :
• 1. Start
• 2 . Enter the size array from 1 to 100 .
• 3 . Use loop , set i = 0 until i<n .
• 4 . If a[I] = element then print the number and its position .
• 5 . Else if the element is not found then return zero to the system .
• 6 . Stop .
C CODE :

#include<stdio.h>
int main() {
int a[100], n, element, pos=0;
int i;
printf("Enter array size [1-100]: ");
scanf("%d", &n);
printf("Enter array elements: ");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
printf("Enter element to search: ");
scanf("%d",&element);
}
for(i=0; i<n; i++) {
if(a[i]==element) {
printf("%d found at position %d", element, i+1);
return 0;
}
}
printf("%d not found.", element);
return 0;
}
• REMARKS :

There are many other ways also to solve this problem .

You might also like