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

Someone help me with this and I modified it a little bit but still can\'t read from the file!!!

This is the code

/*..............myFunction.h ...........*/

#include <iostream>
#include <fstream>
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 read prototype
void printArray(int p_Array[][C_SIZE]); // function print prototype
void sumArray (int p_Array1[][C_SIZE],int p_Array2[][C_SIZE],int p_Array3[][C_SIZE]);

/*..............myFunction.cpp ...........*/

#include \"myfunction.h\"

// Function: printArray
// Purpose: To print values of an array.
// Parameters: Base address of an array.
// Returns: void
// --------------------------------------------------------

void printArray(int p_Array[R_SIZE][C_SIZE]){
for (int i=0; i < R_SIZE; i++)
{
for (int 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


void sumArray(int p_Array1[][C_SIZE],int p_Array2[][C_SIZE],int p_Array3[][C_SIZE]){
//int temp[R_SIZE][C_SIZE]
for (int i=0; i < R_SIZE; i++)
{
for (int j=0; j< C_SIZE; j++)
{
//temp[i][j]= p_Array1[i][j]+ p_Array2[i][j]+p_Array3[i][j];
p_Array3[i][j] = p_Array1[i][j] + p_Array2[i][j];
}
cout << endl;
}
printArray(p_Array3); // calling function to print the array
}

/*..............main.cpp ...........*/

#include <iostream>
#include <fstream>

#include \"myfunction.h\"
using namespace std;

  

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
}

Can anyone help me please

Solution

/*..............main.cpp ...........*/

#include <iostream>
#include <fstream>
#include \"myFunction.h\"
#include <string>
using namespace std;

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
}

/*..............myFunction.cpp ...........*/
#include \"myFunction.h\"
#include <string>
using namespace std;
using namespace std;
// Function: printArray
// Purpose: To print values of an array.
// Parameters: Base address of an array.
// Returns: void
// --------------------------------------------------------
void printArray(int p_Array[R_SIZE][C_SIZE]){
for (int i=0; i < R_SIZE; i++)
{
for (int 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)
{
string line;
ifstream myfile (fName);
int i=0, j=0;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
  
std::istringstream iss(line);
j=0;
do
{
string sub;
iss >> sub;
p_Array[i][j++]= std::stoi(sub);
} while (iss);
i++;
}
myfile.close();
}
else cout << \"Unable to open file\";

return 0;
} // end InitArray function

void sumArray(int p_Array1[][C_SIZE],int p_Array2[][C_SIZE],int p_Array3[][C_SIZE]){
//int temp[R_SIZE][C_SIZE]
for (int i=0; i < R_SIZE; i++)
{
for (int j=0; j< C_SIZE; j++)
{
//temp[i][j]= p_Array1[i][j]+ p_Array2[i][j]+p_Array3[i][j];
p_Array3[i][j] = p_Array1[i][j] + p_Array2[i][j];
}
cout << endl;
}
printArray(p_Array3); // calling function to print the array
}

/* ------------------------- myFunction.h ---------------------------- */

#include <iostream>
#include <fstream>
#include <string>
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 read prototype
void printArray(int p_Array[][C_SIZE]); // function print prototype
void sumArray (int p_Array1[][C_SIZE],int p_Array2[][C_SIZE],int p_Array3[][C_SIZE]);

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 scre
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 scre
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 scre
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 scre
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 scre
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 scre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site