0% found this document useful (0 votes)
15 views11 pages

Matrix, String Programs

Programs for c

Uploaded by

shriyasm7
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)
15 views11 pages

Matrix, String Programs

Programs for c

Uploaded by

shriyasm7
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
You are on page 1/ 11

22POP13/23

Program 6 : Design and develop a C program that reads two matrices A (m x n) and B (p x q)
and Compute product of matrices A and B. Read matrix A and matrix B in row major order.
Print both the input matrices and resultant matrix appropriately.

(I) Programming Approach

I. Read matrix A of size mxn


II. Read matrix B of size pxq
III. Check for compatibility (n= = p)
IV. Perform multiplication AXB, resultant matrix size will be m X q Where
m & p are the number of rows
n& q are the number of columns

(II) Algorithm

Step 1: Start

Step 2: Declare variables and initialize necessary variables


Step 3: Enter the element of matrices by row wise using loops

Step 4: Check the number of rows and column of first and second matrices
Step 5: If number of rows of first matrix is equal to the number of columns of second matrix,
go to step 6. Otherwise, print matrix multiplication is not possible and go to step 8.

Step 6: Multiply the matrices using nested loops.

Step 7: Print the product in matrix form as console output.

Step 8: Stop

Dept. of CSE, DSCE Page 28


22POP13/23

(III) Flowchart

Dept. of CSE, DSCE Page 29


22POP13/23

(IV) Program

#include<stdio.h>
int main()
{
int A[10][10],B[10][10],C[10][10],i,j,k,m,n,p,q;
printf("Enter the size of matrix A\n”);

scanf(“%d%d",&m,&n); // read size of matrix a


printf("Enter the size of matrix B\n”);
scanf("%d%d",&p,&q); // read size of matrix b
if(n!=p) // check for compatibility
printf("Matrix multiplication is not possible\n”);
else
{
printf("Enter elements of matrix A\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&A[i][j]); //read matrix A
printf("Enter elements of matrix B\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&B[i][j]); //read matrix B
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
C[i][j]=0;
for(k=0;k<n;k++)
C[i][j] = C[i][j] + A[i][k] * B[k][j] ; // multiply a and b matrix
}
printf(“The resultant matrix C is\n");
for(i=0;i<m;i++)
{

Dept. of CSE, DSCE Page 30


22POP13/23

for(j=0;j<q;j++)

printf("%5d",C[i][j]);
printf("\n");
}
} // end of else
return 0;
}

(V) Output

Run 1

Enter the size of matrix A


2
2
Enter the size of matrix B
2
2
Enter the elements of matrix A
2
4
1
3
Enter the elements of matrix B
4
1
2
3
The resultant matrix C is
16 14
10 10

Dept. of CSE, DSCE Page 31


22POP13/23

Run 2

Enter the size of matrix A


3
2
Enter the size of matrix B
4
3
Matrix multiplication is not possible

(VI) Sample Viva questions

1. What is an Array?
2. What is 2D-Array?

3. How to declare 2D-Array?


4. How to initialize 2D-Array?
5. What is the output of the following code?

int main( )

int a[3][3]={10,20,30,1,2,3,21,22,23};

printf(“%d”,a[2][2]);

Dept. of CSE, DSCE Page 32


22POP13/23

Program 7: Design and develop a C program to implement the following operations without
using library functions. Display the results after every operation.
I. Read STRING S1 = “Dayananda”
II. Read STRING S2 = “Sagar”
III. Output the concatenated string (s1+s2) STIRNG s3 = “DayanandaSagar”

(I) Programming Approach

The program is used to read 2 strings STR1 & STR2 and compute the resulting string STR3
which consists of STR1 and STR2 without using library function. The program includes the
concept of Strings and looping.

(II) Algorithm

Step 1: START

[Input 2 strings STR1 & STR2]

Read STR1, STR2

Step 2: [Initialize i, j and count to Zero]

i = 0; j = 0; count = 0;

Step 3: [Copy the string STR1 to STR3]

while STR1[ i ] != “\0”

BEGIN STR3[ count ] = STR1[ i ]

count ++ [Track length of string STR3]

i++;

END

Step 4: [Increment the value of Count to insert space between 2 strings]

STR3[count++]=‟ „

Step 5: [Copy the string STR2 to STR3]

Dept. of CSE, DSCE Page 33


22POP13/23

While STR2[ j ] != “ \0” BEGIN

STR3[ count ] = STR2[ j ]

count ++

j++;

END

Step 6: [Terminate the String with end of Character]

STR3[count] = „\0‟

Step 7: [Print the String STR1, STR2 and STR3]

Print STR1, STR2, STR3

Step 8: END

Dept. of CSE, DSCE Page 34


22POP13/23

(III) Flow Chart


A
Start

while STR2[j] !=
False
‘\0’
Read STR1,
STR2

tru
e

STR3[count]=STR2[i] j++
count++
while STR1[i] != ‘\0’ False

tru
e

STR3[count]=STR1[i] i++
count++

STR3[count]='\0'

Print STR1,
STR2,
STR3

STR3 coun ++]=

Sto p
A

Dept. of CSE, DSCE Page 35


22POP13/23

(IV) Program

/* The program to Concatenate 2 Strings */

#include<stdio.h>
int main()
{
char STR1[100],STR2[100],STR3[100];
int i=0,j=0,count=0;
//READING OF STRINGS STR1 AND STR2
printf("Enter the String 1\n");
gets(STR1);
printf("Enter the String 2\n");
gets(STR2);

//COPY STRING STR1 TO STR3


while(STR1[i]!='\0')
{
STR3[count]=STR1[i];
count++;
i++;
}

//INSERT BLANK SPACE BETWEEN STR1 AND STR2


STR3[count++]=' ';
//COPY STRING STR2 TO STR3 FROM THE LOCATION COUNT
while(STR2[j]!='\0')
{
STR3[count]=STR2[j];
count++;
j++;
}

Dept. of CSE, DSCE Page 36


22POP13/23

//END OF STRING
STR3[count]='\0';
//PRINTING OF STRINGS STR1, STR2, STR3
printf("\n String STR1 =\t");
puts(STR1);
printf("\n String STR2 =\t");
puts(STR2);
printf("\n String STR3 =\t");
puts(STR3);
return 0;
}

(V) Output

Run1:

Enter the String 1

Dayananda

Enter the String 2

Sagar

String STR1 = Dayananda

String STR2 = Sagar

String STR3 = DayanandaSagar

Run2:

Enter the String 1

Engineering

Enter the String 2

Department

String STR1 = Engineering

Dept. of CSE, DSCE Page 37


22POP13/23

String STR2 = Department

String STR3 = Engineering Department

(VI) Sample Viva Questions

1. What is String?
2. Write C code snippet to find the length of a string without using library function?
3. What is the difference between character constant and strings?
4. List out all the string handling functions.
5. Write a code snippet to reverse a string using the strrev() function.

Dept. of CSE, DSCE Page 38

You might also like