Lab Exercise 2D Arrays in C In this lab exercise you are re
Lab Exercise — 2-D Arrays in C++
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
If you can correct this and then saparete it to three files
#include
#include
#include
using namespace std;
const unsigned int R_SIZE=4;
const unsigned int C_SIZE=5;
void readArray(int p_Array[][C_SIZE], string fName); // function prototype
int main()
{
int i, j;
int Nums1[R_SIZE][C_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 < R_SIZE; i++)
{
for ( j=0; j< C_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[R_SIZE][C_SIZE], string fName)
{
ifstream inF;
int i, j;
inF.open(fName.c_str());
if(inF.fail())
{
cout << fName << \"does not enist\" << endl;
}
else
{
cout << fName << \"exists\" << endl;
}
while(!inF.eof())
{
for ( i=0; i < R_SIZE; i++)
{
for ( j=0; j< C_SIZE; i++)
{
inF >> p_Array[i][j];
}
}
}
} // end InitArray function
Solution
/*..............myFunction.h ...........*/
const unsigned int R_SIZE=4;
const unsigned int C_SIZE=5;
void readArray(int p_Array[][C_SIZE], string fName); // function read prototype
void printArray(int p_Array[][C_SIZE]); // function print prototype
/*..............myFunction.cpp ...........*/
#include \"myfunction.h\"
// Function: printArray
// Purpose: To print values of an array.
// Parameters: Base address of an array.
// Returns: void
// --------------------------------------------------------
void function printArray(int p_Array[R_SIZE][C_SIZE]){
for ( i=0; i < R_SIZE; i++)
{
for ( j=0; j< C_SIZE; j++)
{
cout<< p_Array[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[R_SIZE][C_SIZE], string fName)
{
ifstream inF;
int i, j;
inF.open(fName.c_str());
if(inF.fail())
{
cout << fName << \"does not enist\" << endl;
}
else
{
cout << fName << \"exists\" << endl;
}
while(!inF.eof())
{
for ( i=0; i < R_SIZE; i++)
{
for ( j=0; j< C_SIZE; i++)
{
inF >> p_Array[i][j];
}
}
}
} // end InitArray function
/*..............main.cpp ...........*/
#include <iostream>
#include <fstream>
#include \"myfunction.h\"
using namespace std;
void sumArray (int p_Array1[][C_SIZE],int p_Array2[][C_SIZE],int p_Array3[][C_SIZE]); //function sum prototype
int main()
{
int i, j;
int Nums1[R_SIZE][C_SIZE],Nums2[R_SIZE][C_SIZE];
string filename1, filename2;
cout << \"Please enter the name of the file containing data for Array 1: \";
cin >>filename1;
readArray(Nums1, filename1); // calling function for reading array
printArray(Nums1); // calling to print the array
cout << \"Please enter the name of the file containing data for Array 2: \";
cin >>filename2;
readArray(Nums2, filename2); // calling function for reading array
printArray(Nums2); // calling to print the array
sumArray(Nums1,Nums2,Nums1); // calling for adding the arrays
}
//function for adding array
void function sumArray(int p_Array1[][C_SIZE],int p_Array2[][C_SIZE],int p_Array3[][C_SIZE]){
int temp[R_SIZE][C_SIZE]
for ( i=0; i < R_SIZE; i++)
{
for ( j=0; j< C_SIZE; j++)
{
temp[i][j]= p_Array1[i][j]+ p_Array2[i][j]+p_Array3[i][j];
}
cout << endl;
}
printArray(temp); // calling function to print the array
}



