Using C++ Requirements You are to create a program that performs six operations:
ID: 3740637 • Letter: U
Question
Using C++
Requirements You are to create a program that performs six operations: 1. Read: Read a board from a file into our board] [] array. 2. Write: Write the board[] array back to a file. 3. 'D' Option: Display the contents of the board[] [1 array on the screen. 4. 'E' Option: Edit a square with no verification that it is correct or valid 5. 'Q' Option: Exit the interaction loop and launches the Write code. 6' '?' Option: Displays the instructions on the screen. Example The following is a sample run of the program: Where is your board located? prj4.txt Options: ? Show these instructions D Display the board E Edit one square S Show the possible values for a square Save and Quit ABCDEFGHI 11 5 9 1 7 2 3 13 2 2 4 7 16 5 4 2 412 713 6 5 19 3 11 4 11 3 9 93 21 17 14 > E What are the coordinates of the square: e5 What is the value at 'E5': 8 What file would you like to write your board to: deleteMe Please note that on the Options, there are exactly three spaces at the front of each line.Explanation / Answer
For the given data write the C++ program as shown in below
C++ language program:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
void read(int board[9][9])
{
string file;
cout << "Where is your board located? ";
cin >> file;
ifstream in(file.c_str());
if (in.fail())
{
cout << "Unable to open" << endl;
exit(1);
}
int i = 0;
while (!in.eof())
{
in >> board[i / 9][i % 9];
i++;
}
in.close();
}
void write(int board[9][9])
{
string file;
cout << "What file would you like to write your board to: ";
cin >> file;
ofstream out(file.c_str());
if (out.fail())
{
cout << "Unable to write" << endl;
exit(1);
}
int i, j;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
out << " " << board[i][j];
}
out << endl;
}
}
void display(int board[9][9])
{
int i, j;
cout << setw(3) << " ";
for (i = 0; i < 9; i++)
{
if (i % 3 == 0)
cout << setw(3) << " ";
cout << setw(3) << (char)(i + 'A');
}
cout << endl;
for (i = 0; i < 9; i++)
{
if (i % 3 == 0)
cout << " -" << endl;
cout << " " << (i + 1) << " ";
for (j = 0; j < 9; j++)
{
if (j % 3 == 0)
cout << " | ";
if (board[i][j] == -1)
{
cout << setw(3) << ' ';
}
else
{
cout << setw(3) << board[i][j];
}
}
cout << "|" << endl;
}
}
void edit(int board[9][9])
{
char ind[2];
cout << "What are the coordinates of the squares: ";
cin >> ind;
if (ind[0] >= 'a' && ind[0] <= 'z')
ind[0] = ind[0] - 32;
int r = ind[0] - 'A';
int c = ind[1] - '0';
cout << "What is the value at: '" << ind << "': ";
while (1)
{
cin >> board[r][c-1];
if (board[r][c-1] > 0 && board[r][c-1] <= 9)
break;
cout << "Invalid value, try again: ";
}
}
void show_possible(int board[9][9])
{
char ind[2];
cout << "What are the coordinates of the squares: ";
cin >> ind;
if (ind[0] >= 'a' && ind[0] <= 'z')
ind[0] = ind[0] - 32;
int r = ind[0] - 'A';
int c = ind[1] - '0'-1;
int i, rows[9] = {0}, cols[9] = {0};
for (i = 0; i < 9; i++)
{
if (board[i][c] != -1)
rows[board[i][c] - 1] = 1;
if (board[r][i] != -1)
cols[board[c][i] - 1] = 1;
}
cout << "Possible values: ";
bool exists = false;
for (i = 0; i < 9; i++)
{
if (rows[i] == 0 && cols[i] == 0)
{
cout << (i + 1) << " ";
exists = true;
}
}
if (!exists)
cout << "none";
}
int main()
{
int board[9][9];
read(board);
char choice;
do
{
cout << " Options: " << endl;
cout << " ? Show these instructions" << endl;
cout << " D Display the board" << endl;
cout << " E Edit one square" << endl;
cout << " S Show the possible values for a square" << endl;
cout << " Q Save and Quit" << endl;
cout << " Enter your choice: ";
cin >> choice;
switch (choice)
{
case '?':
cout << "Instructions: " << endl;
cout << "* Each board consists of a 9x9 grid containing given clues in various places. * The object is to fill all empty squares so that the numbers 1 to 9 appear exactly once in each row, column and 3x3 box." << endl;
break;
case 'd':
case 'D':
display(board);
break;
case 'e':
case 'E':
edit(board);
break;
case 's':
case 'S':
show_possible(board);
break;
case 'q':
case 'Q':
write(board);
exit(1);
default:
cout << "Invalid choice. " << endl;
}
} while (choice != 'Q');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.