Question

11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single user-defined function that analyzes a 2D array of cthe following values should be returned by win0 if called as: win(board A) returns 0 win(board,B) returns 0 win(board,D) retumain.c Load default template... 1 2 #include<stdio.h> #include<stdlib.h> 4 int win(char board[ 7][5], char player) i 5 I/ ins32 printf(n); 34 35 36 printf( Enter the uesrs token to see if they won the game!); 37 scanf&user) 38 printf(Inln); 39

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

C code

#include<stdio.h>
#include<stdlib.h>

int win(char board[7][5], char player){

//looping variables
int i,j;
//checking consecutive positions in a row

//for looping through the rows
for(i=0;i<7;i++){
//for looping through the columns
//in this loop 3 elements are bring compared at once
//so the loop starts from 1 and ends at 3
//because j-1,j and jth elements are being considered
for(j=1;j<4;j++){
//if the given character is present in these 3 consecutive positions
//then 1 will be returned by the function
//in the if condition the row variable 'i' is same
//and the column variable 'j' is changed for checking the values in a single row
if( (board[i][j-1]==player) && (board[i][j]==player) && (board[i][j+1]==player)){
return 1;
}
}
}

//now we check for consecutive positions in a column

//for looping through the column
for(i=0;i<5;i++){
//for looping through the columns
//as 3 elements are being checked at once
//so the loop will run from 1 to 5
for(j=1;j<6;j++){
//here the column variable 'i' is kept constant
//and the row variable 'j' is changed for checking values
//in a single column
//if three consecutive values are found then 1 is returned
if( (board[j-1][i]==player) && (board[j][i]==player) && (board[j+1][i]==player)){
return 1;
}
}
}
//if no value is returned from the execution of above loops
//then the given character is not present consecutively
//so 0 will be returned
return 0;
}

int main() {
//initialize the array
char board[7][5];
int i,j;
char user;
int gameresult;

//read the board
printf("Enter 35 values for the 7X5 game board\n");
for(i=0;i<7;i++){
for(j=0;j<5;j++){
scanf(" %c",&(board[i][j]));
}
}

//print the board
for(i=0;i<7;i++){
for(j=0;j<5;j++){
printf(" %c",(board[i][j]));
}
printf("\n");
}

printf("Enter the user's token to see if they won the game!");
scanf(" %c",&user);
printf("\n\n");

gameresult = win(board,user);

if(gameresult==1)
printf("Player %c won the game!",user);
else if(gameresult==0)
printf("Player %c didn't win.",user);
else
printf("Invalid response from win()\n");

return 0;

}

***Important Note***

One very important that needs to be taken care of here is how you take character input.

The %c conversion specifier won't automatically skip any leading white space, so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately.

This problem can be solved by putting a white space before the conversion specifier in format string.

scanf(" %c", &c);

The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.

SnapShots of the code

# include<stdio.h> # include<stdlib . h> int win (char board[7] [5], char player) t hooping variables int i,j //checking cons//now we check for consecutive positions in a colum //for looping through the colum for (i=0;i< 5 ; i++) { //for looping throint main) initialize the array char boardt7] 151: int i,j: char user; int gameresult; //read the board printf (Enter 35 valuif (gane result=1) printf (Player %c won the game!, user); printf( Player %c didnt win., user); printf (Invalid respons

Output

Enter 35 values for the 7X5 game board A B B DE ZABGG ZBAGG ZZX GK A B B DE A B B DE Z A BGG Z BAGG A B B DE Enter the usersEnter 35 values for the 7X5 game board A B B DE ZABGG ZBAGG ZZX GK A B B DE A B B DE Z A BGG Z BAGG A B B DE Enter the users

Add a comment
Know the answer?
Add Answer to:
11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single...
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
  • In basic C please with comments so I can redo It on my own, thank you!...

    In basic C please with comments so I can redo It on my own, thank you! For this lab, you only need to write a single user-defined function that analyzes a 2D array of chars. You do NOT need to write any of the code in main() that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win() with the following definition (i.e. prototype): int win(char board[7][5], char player)...

  • home / study / engineering / computer science / computer science questions and answers / in...

    home / study / engineering / computer science / computer science questions and answers / in basic c please with comments so i can redo it on my own, thank you! for this lab, you only ... Question: In basic C please with comments so I can redo It on my own, thank you! For this lab, you only nee... In basic C please with comments so I can redo It on my own, thank you! For this lab, you...

  • 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...

  • 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...

  • 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...

  • For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays...

    For this exercise, you will complete the TicTacToe Board that we started in the 2D Arrays Lesson. We will add a couple of methods to the TicTacToe class. To track whose turn it is, we will use a counter turn. This is already declared as a private instance variable. Create a getTurn method that returns the value of turn. Other methods to implement: printBoard()- This method should print the TicTacToe array onto the console. The board should include numbers that...

  • My homework is to write a tic tac toe function, this code isn't working and I...

    My homework is to write a tic tac toe function, this code isn't working and I can not find the error. It prints the board and accepts user input just fine, the only problem is the checkBoard function. I can not get it to return a value(player win) to end the while loop. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> int checkBoard(char board[3][3]) {    int i, j;    for (i = 0; i < 3; i += 1) {        if...

  • Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board...

    Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...

  • Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am...

    Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am lost on how to complete the main function (play_card_match) without the sub functions complete. The program runs fine as is, but does not actually have a working turn system. It should display question marks on unflipped cards when the player is taking a turn and should also clear the screen so the player can't scroll up to cheat. Thank you #include "cardMatch.h" //main function /*...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

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