Question

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 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)

The function win() should return a 1 if the character player is found in three consecutive positions in the board. Consecutive for this lab means in the same row or in the same column, NOT on a diagonal.

For the board shown below,

A    B   B   D   E
Z    A   B   G   G
Z    B   A   G   G
Z    Z   X   G   K
X    X   O   K   O
X    X   O   O   W
A    B   B   D   E

the following values should be returned by win() if called as:

  • win(board,'A') returns 0
  • win(board,'B') returns 0
  • win(board,'D') returns 0
  • win(board,'G') returns 1
  • win(board,'O') returns 0
  • win(board,'X') returns 0
  • win(board,'Z') returns 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<stdio.h>

//required method

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

                //declaring an int variable to store the count of consecutive occurrence

                //of the player value

                int count;

                //checking horizontally (row wise)

                //looping through each row

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

                                //setting count to 0

                                count=0;

                                //looping through each column

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

                                               //if character at current location is player, incrementing count

                                               if(board[i][j]==player){

                                                               count++;

                                                               //if count reach 3, returning 1 (found)

                                                               if(count==3){

                                                                               return 1;

                                                               }

                                               }else{

                                                               //if the character is not player, resetting count to 0

                                                               count=0;

                                               }

                                }

                }

               

                //checking vertically (column wise)

                //looping through each column

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

                                //setting count to 0

                                count=0;

                                //looping through each row

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

                                               if(board[i][j]==player){

                                                               count++;

                                                               if(count==3){

                                                                               return 1;

                                                               }

                                               }else{

                                                               count=0;

                                               }

                                }

                }

                return 0; //not found

}

int main(){

                //testing

                char board[7][5]={

                {'A','B','B','D','E'},

                {'Z','A','B','G','G'},

                {'Z','B','A','G','G'},

                {'Z','Z','X','G','K'},

                {'X','X','O','K','O'},

                {'X','X','O','O','W'},

                {'A','B','B','D','E'}};

               

                printf("%d\n",win(board,'A')); //0

                printf("%d\n",win(board,'B')); //0

                printf("%d\n",win(board,'D')); //0

                printf("%d\n",win(board,'G')); //1

                printf("%d\n",win(board,'O')); //0

                printf("%d\n",win(board,'X')); //0

                printf("%d\n",win(board,'Z')); //1

                return 0;

}

/*OUTPUT*/

0

0

0

1

0

0

1

Add a comment
Know the answer?
Add Answer to:
home / study / engineering / computer science / computer science questions and answers / in...
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)...

  • 11.9 Week 11 Lab: 2D Arrays For this lab, you only need to write a single...

    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 chars. You do NOT need to write any of the code in main0 that calls your function. Make sure to use the template and only add code to the user-defined function. Write a function win0 with the following definition (i.e. prototype): int win(char board17)15), char player) The function win0 should return a 1 if the character...

  • home / study / engineering / computer science / computer science questions and answers / l(a)...

    home / study / engineering / computer science / computer science questions and answers / l(a) be the language generated by g(a) - (n, 2, s, p) where 2 - [a, b), n= {s,x) and s->axb ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: L(a) be the language generated by g(a) - (n, 2, s, p) where 2 - [a, b), n= {s,x) and s->axb ..... l(a) be the...

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

    home / study / engineering / computer science / computer science questions and answers / Write A Second Game Program That Prints A Chart To The Screen Showing The Randomness Of A Die. ... Question: Write a second game program that prints a chart to the screen showing the randomness of a die. Th... Write a second game program that prints a chart to the screen showing the randomness of a die. The game should first prompt the client for...

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

    home / study / engineering / computer science / computer science questions and answers / this is a data modeling exercise i am assigned a project as a dba to design a data model to ... Question: This is a Data Modeling exercise I am assigned a project as a DBA to design a data model to suppo... This is a Data Modeling exercise I am assigned a project as a DBA to design a data model to support a...

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

    home / study / engineering / computer science / computer science questions and answers / write a python program that generates a list of 50 random numbers. the program should display ... Question: Write a python program that generates a list of 50 random numbers. The program should display (wi... Write a python program that generates a list of 50 random numbers. The program should display (with labels) the sum, average, largest, and smallest of the list along with the...

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

    home / study / engineering / computer science / computer science questions and answers / customer customerid,lastname,firstname, address,city,state,zip,email houses houseid, price ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Customer Customerid,lastname,firstname, address,city,state,zip,email Houses Houseid, price range,... Customer Customerid,lastname,firstname, address,city,state,zip,email Houses Houseid, price range, regionid, housename, bedroom no., pool, address, city,state,zip Reservations reservationid, customerid, date reservation, discount, start date, end date House amenities houseid, amenities id Price Range- houseid,price,start date,...

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

  • Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...

    Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won. The board will be read in as 3x3 array of characters (x, o or . for a blank spot). You are required to write the following functions to help solve the problem: // input prompts the user for a board and inputs it // into the given array void input (char board [3][3]); // print prints...

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

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