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

Create a C++ program to read in a file of integers and store them in a 2D array.

ID: 3538300 • Letter: C

Question

Create a C++ program to read in a file of integers and store them in a 2D array. The file has one integer per line. Your 2D array must be 43 rows and 40 columns. Create a function to iterate over the 2D array and display and 'X' on the screen if the integer is even, otherwise display a '.' (single period). The dimensions of the displayed image are the same as the 2D array, 43 rows by 40 columns. Your program must have used a function to iterate over the 2D array and another function to generate the display.


Sample File:

Explanation / Answer

//assumes sample file is called 'input.txt'

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

void iterate(int n[][40],ifstream& myfile)

{

for(int i=0;i<43;i++)

{

for(int j=0;j<40;j++)

{

myfile>>n[i][j];

}

}

}


void generateDisplay(int n[][40])

{

cout<<endl;

for(int i=0;i<43;i++)

{

for(int j=0;j<40;j++)

{

if(n[i][j]%2 == 0)

{ cout<<"X"; }

else

{ cout<<"."; }

}

cout<<endl;

}

}


int main()

{

int nums[43][40];

int tmp;

ifstream myfile("input.txt");

if (myfile.is_open())

{

iterate(nums,myfile);

  

}

  

myfile.close();

generateDisplay(nums);

  

  

return 0;

}