Challenge 2!!

+1 Ossama Memon · December 24, 2015


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int sum = 0;
    int sum1 = 0;
    char g;
    int i;
    int j;
    int dice;
    int a;
    printf("Press 1 to enter the game\n");
    scanf(" %d", &a);
    if(a == 1){
        for(i=0; i<3; i++){
        dice = (rand()%6) + 1;
        printf("Random numbers are %d \n",dice);
        sum += dice;
    }
    printf("The sum of three rolls is %d\n", sum);
    for(j=0; j<3; j++){
        dice = (rand()%6) + 1;
        printf("Random numbers are %d \n", dice);
        sum1 += dice;
    }
    printf("Guess if upper, lower or same. (U/L/S) \n");
    scanf(" %c", &g);
    if(sum1 < sum){
        if(g == 'U'){
            printf("You guessed right..\n");
        }else{
            printf("Loser!!");
        }
        if(sum1 > sum){
            if(g = 'L'){
                printf("You guessed right..\n");
            }else{
                printf("Loser!!");
            }
            if(sum1 == sum){
                if(g == 'S'){
                    printf("You guessed right..\n");
                }else{
                    printf("Loser!!");
                }
            }
        }
    }

    }
    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Ossama Memon · December 30, 2015
Linguist Llama I googled it before your reply to know how actually srand works. But I couldn't understand it's syntax. What is time used for in srand?
0 Linguist Llama · December 30, 2015
Let's try another approach... Have you tried googling "How do I use srand"? Or would you like us to google for you?
0 Ossama Memon · December 29, 2015
Ok. Sorry for the noob question Laura but how to use srand function ?:P Llama I've watched the whole video again but looks like I haven't missed anything.
0 Ossama Memon · December 29, 2015
Alright. First let me use srand function and if nothing happens then I've to see the video again. Thanks Linguist Llama for pointing out my mistake and thanks Laura as well.
+1 Linguist Llama · December 28, 2015
Go back through the video and see what you have missed...
+1 Laura Lee · December 28, 2015
You will have to seed the random. In your case, using the srand function.
0 Ossama Memon · December 28, 2015
Linguist Llama I noticed that the random numbers are just 5 and 6 :'( ok. How to resolve this problem?
0 Linguist Llama · December 27, 2015
What have you noticed about the sequence of numbers that your program generates when you run it time and time again?
  • 1

C

107,210 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator