Question

In: Computer Science

C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h. Need to...

C program. librarys that are in use; stdio.h, stdlib.h, time.h, stddef.h, ctype.h, math.h, string.h.

Need to rewrite the functions linked in the code: "return add_move_lib ( board, col, colour ) ;" "return board_full_lib ( board ) ;" "return display_board_lib ( board ) ;

// TASKS

// board_full() display_board() add_move()

// adds a token of the given value (1 or 2) to the board at the

// given column (col between 1 and COLS inclusive)

// Returns 0 if successful, -1 otherwise

int add_move ( int board[COLS][ROWS], int col, int colour )

{

// check that the column isn't full - if it is, return -1 and abort

// start at the bottom of the board, and move upwards in the specified column until an empty spot is found

// put a token of the specified colour at that location

return add_move_lib ( board, col, colour ) ;

}

// determines if the board is completely full or not.

// Return TRUE if full, false otherwise

int board_full ( int board[COLS][ROWS] ){

// loop through each column

// if a column ISN'T full, return 0 (board can't be full if a single column isn't

// if all columns are checked and full, return 1

return board_full_lib ( board ) ;

}

// displays the board to the screen

int display_board ( int board[COLS][ROWS] ){

// loop through each row and column of the board and display in appropriate format

// use | and - characters to draw boundaries of the board, and put numbers at the bottom to indicate the column numbers

return display_board_lib ( board ) ;

}

Solutions

Expert Solution

// adds a token of the given value (1 or 2) to the board at the

// given column (col between 1 and COLS inclusive)

// Returns 0 if successful, -1 otherwise

int add_move ( int board[COLS][ROWS], int col, int colour )

{

// check that the column isn't full - if it is, return -1 and abort

// start at the bottom of the board, and move upwards in the specified column until an empty spot is found

// put a token of the specified colour at that location

return add_move_lib ( board, col, colour ) ;

}

// helper function to add a token of the given value (1 or 2) to the board at the given column

int add_move_lib ( int board[COLS][ROWS], int col, int colour )

{

       // check if valid column

       if(col < 1 || col > COLS)

       {

             return -1;

       }else

       {

             int i;

             // get the first empty spot from bottom

             for(i=ROWS;i>0;i--)

             {

                    if((board[col][i] != 1) && (board[col][i] != 2))

                    {

                           board[col][i] = colour;

                           return 0;

                    }

             }

             return -1; // non-empty column

       }

}

// determines if the board is completely full or not.

// Return TRUE if full, false otherwise

int board_full ( int board[COLS][ROWS] ){

// loop through each column

// if a column ISN'T full, return 0 (board can't be full if a single column isn't

// if all columns are checked and full, return 1

return board_full_lib ( board ) ;

}

// helper function to check if board is empty or not

int board_full_lib ( int board[COLS][ROWS] )

{

       int i,j;

       for(i=1;i<=COLS;i++)

       {

             for(j=1;j<=ROWS;j++)

                    if((board[i][j] != 1) && (board[i][j] != 2))

                           return 0;

       }

       return 1;

}

// displays the board to the screen

int display_board ( int board[COLS][ROWS] ){

// loop through each row and column of the board and display in appropriate format

// use | and - characters to draw boundaries of the board, and put numbers at the bottom to indicate the column numbers

return display_board_lib ( board ) ;

}

// helper function to display the board on the console

int display_board_lib ( int board[COLS][ROWS] )

{

       int i,j;

       for(i=1;i<=COLS;i++)

       {

             printf("\n|");

             for(j=1;j<=ROWS;j++)

             {

                    if(board[i][j] == 1 || board[i][j] == 2)

                           printf("%d|",board[i][j]);

                    else

                           printf(" |");

             }

             printf("\n");

             if(i < COLS)

             {

                    for(j=0;j<2*ROWS;j++)

                           printf("-");

             }

       }

       return 0;

}


Related Solutions

Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I...
Use C language , pointer limit use //#include <stdio.h> //#include <stdlib.h> //#include <time.h> For example, I have random array [4,2,7,1,9,8,0]. Sort the array's index but cannot change the position of item in the array, if you copy the array to a new array, you also cannot change the item's position in array. The index now is[0,1,2,3,4,5,6] The output should be[6,3,1,0,2,5,4]
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b...
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b 6 #define t 6 double bmical(double w, double h){ double o; double bmi; o = pow(h,2); bmi = w/o; return bmi; } double maxheartrate(int num1, int age){ double mhr; mhr = num1 - age; return mhr; } double heartratereserve(double mhr, double rhr){ double hrr; hrr = mhr - rhr; return hrr; } double minrange(int hrr, int rhr){ double mirt; mirt = (hrr * 0.7)...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu...
LANGUAGE: C Only using <stdio.h> & <stdlib.h> Write a program that gives the user a menu to choose from – 1. Convert temperature input from the user in degrees Fahrenheit to degrees Celsius 2. Convert temperature input from the user in degrees Celsius to degrees Fahrenheit 3. Quit. Formulae you will need: C = (5 / 9) * (F-32) and F = (9/5) * C + 32 1. Use functions to accomplish 1 and 2 above. 2. Use at least...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
converting strings to floats without using stdlib.h or string.h
converting strings to floats without using stdlib.h or string.h
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT