Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

pac... Heading1 Heading 2 Title Subtitle Subtle Em. Emphasis IntenseE. Strong Qu

ID: 3881506 • Letter: P

Question

pac... Heading1 Heading 2 Title Subtitle Subtle Em. Emphasis IntenseE. Strong Quote Styles Game Logic Your program will run and generate 25 random numbers between 1 to 100 (no repeats). The game can be between two users or between you and computer. Both the players will get two guesses each time. Every correct guess will be counted as 1 correct answer and will be displayed on the screen in the board. The player having more number of correct guesses will be a winner. The game will continue until the board is completely revealed. 3. Steps: In this assignment, you will create a simple Number Guessing game designed for two two separate players or one player against the computer). You will need to create arrays and functions players (may be to complete this program. The first screen should ask the user for his name, print a welcome message, and displays an empty 5x5 board. But this Sx5 board has the values in it internally 1. Please Enter your nane lright Shansher t The Rutes 1se gets two Once alt Now Let's Play COMP 2130 INTRODUCTION TO COMPUTER SYSTEMS 2. 3, 4. Now the program should ask the number of players (1 or 2) one, ask one name and second is computer and f 2 ask two names For playing against computer, you are asked for two guesses and every correct guess is shown on the board. 5. Now 2 guesses for computer would be taken and shown on the screen

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

int numbers[25];

int flag[25];

int isInArray(int n,int size){

int i;

for(i=0;i<size;i++){

if(numbers[i]==n){

return i;

}

}

return -1;

}

void generateNumbers(){

int i,num;

for(i=1;i<=25;i++){

num = (rand()+1)%101;

if(isInArray(num,i)>-1){

i--;

}

else{

numbers[i-1]=num;

}

}

}

void showBoard(){

int i;

for(i=0;i<25;i++){

if(flag[i]==0){

printf(" 0");

}

else{

printf(" %d",numbers[i]);

}

if((i+1)%5==0){

printf(" ");

}

}

}

void checkWinner(int firstCount,char *first,int secondCount,char *second){

if(firstCount>secondCount){

print(" %s won by guessing more correct numbers !!",first);

}

else{

print(" %s won by guessing more correct numbers !!",second);

}

}

int main(){

int player,num,index,firstCount=0,secondCount=0;

char *first,*second;

printf("Enter number of Players: ");

scanf("%d",&player);

if(player<=0){

printf("Atleast one player, max two player can play this game !!");

return 0;

}

else if(player>2){

printf("Max two player can play this game !!");

return 0;

}

else{

printf("Enter 1st player name: ");

scanf("%s",first);

fflush(stdin);

printf(" Welcome %s !!",first);

if(player==2){

printf("Enter 2nd player name: ");

scanf("%s",second);

fflush(stdin);

printf(" Welcome %s !!",second);

}

else{

second = "Computer";

}

}

fflush(stdin);

while(1){

int i=0;

for(i=1;i<=2;i++){

printf("%s ... Please enter a number: ",first);

scanf("%d",&num);

if((index=isInArray(num,25))>-1){

flag[index]=1;

firstCount++;

}

showBoard();

}

for(i=1;i<=2;i++){

if(player==2){

printf("%s ... Please enter a number: ",second);

scanf("%d",&num);

}

else{

printf("%s played:",second);

num = (rand()+1)%101;

}

if((index=isInArray(num,25))>-1){

flag[index]=1;

secondCount++;

}

showBoard();

}

if((firstCount+secondCount)==25){

break;

}

}

checkWinner(firstCount,first,secondCount,second);

return 0;

}