Sample Input: 2 3 1 2 3 0 1 2 ------ 3 1 4 5 6 Sample Output: 2 1 37 17 CS 302 A
ID: 3741943 • Letter: S
Question
Sample Input:
2 3
1 2 3
0 1 2
------
3 1
4
5
6
Sample Output:
2 1
37
17
CS 302 Assignment 1 The Matrix Revolution In this assignment you need to write and execute a program that will perform addition, subtraction and multiplication of two valid matrices. The matrices will be stored as double linked lists. Each item in the matrix will contain pointers to its neighboring values. Meaning the coners will have two pointers and the middle items will have four pointers. The entire matrix will only be accessible with a start pointer that points to the top right position as shown below start To perform the addition and subtraction you must use a recursive function. You are not allowed to use a loop. You can however, use a loop for the matrix multiplication Your link list should use templates instead of a specific data type. I highly recommend your link list works before moving on to doing your matrix class. Input will be given in the form of two matrices where the first two values of the input file will be the dimensions of the first matrix. Then the items of the first matrix, then the next two values will be the dimensions of the second matrix and then the items of the matrix. You must check if the matrix dimensions are valid for each operation and only perform those operations which the dimensions are valid for Notes Comment your source code appropriately. Make sure you name your program file in accordance with the syllabus stipulations Test your program by running it a few times with different input to make sure the output is correct. Test and execute your program by using data-in by Linux input redirection. If your executable code is Asif and your data file is named data# execute as: Ast#Explanation / Answer
//Here is the code for your problem in C language
//Input must be integer,space or new line character only
#include <stdio.h>
const int MAX = 1000;
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];
void multiplyMatrixRec(int n1, int m1, int n2, int m2)
{
static int i = 0, j = 0, k = 0;
if (i >= n1)
return;
if (j < m2){
if (k < m1){
C[i][j] += A[i][k] * B[k][j];
k++;
multiplyMatrixRec(n1, m1, n2, m2);
}
k = 0;
j++;
multiplyMatrixRec(n1, m1, n2, m2);
}
j = 0;
i++;
multiplyMatrixRec(n1, m1, n2, m2);
}
void multiplyMatrix(int n1, int m1, int n2, int m2){
if (n2 != m1){
printf("Not Possible ");
return;
}
multiplyMatrixRec(n1, m1, n2, m2);
for (int i = 0; i < n1; i++){
for (int j = 0; j < m2; j++)
printf("%d ", C[i][j]);
printf(" ");
}
}
int main()
{
int n1,n2,m1,m2;
scanf("%d %d",&n1,&m1);
for(int i=0;i<n1;i++)
for(int j=0;j<m1;j++)
scanf("%d ",&A[i][j]);
scanf("%d %d",&n2,&m2);
for(int i=0;i<n2;i++)
for(int j=0;j<m2;j++)
scanf("%d ",&B[i][j]);
printf("%d %d ", n1,m2);
multiplyMatrix(n1, m1, n2, m2);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.