Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this lab exercise, you are required to write a C++ program to add two 4 * 5 m

ID: 3586237 • Letter: I

Question

In this lab exercise, you are required to write a C++ program to add two 4 * 5 matrices and then output the results to the screen.

* Prompt the user to enter the name of two files with matrices

* Compute the sum of the two matrices and output the results

* A sample run is as follows:

* Further requirements:

* Create three functions:

            *readArray with void return and one 2D array argument and one string argument

            *printArray with void return and one 2D array argument

            *sumArray with void return and three 2D array arguments

*Separate your code into three files as described in the lab:

             *main.cpp

             *myFunction.cpp

             *myFunction.h

*Remember, you will have to break the compiling into three steps now:

             *g++ -c main.cpp

             *g++ -c myFunction.cpp

             *g++ main.o myFunction.o -o main

*The two input files are available by clicking: input1.txt and input2.txt

*Once again, your cousin Brittany has been writing code. She has made 6 mistakes. Also, she hasn't broken her code into three files.

This is in c++ program. Please follow all instructions and the output of your code.

input1.txt

input2.txt

Britany's code with 6 mistakes (not in three files yet).

#include <iostream>
#include <fstream>
using namespace std;

const unsigned int R_SIZE=4;
const unsigned int C_SIZE=5;

void readArray( int p_Array[][], string fName ); // function prototype

int main()
{
   int i, j;
   int Nums1[C_SIZE][R_SIZE];
   string filename;
   cout << "Please enter the name of the file containing data for Array 1: ";
   cin >>filename;
   
   readArray(Nums1[][],filename);
   for ( i=0; i < C_SIZE; i++)  
   {
      for ( j=0; j< R_SIZE; j++)
      {
   cout<< Nums1[i][j] << " ";  
      }
      cout << endl;
   }

}

// Function: readArray
// Purpose:   To read values into an array.
// Parameters: Base address of an array.
// Returns:   void
// --------------------------------------------------------
void readArray(int p_Array[][C_SIZE], string fName)
{

   ifstream inF;
   int i, j;

   inF.open(fName.c_str());

   for ( i=0; i < R_SIZE; i++)  
   {
      for ( j=0; j< C_SIZE; i++)
      {
   inF >> Nums1[i][j];  
      }
   }

} // end InitArray function

Explanation / Answer

Mistake 1.  int Nums1[C_SIZE][R_SIZE];

As per array size, it should be 4*5 so should be int Nums1[R_SIZE][C_SIZE];

and 2 Arrays should be used.

int Nums2[R_SIZE][C_SIZE];

Mistake 2.

Input read should start from Rows in outer loop and columns in inner loop

for ( i=0; i < R_SIZE; i++)  
   {
      for ( j=0; j< C_SIZE; i++)
      {
inF >> p_Array[i][j];  
      }
   }

Mistake 3.

readArray(Nums1[][],filename); should be called 2 times to take input of 2 files, input1.txt and input2.txt

and also read method should take input in parameterized array name.

for ( i=0; i < R_SIZE; i++)  
   {
      for ( j=0; j< C_SIZE; i++)
      {
inF >> p_Array[i][j];  
      }
   }

Mistake 4.

Addition should be done in different add function.

for ( i=0; i < R_SIZE; i++)  
   {
      for ( j=0; j< C_SIZE; j++)
      {
Nums1[i][j] +=Nums2[i][j];
      }
      cout << endl;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote