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

using c++ write the code necessary to make the Sudoku appear on the screen and t

ID: 3735083 • Letter: U

Question

using c++

write the code necessary to make the Sudoku appear on the screen and to allow some simple interactions.

Requirements

You are to create a program that performs six operations:

Read: Read a board from a file into our board[][] array.

Write: Write the board[] array back to a file.

'D' Option: Display the contents of the board[][] array on the screen.

'E' Option: Edit a square with no verification that it is correct or valid.

'Q' Option: Exit the interaction loop and launches the Write code.

'?' Option: Displays the instructions on the screen.

Example

The following is a sample run of the program:

Please note that on the Options, there are exactly three spaces at the front of each line.

Process

Perhaps the easiest way to do this is in a four-step process:

Create the framework for the program using stub functions based on the structure chart from your design document.

Write each function. Test them individually before "hooking them up" to the rest of the program.

Explanation / Answer

#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;

}