Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I am working on my c programming. Basically what we do is to check whether queens(1) are in the same row, columns or diagonal.

So I am working on my c programming.

Basically what we do is to check whether queens(1) are in the same row, columns or diagonal.

The instructor told us not to use array[][] to make a board. I don't even understand what that means.

He basically wants us to make use of the pointer variables like **board.

There's Todo's for each method or function. please help me out with it.

Just a little hint would be appreciated.

Thank you. Below is the skeleton code.

#include

#include

#include

char *COMMA = ",";

/* COMPLETED:

* Retrieves from the first line of the input file,

* the number of rows and columns for the board.

*

* fp: file pointer for input file

* rows: pointer to number of rows

* cols: pointer to number of columns

*/

void get_dimensions(FILE *fp, int *rows, int *cols) {

char *line = NULL;

size_t len = 0;

if (getline(&line, &len, fp) == -1) {

printf("Error in reading the file ");

exit(1);

}

char *token = NULL;

token = strtok(line, COMMA);

*rows = atoi(token);

token = strtok(NULL, COMMA);

*cols = atoi(token);

}

/* TODO:

* Returns 1 if and only if there exists at least one pair

* of queens that can attack each other.

* Otherwise returns 0.

*

* board: heap allocated 2D board

* rows: number of rows

* cols: number of columns

*/

int check_queens(int **board, int rows, int cols) {

return 0;

}

/* PARTIALLY COMPLETED:

* This program prints true if the input file has any pair

* of queens that can attack each other, and false otherwise

*

* argc: CLA count

* argv: CLA value

*/

int main(int argc, char *argv[]) {

//TODO: Check if number of command-line arguments is correct.

//Open the file and check if it opened successfully.

FILE *fp = fopen(*(argv + 1), "r");

if (fp == NULL) {

printf("Cannot open file for reading ");

exit(1);

}

//Declare local variables.

int rows, cols;

//TODO: Call get_dimensions to retrieve the board dimensions.

//TODO: Dynamically allocate a 2D array of dimensions retrieved above.

//Read the file line by line.

//Tokenize each line wrt comma to store the values in your 2D array.

char *line = NULL;

size_t len = 0;

char *token = NULL;

for (int i = 0; i < rows; i++) {

if (getline(&line, &len, fp) == -1) {

printf("Error while reading the file ");

exit(1);

}

token = strtok(line, COMMA);

for (int j = 0; j < cols; j++) {

//TODO: Complete the line of code below

//to initialize your 2D array.

/* ADD ARRAY ACCESS CODE HERE */ = atoi(token);

token = strtok(NULL, COMMA);

}

}

//TODO: Call the function check_queens and print the appropriate

//output depending on the function's return value.

//TODO: Free all dynamically allocated memory.

//Close the file.

if (fclose(fp) != 0) {

printf("Error while closing the file ");

exit(1);

}

return 0;

}

and below is the test file check3.txt

4,5 1,0,0,0,0 0,1,0,0,0 0,0,0,0,1 1,0,0,0,0

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

Fundamentals Of Database System

Authors: Elmasri Ramez And Navathe Shamkant

7th Edition

978-9332582705

More Books

Students also viewed these Databases questions