
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
int main()
{
/* first you have to let the computer generate a random number.
Then it has to declare how many tries the user has for the game
loop.
we then need the player to enter their guess. After every guess we
have to give an output of how many numbers they have in the right
location
and how many they have the right number. The player will keep
guessing until their 10 tries are over. Then we ask them if they
want
to play again*/
/*the whole game loop begins here, when it’s a nonzero value its
always true. In this loop the computer will generate a random
number, the user will keep guessing and then at the end the user
will be asked if they want to play again*/
while(1){
int i, j, k;
int randNum, count = 0; //variables used to make sure there are no
duplicates
int sec_code[4], userNum[4]; /*sec_code is the number
generated by the computer and userNum is number generated by the
user*/
int correct_location, correct_number; //used to notify the user for
their reults
printf("welcome to the game of MasterMind\n");
// Generation of the four digit code
srand(time(0));
/*here the non-duplicated code will randomly be generated, count is
less than 4 because we have 4 numbers but counting starts at
0*/
while(count <
4){
randNum = rand()%9 + 1;
sec_code[count] = randNum;
// check if this randNum has already been put into sec_code
for(i = 0; i < count; i++){
/*here is when the computer checks if the random number is equal to
the counter. (it will check all the previous counter to make sure
there are no duplicates)*/
if(randNum == sec_code[i]){
count--;
break;
}
}
count++;
}
//this is the tries
loop, after each guess it will come to this loop to until they get
it right or 10 tries are over//
for (i=0; i<10; i++){
printf("\n\nAttempt: %d", i+1);
// Initialize correct_location and correct_number for each
attempt
correct_location = 0;
correct_number = 0;
printf("\nEnter 4 digits with spaces in between:");
for(j=0; j<4; j++) {
scanf("%d",&userNum[j]);
}
// here we are calculating correct_location and correct_number, to
output
for(j=0; j<4; j++) {
for(k=0; k<4; k++) {
if(j == k && sec_code[k] == userNum[j]) {
correct_location++;
}
else if(userNum[j] == sec_code[k] && j > 0 &&
userNum[j-1] != userNum[j]) {
correct_number++;
}
}
}
/*this is when we check whether they have the right guess in less
than 10 tries or if their 10 tries are over to output a message for
them*/
if(correct_location==4) {
printf("\n\n *** Congratulation ! you break the code ...");
break;
}
if (correct_location !=4 && i<9){
printf("\n\nCorrect Location: %d", correct_location);
printf("\nCorrect Number: %d", correct_number);
}
else{
printf("\n\nSorry, you cannot break the code !.\n you have no more
chances");
// Displaying the original code
for(j=0; j<4; j++){
printf(" %d",sec_code[j]);
}
}
//the tries for loop will ends here
}
char playAgain; //the character for the place holder
for if they want to play again or not
scanf("%c", &playAgain);
printf("Would u like to play again? enter y or any other character
to stop playing?");
scanf("%c", &playAgain);
if(playAgain != 'y'){
break;
/*if the user presses y it will go to the top of the game and let
them play again. Otherwise any other character they press will be
indicated that they don’t want to play again*/
}
//the while loop for the whole game will end here
}
return 0;
}
THIS IS MY CODE I JUST NEED HELP WITH THE FUCTIONS AND THE SECOND
MODIFICATION THAT HAS TO BE DONE THANK YOU
also if you can look through the rest of my original code and make sure you make adjustments to the functions you added so the correct messages are printed
hey thank you soo much for your help but please make sure you use my original code to make the two adjustments. The changes have to be made around my original code
// C code for above problem statement
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int found(int a[],int size,int value) // search for value in array,
if present return its index else return -1
{
int i;
for(i=0;i<size;i++)
{
if(a[i]==value)
return i;
}
return -1;
}
int uniqueRand(int random_numbers[],int size) // method that
generates unique random numbers of given size
{
srand(time(0));
int i=0;
while(i<size)
{
int random_number=rand()%10; // creates a random number
if(random_number>=1 && random_number<=9 &&
found(random_numbers,size,random_number)==-1)
// if number is in between 1 & 9 and must not be present in
array previously, then add into array
{
random_numbers[i]=random_number;
i++;
}
}
return 1;
}
int duplicatesFound(int arr[],int size) // method that checks for
duplicates
{
int i,j;
for(i=1;i<=9;i++)
{
int count=0;
for(j=0;j<size;j++)
{
if(arr[j]==i)
count++;
}
if(count>=2)
return j;
}
return -1;
}
int main()
{
printf("Welcome to the Mastermind Game, Good luck\n");
while(1)
{
int len;
printf("Enter number of digits to be hidden(Enter number in between
1 to 7):");
scanf("%d",&len);
if(len>=1 && len<=7)
{
int random_numbers[len],i,j;
uniqueRand(random_numbers,len);
printf("Enter number of guesses you want from 1-100:");
scanf("%d",&i);
if(i>=1 && i<=100)
{
for(;i>0;i--)
{
printf("You have %d tries to guess\n",i);
printf("Enter your numbers:");
int userNum[len];
for(j=0;j<len;j++)
scanf("%d",&userNum[j]); // take user numbers as input
if(duplicatesFound(userNum,len)!=-1) //if all are not unique then,
print error message
{
printf("Invalid numbers\n");
}
else
{
// correct_numbers indicate numbers that are present in array but
not in correct position
// correct_location indicate that number present in array along
with correct location or index
int index,correct_numbers=0,correct_location=0;
for(j=0;j<len;j++)
{
index=found(random_numbers,len,userNum[j]);
if(index!=-1)
{
if(index==j)
correct_location++;
correct_numbers++;
}
}
if(correct_location==len) // if all the numbers are guessed
correctly, then user is the winner
{
printf("Congratulations... You have won the game!!!!!\n");
break;
}
printf("Correct numbers: %d\t Correct Positioned Numbers:
%d\n",correct_numbers,correct_location);
// print hints to user about correct numbers and correct positioned
numbers
}
}
if(i==0) // if user is unable to guess numbers in 10 chances, then
print he is as looser and print unique random numbers
{
printf("Sorry, you lost the game!!!!\nCorrect numbers are:
");
for(i=0;i<len;i++)
printf("%d ",random_numbers[i]);
printf("\n");
}
char playAgain; //the character for the place holder for if they
want to play again or not
scanf("%c", &playAgain);
printf("Would u like to play again? if YES enter y else enter any
other character? ");
scanf("%c", &playAgain);
if(playAgain!='y')
break;
}
else
{
printf("Invalid number of guesses input\n");
}
}
else
{
printf("Invalid length input\n");
}
}
}
Sample output:


#include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...
#include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } } return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...
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...
C Programming #include <stdio.h> #include <stdlib.h> #include <time.h> // These defines do a text replacement // everytime the string 'ROWS' and 'COLUMNS' // are found in this specific source file. // You can play with these values. #define ROWS 5 #define COLUMNS 5 /* ============= Tutorial on Graph format ============ You are given a randomly generated graph that looks of the form: 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0...
In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps: 1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m. 2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1]. 3. The codebreaker is prompted to enter a guess, an n-digit sequence....
please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
#include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....
sort.c
#include <stdlib.h>
#include <stdio.h>
#include "libsort.h"
int main()
{
int* array;
int size, c;
float median;
printf("Enter the array size:\n");
scanf("%d", &size);
array = (int*) malloc(size *
sizeof(int));
printf("Enter %d integers:\n", size);
for (c = 0; c < size; c++)
scanf("%d",
&array[c]);
sort(array, size);
printf("Array sorted in ascending
order:\n");
for (c = 0; c < size; c++)
printf("%d ",
array[c]);
printf("\n");
median = find_median(array,...
#include <stdio.h> #include<string.h> int main() { char strText[100] ="Start"; char i; int nTextASCIISum = strText[0] + strText[1] + strText[2]; int nTextLen = strlen(strText); int count = 0; printf("Welcome to token generator!\n"); printf("Enter a word to use in the token generator.\n You may enter as many words as you like. \n Press q and key when finished.\n"); scanf("%c", &strText[i]); //compute when to stop loop //check nTextLen == 1 and strText[0] == 'q' for(i=0;i< nTextLen;i++) if ((strlen(strText) != 1) || (strText[0] !=...
In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...