This assignment has six objectives: to use the command line arguments to open a
ID: 3801224 • Letter: T
Question
This assignment has six objectives: to use the command line arguments to open a file, input all data from the file, and close the file use double loop to add two matrices use triple loop to multiply two matrices, one is and the other is to produce formatted output Description Given two integer matrices, you need to perform two matrix operations, addition (A + B) and multiplication (C times D). The addition requires both matrices have the same number of rows (N) and columns (M), the multiplication requires the number of rows (P) of matrix C equals to the number of columns of matrix D and the number of columns (Q) of matrix C equals to the number of rows of matrix D. All data are stored in a file and the format of the file as follow: the first line has two numbers, N and M, where N is the number of rows and M is the number of columns of both A and B the next N lines each has M elements for one of the rows of A the next N lines each has M elements for one of the rows of B the next line has two numbers, P and Q, where P is the number of rows of C (also number of columns of D) and Q is the number of columns of C (also the number of rows of D) the next P lines each has Q elements for one of the rows of C the next Q lines each line has P elements for one of the rows of D Example: 3 4 2 3 1 5 3 1 2 4 1 2 3 4 6 5 4 3 3 3 2 3 7 5 4 3 4 3 2 3 4 3 4 5 4 5 6 1 2 3 3 4 5 6 4 5 6 7 4 3 2 1 Means both matrices A and B are 3 times 4 matrix, C is 4 times 3 matrix, and D is 4 times 3 matrix. Matrix A has three rows, 2315, 3124, and 3124. Matrix B has three rows, 6543, 3323, 7543, Matrix C has four rows, 234, 345, 456, 123. Matrix D has three rows, 3456, 4567, 4321 Instructions You need to open the data file (example file name Matrix.txt), input the numbers of rows and columns and then input the rest elements of both matrices A and B. Continue input the numbers of rows and columns and then input the rest elements for matrices C and D. Output the result of your addition and multiplication. You may define your matrices either using larger dimension (say 30) or using dynamic memory allocation. The output of your result should has the followings. The result of addition: 8 8 5 8 6 4 4 7 8 7 7 7 The result of multiplication: 34 35 36 37 45 47 49 51 56 59 62 65Explanation / Answer
//include basic header files
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
clrscr(); // funtion to clear screen everytime you run the programme
FILE *file= fopen("Array.txt","w"); //opening a file named as Array.txt in read and write mode
int **matrixA; // defining dynamic 2-D array
int **matrixB;
int **add;
int **matrixP;
int **matrixQ;
int **mul;
int rowsA, cloumnA, rowsB, columnB, i, j, k, sum=0;
//Creating Matrix A
printf(" Enter the number of rows and columns in Matrix A:");
scanf("%d%d",&rowsA, &columnA);
fprintf(file,"%d %d ",rowsA,columnA); //storing the number of rows and columns of Matrix A in Array.txt
matrixA= (int*)malloc(rowsA*sizeof(int)); //dynamically assigning no of rows to Matrix A
for(i=0;i<rowsA;i++)
{
matrixA[i]= (int*)malloc(columnA*sizeof(int)); //dynamically assigning no of columns to each row in Matrix A
{
for(j=0;j<columnA;j++)
{
printf(" Enter value for A[%d][%d]:", i,j);
scanf("%d",&matrixA[i][j]);
fprintf(file,"%d ",matrixA[i][j]); //storing the values in Array.txt
}
fputs(" ",file); //entering a new line after each row
}
//Creating Matrix B
printf(" Enter the number of rows and columns in Matrix B:");
scanf("%d%d",&rowsB,&columnB);
fprintf(file,"%d %d ",rowsB,columnB); //storing the number of rows and columns in Matrix B into Array.txt
matrixB= (int*)malloc(rowsB*sizeof(int)); //dynamically assigning no of rows to Matrix B
for(i=0;i<rowsB;i++)
{
matrixB[i]= (int*)malloc(columnB*sizeof(int)); //dynamicaaly assigning no of columns to Matrix B
for(j=0;j<columnB;j++)
{
printf(" Enter value for B[%d][%d]:",i,j);
scanf("%d",&matrixB[i][j]);
fprintf(file," %d ",matrixB[i][j]); //storing values of Matrix B into Array.txt
}
fputs(" ",file);
}
//Adding Matrix A and Matrix B
if((rowsA==rowsB)&&(columnA==columnB))
{
add= (int*)malloc(rowsB*sizeof(int));
for(i=0;i<rowsA;i++)
{
add[i]= (int*)malloc(columnA*sizeof(int));
for(j=0;j<columnB;j++)
{
add[i][j]=matrixA[i][j]+matrixB[i][j];
}
}
}
else
printf(" Matrix A and Matrix B cant be added");
//U can use any value rowsA or rowsB and columnA or columnB since they are equal
//Displaying the resultant Add Matrix
for(i=0;i<rowsA;i++)
{
for(j=0;j<columnB;j++)
{
printf("%d" ",add[i][j]);
}
printf(" ");
}
//Creating Matrix P
printf(" Enter the number of rows and columns in Matrix P:");
scanf("%d%d",&rowsP, &columnP);
fprintf(file,"%d %d ",rowsP,columnP); //storing the number of rows and columns of Matrix P in Array.txt
matrixP= (int*)malloc(rowsP*sizeof(int)); //dynamically assigning no of rows to Matrix P
for(i=0;i<rowsP;i++)
{
matrixP[i]= (int*)malloc(columnP*sizeof(int)); //dynamically assigning no of columns to each row in Matrix P
{
for(j=0;j<columnP;j++)
{
printf(" Enter value for P[%d][%d]:", i,j);
scanf("%d",&matrixP[i][j]);
fprintf(file,"%d ",matrixP[i][j]); //storing the values in Array.txt
}
fputs(" ",file); //entering a new line after each row
}
//Creating Matrix Q
printf(" Enter the number of rows and columns in Matrix Q:");
scanf("%d%d",&rowsQ, &columnQ);
fprintf(file,"%d %d ",rowsQ,columnQ); //storing the number of rows and columns of Matrix Q in Array.txt
matrixQ= (int*)malloc(rowsQ*sizeof(int)); //dynamically assigning no of rows to Matrix Q
for(i=0;i<rowsQ;i++)
{
matrixQ[i]= (int*)malloc(columnQ*sizeof(int)); //dynamically assigning no of columns to each row in Matrix Q
{
for(j=0;j<columnQ;j++)
{
printf(" Enter value for Q[%d][%d]:", i,j);
scanf("%d",&matrixQ[i][j]);
fprintf(file,"%d ",matrixQ[i][j]); //storing the values in Array.txt
}
fputs(" ",file); //entering a new line after each row
}
//Multiplying Matrix P and Matrix Q
if(columnP==rowsQ)
{
mul= (int*)malloc(rowsP*sizeof(int)); //dynamically assigning number of rows to Matrix mul
for(i=0;i<rowsP;i++)
{
mul[i]= (int*)malloc(columnQ*sizeof(int)); //dynamically assigning number of columns to Matrix mul
{
for(j=0;j<columnQ;j++)
{
for(K=0;k<columnP;k++)
{
sum=sum+matrixP[i][k]*matrixQ[k][j];
}
mul[i]j]=sum;
sum=0;
}
}
}
}
else
printf(" Matrix P and Matrix Q cannot be multiplied");
//Displaying the resultant mul Matrix
for(i=0;i<rowsP;i++)
{
for(j=0;j<columnQ;j++)
{
printf("%d ",mul[i][j]);
}
printf(" ");
}
}
getch(); //waits untill the user enters a character
return 0;
}