Question

12 Read Sudoku board from a file Implement a function with the following prototype: int read_sudoku_board (const char file_naHow do I set up my read function?

here's my code so far:

int read_sudoku_board(const char file_name[], int board[9][9])
{
    FILE * fp = fopen("sudoku.txt", "r");
    int a,i,j,c;
    int count = 0;
    for(i = 0; i < a; i++){

        for(j = 0;j < 9;j++){
            c = fgetc(fp);
            if(c == '-'){
                board[i][j] = 0;
            }
            else if(c >= 1 && c <= 9)
                printf(" %d");
            else
                return -2;

        }
        count++;

    }
    if(count != a-1)
        return -1;
    else
        return 0;


}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the program to read and write the board values:

Program:

#include <stdio.h>
#include <string.h>


#define SIZE 10
#define ROW 9
#define COL 9
int board[9][9];

int read_sudoku_board(const char filename[], int board[ROW][COL])
{
FILE *fp = fopen(filename, "r");
int count = 0, i=0;
char row[SIZE]={'\0'};
int j=0, k=0;

char outfile[50] = {'\0'};

strncat(outfile, filename, strlen(filename));
strncat(outfile, ".out", strlen(".out"));

if(fp == NULL)
{
printf("Error opening file\n");
return -1;
}

memset(row, '\0', sizeof(row));
while( fscanf(fp, "%s\n"
,&row) != EOF )
{
if(j > 8)
break;

if(strlen(row) != 9)
{
printf("Error not enough lines or short\n");
return -3;
}

for(i=0; i<strlen(row); i++)
{
if((row[i] >= 49 && row[i] <= 58) || (row[i] == '-'))
{
if(row[i] == '-')
board[j][k++] = 0;
else
board[j][k++] = row[i] - '0';
}
else
{
printf("Error Invalid Chars\n");
return -2;
}
}
j++;
k=0;
}

fclose(fp);

write_sudoku_board(outfile, board);
}

int write_sudoku_board(const char filename[], int board[ROW][COL])
{
int i=0, j=0;
FILE *fp=fopen(filename, "w");


if(fp == NULL)
{
printf("Error opening file\n");
return -1;
}

for(i=0; i<ROW; i++)
{
for(j=0; j<COL; j++)
{
if(board[i][j])
fprintf(fp, "%d", board[i][j]);
else
fprintf(fp, "-");
}
fprintf(fp, "\n");
}

printf("Successfully created the output file %s with board values\n", filename);
fclose(fp);
}
int main( int argc, char *argv[])
{

if (argc != 2)
{
printf("Usage: %s <file-name>\n", argv[0]);
return -1;
}

read_sudoku_board(argv[1], board);

return -3;
}

Output:

USER> cat data.txt

1-3-5-7-1
1-3-5-7-9
2-3-5-7-9
--3-5-7-9
1-3-5-7-9
1-3-5-7-9
--------9
1-3-5-7-9
1-3-5-7-9

USER > ./a.out data.txt
Successfully created the output file data.txt.out with board values

USER > cat data.txt.out

1-3-5-7-1
1-3-5-7-9
2-3-5-7-9
--3-5-7-9
1-3-5-7-9
1-3-5-7-9
--------9
1-3-5-7-9
1-3-5-7-9

USER >./a.out
Usage: ./a.out <file-name>

Add a comment
Know the answer?
Add Answer to:
How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify m...

    This is due in a few hours I don't know if I did this correctly can you double check my code to see if my return statements are fine and I follow the instructions accurately. Thank you!!! Modify my code if anything return statement seems misplaced or inaccurately called. Here is my code: int write_sudoku_board(const char file_name[ ], int board[9][9]) { int number; int i,j; int a = 9; FILE* fp = fopen("sudoku.txt", "w"); int count = 0; for(i =...

  • Help due in a few hours I need to clean this up can you help me clean up this code to accomplish the same task but with fewer lines/ more elegantly. Thank you! My code for the function is below: int...

    Help due in a few hours I need to clean this up can you help me clean up this code to accomplish the same task but with fewer lines/ more elegantly. Thank you! My code for the function is below: int is_valid_board(int board[9][9]) { int array[10]; int array_box[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; int i, j, k, x, y; //rows=i, columns =j for (i=0; i<9; i++) { //Reset test array for (x = 0; x...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • need this in c programming and you can edit the code below. also give me screenshot...

    need this in c programming and you can edit the code below. also give me screenshot of the output #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define LINE_SIZE 1024 void my_error(char *s) { fprintf(stderr, "Error: %s\n", s); perror("errno"); exit(-1); } // This funciton prints lines (in a file) that contain string s. // assume all lines has at most (LINE_SIZE - 2) ASCII characters. // // Functions that may be called in this function: // fopen(), fclose(), fgets(), fputs(),...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • C programming language Purpose The purpose of this assignment is to help you to practice working...

    C programming language Purpose The purpose of this assignment is to help you to practice working with functions, arrays, and design simple algorithms Learning Outcomes ● Develop skills with multidimensional arrays ● Learn how to traverse multidimensional arrays ● Passing arrays to functions ● Develop algorithm design skills (e.g. recursion) Problem Overview Problem Overview Tic-Tac-Toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the...

  • I need help finding what is wrong with this code, it is for a CS course...

    I need help finding what is wrong with this code, it is for a CS course I am taking which codes in C (I am using XCode on Mac to code). This is the assignment: "Write a program that performs character processing on 10 characters read in from a file, and writes the results to output files. The program should read from “input.dat”. The program should write the ASCII values of the characters to “output_ascii.dat”. The program should print statistics...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • My code doesn't output correctly using a .txt file. How do I clean this up so...

    My code doesn't output correctly using a .txt file. How do I clean this up so it posts correctly? ----------------------------------------------- CODE ---------------------------------------------- #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <fstream> using namespace std; struct Customer {    int accountNumber;    string customerFullName;    string customerEmail;    double accountBalance; }; void sortDesc(Customer* customerArray, int size); void print(Customer customerArray[], int size); void print(Customer customerArray[], int size) {    cout << fixed << setprecision(2);    for (int i = 0; i...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT