Question

In: Computer Science

C++ For this assignment, you will write a C++ program to either add (A), subtract (S),...

C++

For this assignment, you will write a C++ program to either add (A), subtract (S), multiply (M), and (N), or (O) two matrices if possible. You will read in the dimensions (rows, then columns) of the first matrix, then read the first matrix, then the dimensions (rows then columns) of the second matrix, then the second matrix, then a character (A, S, M, N, O) to determine which operation they want to do.

The program will then perform the operation, print the answer, and quit. The program should have five functions in addition to main - one for add, one for subtract, one for multiply, one for add, and one for or, but main should do all the input and output. Matrices will be no bigger than 10x10 and will be ints. You cannot assume the matrices will be square.

SAMPLE:

$ ./a.out
input the row size and col size of A
3 2
input matrix A
2 1
-1 0
3 4
input the row size and col size of B
3 2
input matrix B
12 -18
3 9
6 -3
Choose your operation: A for add, S for subtract, M for multiply
                        N for and, O for or
A
The answer is:
14 -17 
2 9 
9 1 

Solutions

Expert Solution

Program :

#include <iostream>
using namespace std;
int row1,column1,row2,column2;
void add_matrix(int **A,int **B){
   int i,j=0;
   if(row1 == row2 && column1 == column2){
       cout << "The answer is:" << endl;
           for(i=0;i<row1;i++){
               for(j=0;j<column1;j++){
                   cout << A[i][j]+B[i][j] << " ";
               }
               cout << "\n";
           }
   }else{
       cout << "not possible";
   }
}
void sub_matrix(int **A,int **B){
   int i,j=0;
   if(row1 == row2 && column1 == column2){
       cout << "The answer is:" << endl;
           for(i=0;i<row1;i++){
               for(j=0;j<column1;j++){
                   cout << A[i][j]-B[i][j] << " ";
               }
               cout << "\n";
           }
   }else{
       cout << "not possible";
   }
}
void mul_matrix(int **A,int **B){
   int i,j,k=0;
   int c[row1][column2];
   if(column1 == row2){
       cout << "The answer is:" << endl;
           for (i=0;i<row1;i++){
               for(j=0;j<column2;j++){
                   c[i][j]=0;
                   for(k=0;k<column1;k++){
                       c[i][j]=c[i][j]+A[i][k]*B[k][j];
                   }
               }
           }
           for(i=0;i<row1;i++){
               for(j=0;j<column1;j++){
                   cout << c[i][j] << " ";
               }
               cout << "\n";
           }
   }else{
       cout << "not possible";
   }
}
void and_matrix(int **A,int **B){
   int i,j=0;
   if(row1 == row2 && column1 == column2){
       cout << "The answer is:" << endl;
           for(i=0;i<row1;i++){
               for(j=0;j<column1;j++){
                   cout << (A[i][j] && B[i][j]) << " ";
               }
               cout << "\n";
           }
   }else{
       cout << "not possible";
   }
}
void or_matrix(int **A,int **B){
   int i,j=0;
   if(row1 == row2 && column1 == column2){
       cout << "The answer is:" << endl;
           for(i=0;i<row1;i++){
               for(j=0;j<column1;j++){
                   cout << (A[i][j] || B[i][j]) << " ";
               }
               cout << "\n";
           }
   }else{
       cout << "not possible";
   }
}
int main(){
   cout << "Input the row size and col size of A" << endl;
   cin >> row1 >> column1;
   if(row1 > 10 || column1 > 10){
       return 0;
   }
   int i,j=0;
   int **A;
   A = new int *[row1];
   for(int i = 0; i <row1; i++)
   A[i] = new int[column1];
   cout << "input matrix A " << endl;
   for(i=0;i<row1;i++){
       for(j=0;j<column1;j++){
           cin >> A[i][j];
       }
   }
   cout << "Input the row size and col size of B" << endl;
   cin >> row2 >> column2;
   if(row2 > 10 || column2 > 10){
       return 0;
   }
   int **B;
   B = new int *[row1];
   for(int i = 0; i <row1; i++)
   B[i] = new int[column1];
   cout << "input matrix B " << endl;
   for(i=0;i<row1;i++){
       for(j=0;j<column1;j++){
           cin >> B[i][j];
       }
   }
   cout << "Choose your operation: A for add,S for subtract,M for multiple,N for and,O for or"<<endl;
   char op;
   cin >> op;
   switch(op){
       case 'A':
           add_matrix(A,B);
           break;
       case 'S':
           sub_matrix(A,B);
           break;
       case 'M':
           mul_matrix(A,B);
           break;
       case 'N':
           and_matrix(A,B);
           break;
       case 'O':
           or_matrix(A,B);
           break;
       default:
           cout << "Invalid operation";
           break;
   }
   return 0;
}

Output :


Related Solutions

Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
Write a python program to display a menu with the following options: (1) add, (2) subtract,...
Write a python program to display a menu with the following options: (1) add, (2) subtract, (3) multiply, (4) divide (5) mod, and (6) do nothing. I f the user enters something else (except 1-6), the program should display an error message. Otherwise, it should ask the user for two numbers, perform the calculation, and display the result.
In C++ For this assignment, you will write a program to count the number of times...
In C++ For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
For this week’s lab assignment, you will write a program called lab9.c. You will write a...
For this week’s lab assignment, you will write a program called lab9.c. You will write a program so that it contains two functions, one for each conversion. The program will work the same way and will produce the same exact output. The two prototypes should be the following: int btod(int size, char inputBin[size]); int dtob(int inputDec); The algorithm for the main() function should be the following: 1. Declare needed variables 2. Prompt user to enter a binary number 3. Use...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing...
C PROGRAMMING – Steganography In this assignment, you will write an C program that includes processing input, using control structures, and bitwise operations. The input for your program will be a text file containing a large amount of English. Your program must extract the “secret message” from the input file. The message is hidden inside the file using the following scheme. The message is hidden in binary notation, as a sequence of 0’s and 1’s. Each block of 8-bits is...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT