Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C Programming - Problems

Options
  • 19-10-2011 9:31pm
    #1
    Closed Accounts Posts: 327 ✭✭


    First, I want to write a program that will allow a user to guess a predefined number. If the number is outside the range (1-10) then I want the program to offer a new guess.

    I've written this but the program offers a new guess whether the number is outside the range (eg, 22), or inside (eg. 2)
    #include<stdio.h>
    #define ANSWER 4 /*Define constant of ANSWER*/
    
    int main()
    
    {
    int guess;
    
    while 
    	(guess <= 1 && guess >= 10 ) /*While loop to determine if guess is within correct range*/
    	{
    
    printf("This is a number between 1 and 10. Enter your guess:"); /*Read guess from the keyboard*/
    scanf("%d" ,&guess); /*Read user input*/
    	
    
    if ( guess == ANSWER ) /*If clause to define if user's guess is correct*/
    {
    printf("You guessed correctly, the answer was 4."); /*If guess is correct this message will show*/
    }
    
    else if (guess < ANSWER) /*If clause to define if user's guess is smaller than ANSWER*/
    {
    printf("You guessed incorrectly, the answer is greater."); /*If guess is smaller this message will show.*/
    }
    
    else if (guess > ANSWER) /*If clause to to define if user's guess is greater than ANSWER*/
    {
    printf("You guessed incorrectly, the answer is smaller."); /*If guess is smaller then this message will show */
    }
    
    }
    
    return 0;
    }
    

    I need to then adapt it to read how many guesses the user has left when they get an answer wrong. i.e. (You have 2 guesses left).

    I believe I need something like this
    int guess(es)
    guess = 3;
    
    a new while clause around the whole body of ifs etc and after each incorrect guess message prinf("You have %d(es) left" ,guess)
    
    and at the bottom just before closing the while a guess=guess-1
    
    but obviously until I figure out the first code I can't get this one done. Be interesting to know if I'm on the right lines though?

    Then I need a program which puts out all the prime numbers between 1 and 100. I have this
    include <stdio.h>
    
    int main()
    {
    int num;
    for (num = 0; num <= 100; num++)
    {
    
    if ( num % 2 == 0 )
    {
    if ( num > 1)
    printf(",");
    
    printf("%d",num);
    }
    }
    
    return 0;
    }
    

    but it puts out even not prime numbers. Not being a maths whizzkid I don't know how to work out prime numbers (I know what one is, but not the equation) and I've spent hours trying to find it. Here's the added bit, it has to be done using a while loop.

    Then I need one that reads a number and sees if its prime. Obviously I need to get the above code cracked first then I'll come to that one.


Comments

  • Registered Users Posts: 545 ✭✭✭ravydavygravy


    On the primes, from wikipedia:
    The most basic method of checking the primality of a given integer n is called trial division. This routine consists in dividing n by each integer m which is greater than 1 and less than or equal to the square root of n. If the result of any of these divisions is an integer, then n is not a prime, otherwise, it is a prime


  • Registered Users Posts: 7,863 ✭✭✭The_B_Man


    so that while loop is going to loop while the number entered is 1 or less AND 10 or above?

    Also, maybe try initialise the guess variable.

    Also also, you're using "scanf" to scan in a double. Shouldnt it be %i instead of %d?

    i havent done C in nearly ten years so forgive me if I'm wrong.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

    You can generate primes that way.


    As for the first question, it should be %d alright and yeah that uninitialised variable is a bad idea! It will be a different value each time you run the program which means it could pass or fail the condition sometimes.

    I think this might be an easier way

    [PHP]
    while(true)
    {
    printf "enter a number between 1 and 10 plz, %d guesses left"
    scanf into var

    if( var == answer )
    bingo
    else if( var < 1 || var > 10 )
    msg about the range
    else
    guess amount -1

    if( guess amount == 0 )
    end game
    }
    [/PHP]


  • Registered Users Posts: 105 ✭✭damoth


    I've written this but the program offers a new guess whether the number is outside the range (eg, 22), or inside (eg. 2)

    To fix this, you need a closing '}' for the while loop, just after the scanf line. You also need to then remove the previous closing '}' for the while loop, which is just above the 'return 0' line.
    while (guess <= 1 && guess >= 10 )
    	{
                  printf("This is a number between 1 and 10. Enter your guess:"); 
                  scanf("%d" ,&guess);
            }
    

    Now, the program will repeatedly ask for a guess until the user enters one in the correct range. Only when the user enters a valid guess will the program move on and provide feedback on the guess.

    EDIT - also, as mentioned above - initialise guess to be some value outside the acceptable range (eg. int guess = -1) so that the code will enter the while loop on the first run.


Advertisement