Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * TODO: COMPLETE THE MAIN FUNCTION * This program prints valid ( without quotes ) if the input file contains * a valid state

/* TODO: COMPLETE THE MAIN FUNCTION
* This program prints "valid" (without quotes) if the input file contains
* a valid state of a Sudoku puzzle board wrt to rows and columns only.
* It prints "invalid" (without quotes) if the input file is not valid.
*
* Usage: A single CLA that is the name of a file that contains board data.
*
* argc: the number of command line args (CLAs)
* argv: the CLA strings, includes the program name
*
* Returns 0 if able to correctly output valid or invalid.
* Exit with a non-zero result if unable to open and read the file given.
*/
int main( int argc, char **argv ){
// TODO: Check if number of command-line arguments is correct.
// Open the file
FILE *fp = fopen(*(argv +1),"r");
if (fp == NULL){
printf("Can't open file for reading.
");
exit(1);
}
// will store the board's size, number of rows and columns
int size;
// TODO: Call get_board_size to read first line of file as the board size.
// TODO: Dynamically allocate a 2D array for given board size.
// You must dyamically create a 1D array of pointers to other 1D arrays of ints
// Read the remaining lines.
// Tokenize each line and store the values in your 2D array.
char *line = NULL;
size_t len =0;
char *token = NULL;
for (int i =0; i < size; i++){
// read the line
if (getline(&line, &len, fp)==-1){
printf("Error while reading line %i of the file.
", i+2);
exit(1);
}
token = strtok(line, DELIM);
for (int j =0; j < size; j++){
// TODO: Complete the line of code below
// to initialize elements of your 2D array.
/* ADD ARRAY ACCESS CODE HERE */= atoi(token);
token = strtok(NULL, DELIM);
}
}
// TODO: Call valid_board and print the appropriate
// output depending on the function's return value.
// TODO: Free dynamically allocated memory.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Deductive And Object Oriented Databases Second International Conference Dood 91 Munich Germany December 18 1991 Proceedings Lncs 566

Authors: Claude Delobel ,Michael Kifer ,Yoshifumi Masunaga

1st Edition

3540550151, 978-3540550150

More Books

Students also viewed these Databases questions