My C program is not giving desired output - c

I am learner and new to C. I am making a number guess game using C but it is not executing properly, my if statement is not getting executed in this code please help out.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int num, guess, count;
count = 1;
srand(time(0));
num = (rand()%10 + 1);
do
{
printf("Guess the number: \n");
scanf("%d", guess);
if(guess>num)
{
printf("too large");
}
else if(guess<num)
{
printf("too small");
}
else
{
printf("you won!\nIn %d count.", count);
}
count++;
}while(guess != num);
return 0;
}
Code was supposed to give output as
Guess the number
5
too small!
Guess the number
7
You won in 2 count
But it is not executing if else statement and breaking the loop after scanf function. Please help.

Your scanf is incorrect:
scanf("%d", guess); // should be scanf("%d", &guess);

There is no problem with your if-statement. The problem is that scanf() takes a format string and a pointer, but not a format and a non-pointer variable.

Related

Program start-over function

I'm a beginner in C programming and i would appreciate if i could get some tips on how to set a program to restart? I'm currently building a guessing game, where the user has 10 attempts to guess the secret number which is provided randomly. I want the program to be able to offer the user a new round of game from start (Attempt number 1 Guess the number:), meaning re-run the program.
Here is the program:
#include <stdlib.h>
#include <time.h>
#define guessLimit 10
int main()
{
int secret_number;
int guess;
int guessCount = 0;
int outofGuesses = 0;
int i;
setbuf(stdout, NULL);
srand(time(0));
secret_number = rand() % 100;
printf("\n---GUESS THE SECRET NUMBER---\n");
for(i=1; i < 11; i++){
printf("Attempt number %d Guess a number: ", i);
scanf("%d", &guess);
if(guess == secret_number){
printf("Correct number!\n");
break;
}
if(guess < secret_number){
printf("sorry, number too small.\n");
}
else if(guess > secret_number){
printf("Sorry, number too big.\n");
}
if(i==10){
printf("Out of Attempts");
}
if(guess>99 || guess<0){
printf("Out of Range.\n");
}
}
return 0;
}
You could encapsulate your for loop in a while loop and have the conditional be an input from the console to indicate the user is done playing.
The best thing to do is to wrap the primary routine within a while loop and use a condition to determine if you want to either repeat or exit the loop. In this case, the do while construct works nicely. Simply ask the user if they would like to play again at the end of the loop. If not, then exit. Otherwise, repeat the code. Be mindful not to call srand(time(0)) within your loop or you reset the random sequence.
#include <stdlib.h>
#include <time.h>
#define guessLimit 10
int main()
{
int secret_number;
int guess;
int guessCount = 0;
int outofGuesses = 0;
int i;
char play;
srand(time(0));
do {
secret_number = rand() % 100;
printf("\n---GUESS THE SECRET NUMBER---\n");
for(i=1; i < 11; i++){
printf("Attempt number %d Guess a number: ", i);
scanf("%d", &guess);
if(guess == secret_number){
printf("Correct number!\n");
break;
}
if(guess < secret_number){
printf("sorry, number too small.\n");
}
else if(guess > secret_number){
printf("Sorry, number too big.\n");
}
if(i==10){
printf("Out of Attempts");
}
if(guess>99 || guess<0){
printf("Out of Range.\n");
}
}
printf("\nPlay again? (y/n): ");
scanf(" %c", &play);
} while (play == 'y');
return 0;
}
As a side note - giving the user 10 chances to guess a number in the range 1-100 is too generous if you're providing "higher/lower" feedback. If my calculations are correct, a binary search would find the answer in maximally log2(100)=6.64... attempts. In other words, you should be able to find the answer in no more than 7 attempts if you know what you're doing. A binary search works of course by guessing the number in between the bounds and then adjusting the bounds according to your feedback.

C Loop until the condition is met problem

I want to write a loop that runs until the user enters a number greater than 10, but I have to do something wrong because it creates an infinite loop.
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
for(int i=0;a<10;i++){
printf("Enter value>10");
i++;
printf("%d",&a);
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
You mix an index that does not make sense. Also you print the memory address of variable instead of its value, not sure it is what you wanted?
Code partially corrected (because I don't know what is your ultimate goal):
#include <stdio.h>
int main()
{
int a;
do {
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
printf("\na: %d\n",a);
} while (a <= 10);
printf("Result:%d\n",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
ps: \n is line return and added do while which is what you want when you want to execute a loop at least once.
Have a look at your for-loop: you let i start at zero, you continue until a is not smaller than ten anymore, but it's not the value of a you need to check, it's the one of i.
In top of that, you are doing a i++ within your for-loop, while this is already covered in the definition of the for-loop.
I think this is the code that you are looking for: See comments
#include <stdio.h>
int main()
{
int a, ok = 0, end_of_input = 0;
do {
printf("Please input an integer value (min. 10): ");
fflush(stdout); // So the user can see the above line!
switch(scanf("%d",&a)) {
case EOF: // End of input - Give up!
end_of_input = 1;
break;
case 1: // Got a number - Check it!
if (a < 10)
{
ok = 1;
} else {
printf("%d - Not appropriate input. Please try again.\n\n",a);
}
break;
default: // Summat else - "eat" the input to the next line
scanf("%*[^\n]\n"); // "eats" the rest of the line in the buffer w/o assignment
break;
}
} while (end_of_input == 0 || ok == 0);
if (ok) { // User entered a valid number
printf("Got a that is smaller than ten %d\n", d);
} else { // We have ran out of input
printf("See you want to leave us :-(\n");
}
return 0;
}
I am not sure what you are trying to achieve but one problem that I found in your logic is you prompting user for input outside the loop. So whenever you enter number less than 10 it always goes in infinite iteration.
Try following code, with scanf inside loop
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
int i=0;
for(;a<10;){
printf("Enter value>10");
scanf("%d",&a);
printf("%d",a);
i++;
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}

C - What is wrong with this code, it does not continue after user entered his guess?

as you saw up there the problem is that the loop does not continue after the users has entered his code, i am wondering why this is and if you have a better purpose for me. I am new to the C language help is much appreciated!!!!!!
#include <stdlib.h>
#include <stdio.h>
int main()
{
int randomNumber = 11;
int usersGuess;
int i;
do {
printf("You need to guess a number between 0 and 20! Good Luck! \n");
for (i = 5; i > 0; i--) {
printf("You have got %d amount of tries, Guess The random number: ", i);
scanf_s("%d", usersGuess);
if (usersGuess == randomNumber) {
printf("You won");
break;
} else if (usersGuess > randomNumber) {
printf("That is wrong, random number is less than that");
} else if (usersGuess < randomNumber) {
printf("that is wrong, the random number is higher than that");
} else if (usersGuess > 20) {
printf("please guess again cause the random number is between 0 and 20");
}
}
} while(i > 0);
return 0;
}
Your code has Undefined Behaviour, which means it's buggy and anything can happen. The problem is that you're passing an integer to scanf_s where it want a pointer. Do this:
scanf_s("%d", &usersGuess);
The reason is that you want the function to write into the variable usersGuess. In C, all parameters are passed by value, so if you want an output parameter, you have to make it a pointer.

palindrome number - compiler neither compiling code nor showing any error

can anyone help me this c program.i am trying to execute this palindrome check code but its not getting executed after i enter number.is there any error?
#include<stdio.h>
int main()
{
int num,rev=0,r,temp;
printf("enter the number: ");
scanf("%d",&num);
temp=num;
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
temp=temp/10;
}
if(num==rev)
{
printf("the number is palindrome %d: ",temp);
}
else
{
printf("%d is not a palindrome",temp);
}
return 0;
}
the block shows nothing, neither it stops executing.i tried it in code block and some online websites.
I believe you have an infinite loop here as num does not change within the loop, so the outcome of num>0 never changes:
while(num>0)
{
r=num%10;
rev=(rev*10)+r;
temp=temp/10;
}
#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
i still don't get this issue but its working perfectly.

Why is the line 12 is printed twice?

Given Problem
Implement the binary search algorithm in a non-recursive manner.
Keep the search array as a numeric array, initialized at time of declaration and keep it global.
The program should ask for a value to search, and then tell the location where it is found.
If the value is not found the program should display not found.
* Additionally the program should display the total number of comparisons done to locate the value ( or realize that the value was not found)
My Solution
#include<stdio.h>
int arr[]={1,3,4,6,8,9,10,15,17,21};
int bi_search(int n)
{
int start=0,end=9,mid=0,count=0;
while(start<=end)
{
count++;
mid=(start+end)/2;
if(n==arr[mid])
{
printf("\nThe total number of comparisons done to locate the value--%d\n",count);
return mid;
}
else if(n<arr[mid])
end=mid-1;
else
start=mid+1;
}
printf("\nThe total number of comparisons done to realize that the value was not found--%d\n",count);
return-1;
}
main()
{
int n=0,ch=0;
do
{
printf("\nEnter the value you want to search for--\n");
scanf("%d",&n);
if(bi_search(n)==-1)
printf("\nSORRY :( !! \nThe value was not found.");
else
printf("\nHurray :) !! \nThe value you entered found at %d position", bi_search(n)+1);
printf("\nEnter:-\n\t1 to continue.\n\t2 to terminate.\n");
scanf("%d",&ch);
}while(ch==1);
printf("\nThank You\n");
return 0;
}
When I run this code, in function bi_search(int n) whenever arr[mid] becomes equal to n, line The total number of comparisons done to locate the value-- is getting printed twice.
You are calling the bi_search function twice. Once in your if statement, and again in a printf statement. You should call it only once, and cache the value.
main()
{
int n=0,ch=0;
do
{
printf("\nEnter the value you want to search for--\n");
scanf("%d",&n);
if(bi_search(n)==-1) // Calling it here
printf("\nSORRY :( !! \nThe value was not found.");
else
printf("\nHurray :) !! \nThe value you entered found at %d position", bi_search(n)+1); // And here
printf("\nEnter:-\n\t1 to continue.\n\t2 to terminate.\n");
scanf("%d",&ch);
}while(ch==1);
printf("\nThank You\n");
return 0;
}
It is just because you are calling binary search function twice.
One on if part and other on else if part.
Alternatively you can do the same in following way
int temp = bi_search(n);
if (temp == -1)
printf("Value not found\n");
else
printf("Value found at position %d\n", temp);
Your main routine can look like this:
int main()
{
int n = 0, ch = 0;
do
{
printf("\nEnter the value you want to search for--\n");
scanf("%d", &n);
int res = bi_search(n); // store the result
if (res == -1) // check if not find
printf("\nSORRY :( !! \nThe value was not found.");
else
printf("\nHurray :) !! \nThe value you entered found at %d position", res + 1); // print if find
printf("\nEnter:-\n\t1 to continue.\n\t2 to terminate.\n");
scanf("%d", &ch);
} while (ch == 1);
printf("\nThank You\n");
return 0;
}
1) int main() (to make compiler calm)
2) int res = bi_search(n) (to avoid second bi_search() call)

Resources