Hello, I need help with my computer science project. Printing with 2D Arrays In
ID: 638977 • Letter: H
Question
Hello, I need help with my computer science project.
Printing with 2D Arrays
In this program you will be reading in a 6 x 6 array of characters. You need to use a 2 dimension array of
chars to do this. The constants defining the maximum number of rows and columns can be global
variables, but nothing else can be a global variable.
Your program must have three or more functions. You will have the main function, one or more
functions to read in the file, and one or more functions to display the output of the character array.
You will be reading these characters from an input file. The characters need to be read in a character at
a time and can include a space character. You will not be able to use the >> stream operator for this
since the >> operator ignores spaces. You can use the get() function discussed in Chapter 3 of your book
(see section 3.8). The example in the book uses cin.get(), but you can replace cin with your input file
(ifstream) object. Your get() function will also read in newline ( ) characters. You need to ignore the
newline characters.
The input file will contain records as follows (each line below is assumed to end with a newline):
<Row1>
2ndRow)
[Row3]
Now 4
Fifth1
6Final
For each row in the 6 x 6 array of characters you will output the row (from index 0 to index == 5) AND
the same row from index 5 to index 0. So the first row (above) will be output as:
<Row1>>1woR<
The second row will be:
2ndRowwoRnd2
All 6 rows will be:
<Row1>>1woR<
2ndRowwoRdn2
[Row3]]3woR[
Now 44 woN S15 CS/CE/TE 1337.006 Assignment 1 2
Fifth11htfiF
6FinallaniF6
You are not done yet, however. You need to do the same 6 rows of output again, but you will be
outputting row 6, then row 5, then row 4, down to the 1st
row.
Here is the final output from the input file above:
<Row1>>1woR<
2ndRowwoRdn2
[Row3]]3woR[
Now 44 woN
Fifth11htfiF
6FinallaniF6
6FinallaniF6
Fifth11htfiF
Now 44 woN
[Row3]]3woR[
2ndRowwoRdn2
<Row1>>1woR<
Looking at another example we will assume the input file contains:
x
***x*
* x
*x #
x #
x* #
S15 CS/CE/TE 1337.006 Assignment 1 3
The output for this will be as follows:
xx
***x**x***
* x x *
*x ## x*
x # # x
x* # # *x
x* # # *x
x # # x
*x ## x*
* x x *
***x**x***
xx
The above is the basic output from the program.
Additional requirements:
Your program must be able to handle any number of input files. You will read in the name of the input
file from the application user. If the file can be opened you will format the output as discussed above. If
the file is invalid you need to output an error message saying that the file could not be opened.
After you have displayed the output, or an error message, your program should ask the user if they have
any more files they want to process. If they do, you need to read in the next file name and process it, as
you did for the first file. You will continue to process files until the user tells you they have no more files.
I have given you two input files, input1.txt and input2.txt. You need to create two additional text files
that you will submit with your program. Try and be creative with your text files to see what kind of
output you can create.
Program output:
Here is some sample output. You output of the text file must match what is shown here for the same
input. The other messages can be different, but you must have all of the needed processing:
Enter the name of an input file: badfile.txt
The input file name "badfile.txt" could not be found
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue): y
Enter the name of an input file: input1.txt
******
* *
* @@ *
* @ @ *
* @ @ *
* @ @ *
* @ @ *
* @ @ *
* @ @ *
* @@ *
* *
******
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue): y
Enter the name of an input file: input2.txt
xx
***x**x***
* x x *
*x ## x*
x # # x
x* # # *x
x* # # *x
x # # x
*x ## x*
* x x *
***x**x***
xx
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue): n
S15 CS/CE/TE 1337.006 Assignment 1 5
Part 1: Program basics
Make sure your variable names have meaningful names and that you have included appropriate
comments in your application.
INPUT FILE 1
INPUT FILE 2
Explanation / Answer
Program Code:
// ReadandFormatTextFiledata.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
//global variables
const int MAXROWS=6;
const int MAXCOLUMNS=6;
//declaration of functions:
void readData(ifstream &file,char TwoD[][MAXCOLUMNS], int size);
void printData(char TwoD[][MAXCOLUMNS], int size);
//main function
int main()
{
//declare a two dimensional array and other variables
char TwoDArray[MAXROWS][MAXCOLUMNS];
char filename[20];
char choice;
//continuous loop
do
{
//prompt the user to enter the name of the file
cout<<"Enter the name of an input file: ";
cin>>filename;
//create a file object
ifstream myfile(filename);
//if file does not exist display an error messaage
if(!myfile)
{
cout<<"The input file name "<<filename<<" could not be found."<<endl;
}
//if the file exist call the readDat function
else
{
readData(myfile,TwoDArray, MAXROWS);
cout<<" ";
}
//prompt the user whether the user wants to work
//with other files
cout<<"Do you want to try again with a new file? "<<endl;
cout<<"Enter n or N to quit, any other character to continue: ";
cin>>choice;
}while(choice !='n' || choice != 'N');
system("pause");
return 0;
}
//definition of the readData function
void readData(ifstream &file, char TwoD[][MAXCOLUMNS], int size)
{
//declare a char variable and increment variable
char ch;
int i=0;
//read the data from the file until the
//the pointer reaches the end of the file
while(!file.eof())
{
int j=0;
//logic to read the data from the file
//and assign each character into the
//array
if(i<MAXROWS)
{
while(j<=MAXCOLUMNS)
{
file.get(ch);
if(ch==' ')
{
}
else
{
TwoD[i][j]=ch;
}
j++;
}
}
i++;
}
//close the file
file.close();
//call the printData function
printData(TwoD, MAXROWS);
}
//definition of printData function
//this contains the logic to print the required
//formatted output
void printData(char TwoD[][MAXCOLUMNS], int size)
{
//logic to print the first six rows
for(int i=0;i<MAXROWS;i++)
{
//logic to print the first six columns
for(int j=0;j<MAXROWS;j++)
{
cout<<TwoD[i][j];
}
//logic to print the reverse of the columns
for(int j=MAXROWS-1;j>=0;j--)
{
cout<<TwoD[i][j];
}
cout<<endl;
}
//logic to print the characters from bottom to top
for(int i=MAXROWS-1;i>=0;i--)
{
//logic to print the first six columns
for(int j=0;j<MAXROWS;j++)
{
cout<<TwoD[i][j];
}
//logic to print the reverse of the columns
for(int j=MAXROWS-1;j>=0;j--)
{
cout<<TwoD[i][j];
}
cout<<endl;
}
}
-------------------------------------------------------------------------------------------------------------------
Sample InputText files:
Input1.txt:
Sample Input files:
Input1.txt:
***
*
* @
* @
* @
* @
Input2.txt:
x
***x*
* x
*x #
x #
x* #
Input3.txt:
# #
# #
@ @
@
# #
# #
Input4.txt:
#
# #
@
@#@
@ @
#
--------------------------------------------------------------------------------------------
Sample Output:
Enter the name of an input file: badfile.txt
The input file name badfile.txt could not be found.
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue: y
Enter the name of an input file: Input1.txt
******
* *
* @@ *
* @ @ *
* @ @ *
* @ @ *
* @ @ *
* @ @ *
* @ @ *
* @@ *
* *
******
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue: y
Enter the name of an input file: Input2.txt
xx
***x**x***
* x x *
*x ## x*
x # # x
x* # # *x
x* # # *x
x # # x
*x ## x*
* x x *
***x**x***
xx
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue: y
Enter the name of an input file: Input3.txt
# # # #
# ## #
@ @@ @
@ @
# ## #
# # # #
# # # #
# ## #
@ @
@ @@ @
# ## #
# # # #
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue: y
Enter the name of an input file: Input4.txt
# #
# ## #
@ @
@#@ @#@
@ @ @ @
##
##
@ @ @ @
@#@ @#@
@ @
# ## #
# #
Do you want to try again with a new file?
Enter n or N to quit, any other character to continue: n
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.