Question

C++

Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result.

a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array.

b) Function addMatrices: This function calculatesthe sum result of two matrices.

c) Function printMatrix: This function prints the matrix to screen.

You need to use "#define SIZE x" directive to set the constant for the size of arrays to x (arbitrary integeger value, dont make it too big). To make things easier, you can set the number of rows equal to the number of columns.

It is recommended to write the functions with the following signatures:

void inputMatrix(int matrix[SIZE][SIZE]);

void addMAtrices (int matrix1[SIZE][SIZE]), int matrix2[SIZE][SIZE]), int sum[SIZE][SIZE]);

void printMatrix(int matrix[SIZE][SIZE]);
Image for Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your progr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//method for adding the natrices void addMatrices (int, matrixl [SIZE] [SIZE],int matrix2[SIZE] [SIZE],int sum[SIZE] [SIZE],i//main method int main () //declaring variables int matrix1[SIZE] [SIZE], matrix2[SIZE] [SIZE], sum[SIZE] [SIZE], rows, colsiC.\Users Pidocuments visual stud....- Enter No:of rows Enter No:Of colonns: Enter Element:1:1-2 Enter Element 1:2-3 Enter Ele

Executable code:


#include<iostream>
//defining the size
#define SIZE 10
using namespace std;
//method to input the matrix
void inputMatrix(int a1[SIZE][SIZE],int rows,int cols)
{
    int a,b;
    for(a=1;a<=rows;a++)
    {
    for(b=1;b<=cols;b++)
    {
        cout<<"Enter Element:"<<a<<":"<<b<<"=";
       cin>>a1[a][b];
            }
    }
}
//method for adding the natrices
void addMatrices(int matrix1[SIZE][SIZE],int matrix2[SIZE][SIZE],int sum[SIZE][SIZE],int rows,int cols)
{
    int a,b;
    for(a=1;a<=rows;a++)
    {
    for(b=1;b<=cols;b++)
    {
    sum[a][b] = (matrix1[a][b] + matrix2[a][b]);
    }
    }
}
//method for printing the matrices
void printMatrix(int matrix[SIZE][SIZE],int rows,int cols)
{
    int a,b;
    for(a=1;a<=rows;a++)
        {
        for(b=1;b<=cols;b++)
        {
            cout<<matrix[a][b]<<endl;
         }
        cout<<"\n";
        }
}
//main method
int main()
{
   //declaring variables
    int matrix1[SIZE][SIZE],matrix2[SIZE][SIZE],sum[SIZE][SIZE],rows,cols;
    //getting inputs
    cout<<"Enter No:Of rows :"<<endl;
    cin>>rows;
    cout<<"Enter No:Ofcolomns :"<<endl;
    cin>>cols;
   //calling functions
    inputMatrix(matrix1,rows,cols);
    inputMatrix(matrix2,rows,cols);
    addMatrices(matrix1,matrix2,sum,rows,cols);
    printMatrix(sum,rows,cols);
    system("pause");
return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function to add two one-dimensional matrices of integer values. The matrices are represented as...

    Write a function to add two one-dimensional matrices of integer values. The matrices are represented as arrays of int and the function must return the result as a pointer to a new dynamically allocated array of int. You can assume the parameters are always of length greater than 0 and that no errors can occur.

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • Sum of two matrices

    Create a program that:Receives as input in command line the sizes [rows] and [cols] of a two dimensional array, and [minValue] and [maxValue] between which the random numbers will be generated. Assume maxValue <= 999Generates two random two dimensional array with numbers between minValue and maxValue (inclusive) with the given size (rows x cols), and store them in m1 and m2Compute the sum of the two matrices and stores the result in a new matrix mSum.

  • Write a C++ program to read two matrices with any size. Your program should have at...

    Write a C++ program to read two matrices with any size. Your program should have at least the following functions: Main() Read a matrix Add two matrices Subtract two matrices Multiply two matrices Divide two matrices Display a matrix

  • Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and...

    Write a MIPS program to that will take two 4x4 matrices, and calculate their sum and product, using row major, and column major math (so this is actually 4 problems, but obviously they’re all pretty related). I generated two sample arrays to test 2   1       9       2 7   9       10      10 3   4       4       4 2   5       4       4 8 7 1 2 2 7 8 6 7 5 6 8 9 4 8 9 The output of your program...

  • Exercise 3: Write a method to add two matrices. The header of the method is: I...

    Exercise 3: Write a method to add two matrices. The header of the method is: I public static double[][] addMatrix(double [ ][] a, double[][] b) In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let e be the resulting matrix. Each element c is c -a, +b For example, for two 3x3 matrices a and b, c is Write a test program that (a) prompts the user to...

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

  • Write a C program (not C++) that calls a function to multiply a matrix (two dimensional...

    Write a C program (not C++) that calls a function to multiply a matrix (two dimensional array). The main program should ask the user to enter an integer that will be used as the matrix “adder” and then call the function. The function should perform a matrix increment (every element of the array is incremented by the same value) and assign the result to another array. The function should be flexible enough to work with any array size and “adder”,...

  • Here's the main function of a C program that Reads in two matrices by prompting the...

    Here's the main function of a C program that Reads in two matrices by prompting the user for their dimensions, dynamically allocating memory from the heap, and reading the values into the memory using row major ordering for the matrix values. Multiplies the two matrices together and puts the result into another dynamically allocated piece of memory (after checking that the dimensions are appropriate for matric multiplication). Outputs the two input and the result matrices. Write the implementations of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT