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
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)
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
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a 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 all the values in the array. ▪ getRowTotal ....
Using JAVA Write a program that uses a 2-D array to store the highest and lowest...
Using JAVA Write a program that uses a 2-D array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and highest and lowest temperatures of the year. Your program must consist of the following methods with their appropriate parameters: a.) getData: This method reads and stores the data in the 2-D array. b.) averageHigh: This method calculates and returns the average high temperature of the year. c.)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT