Question

In: Computer Science

Write Matrix Addition 2 D (dimensional) Array program in c++.

Write Matrix Addition 2 D (dimensional) Array program in c++.

Solutions

Expert Solution

#include <iostream>
using namespace std;

void matrixAdd(int** A, int** B, int** R1, int r, int c){
   int i,j;
   for(i = 0;i<r;i++){
      for(j = 0;j<c;j++){
         R1[i][j] = A[i][j] + B[i][j];
      }
   }
}

int main(){
   int rows, columns;
   
   cout<<"Enter number of rows: ";
   cin>>rows;
   
   cout<<"Enter number of columns: ";
   cin>>columns;
   
   int** arr1 = new int*[rows];
    for(int i = 0; i < rows; ++i)
        arr1[i] = new int[columns];
    
   int** arr2 = new int*[rows];
    for(int i = 0; i < rows; ++i)
        arr2[i] = new int[columns];
   
   int** res = new int*[rows];
    for(int i = 0; i < rows; ++i)
        res[i] = new int[columns];
        
   cout<<"Enter "<<rows<<" x "<<columns<<" matrix 1: "<<endl;
   for(int i = 0;i<rows;i++){
      for(int j = 0;j<columns;j++){
         cin>>arr1[i][j];
      }
   }
   
   cout<<"\nEnter "<<rows<<" x "<<columns<<" matrix 2: "<<endl;
   for(int i = 0;i<rows;i++){
      for(int j = 0;j<columns;j++){
         cin>>arr2[i][j];
      }
   }
   
   
   matrixAdd(arr1,arr2,res,rows,columns);
   cout<<"\nAddition matrix:\n";
   for(int i = 0;i<rows;i++){
      for(int j = 0;j<columns;j++){
         cout<<res[i][j]<<" ";
      }
      cout<<endl;
   }
   
   return 0;
}


Related Solutions

C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
[C++ Coding question] write a program which inputs data to a two-dimensional array: - Input number...
[C++ Coding question] write a program which inputs data to a two-dimensional array: - Input number of rows r aand number of columns c - Create a two-dimensional array of integers int numbers[r][c] -input data for each element of numbers Your program must compute and display the largest number is each row and column of the number array
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size...
write a c++ program. Define a class ‘Matrix’ which contain 2D int array ‘m’ of size 3x3 as private member.        There should be two public methods within the class definition namely: void setMatrixValue(int i, int j); that should set m[i][j] with user defined values int getMatrixValue(int i, int j); that should return m[i][j] Make a global function named ‘CrossProduct(Matrix m1, Matrix m2)’ that should compute the marix multiplication
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT