Hi, I know this is a C++ forum and I'm writing code in C, however since C and C++ have similarities I would like to know your insights on my program and how could I improve it in order to address the tasks given on the assignment.

Dice Rolling: Write a program that simulates the rolling of 3 dice (x,y,z), consider that each dice can give 1-12 values. The program should use rand() three times to roll the first, second and third die respectively. The sum of the three values should then be calculated.

A) Consider the sum of the three dice values as the first winner.

eg. x+y+z -----> first winner.

B) Consider the sum of two dice and subtracting the third as the second winner.

eg. x+y+z = winning sum;

x+y-z ----> second winner // but I'm confused since x+y-z is not the only existing combination.

// I can also have: x+z - y or y+z -x How would I establish a difference between this cases and also is there a way to check which combination is going to occur first?

C) Any negative value player has to pay double amount ----> Here I'm lost (Never played dice rolling maybe that's why I have no clue what it means) Does it has to do with the question B) ?

D) On a 3600 trial, map the most possible, and the least possible winning values for the first and second player. ----> I think they talk about the matrix for 2 dice the most possible winning value would be 7 and the least possible would be 2 and 12. Should I make a matrix and get the values?

E) Least winning combinations for the player.

Dice Rolling Game Assignment in C:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main (void)

{

int i, k, t, sum_total, win_sum;

int j, x, y, z,sum_xy, sum_xz, sum_yz;

printf("=============Random Dice Rolling=======\n");

srand(time(NULL));

for(j = 0; j<=3600; j++)

{

win_sum = 3 + rand()%7;

}

printf("Winning sum = %d\n\n", win_sum);

for (i = 1; i<=3600; ++i)

{

x = 1 + rand() % 3;

y = 1 + rand() % 3;

z = 1 + rand() % 3;

}

sum_total = x+y+z;

sum_xy = sum_total - z;

sum_xz = sum_total - y;

sum_yz = sum_total - x;

if (sum_total == win_sum)

{

printf(" 1st winner: %d + %d + %d = %d\n",x,y,z, win_sum);

}

if (sum_xy == win_sum - z)

{

printf("xy winner: %d == %d\n", sum_xy, win_sum - z);

} else if (sum_xz == win_sum - y)

// I'm not sure if I should use If or else If

{

printf("xz winner: %d == %d\n", sum_xz, win_sum - y);

} else if (sum_yz == win_sum - x)

{

printf("yz winner: %d == %d\n", sum_yz, win_sum - x);

}

if (sum_total != win_sum && sum_xy != win_sum -z && sum_xz != win_sum - y && sum_yz != win_sum -x)

{

printf("No winners yet\n");

}

}

P.S I'm using the following book: C How to Program by Deitel. Tried to find some example and some more info here but I couldn't.

Thanks for your help,

BusyOcean.

Dice Rolling: Write a program that simulates the rolling of 3 dice (x,y,z), consider that each dice can give 1-12 values. The program should use rand() three times to roll the first, second and third die respectively. The sum of the three values should then be calculated.

A) Consider the sum of the three dice values as the first winner.

eg. x+y+z -----> first winner.

B) Consider the sum of two dice and subtracting the third as the second winner.

eg. x+y+z = winning sum;

x+y-z ----> second winner // but I'm confused since x+y-z is not the only existing combination.

// I can also have: x+z - y or y+z -x How would I establish a difference between this cases and also is there a way to check which combination is going to occur first?

C) Any negative value player has to pay double amount ----> Here I'm lost (Never played dice rolling maybe that's why I have no clue what it means) Does it has to do with the question B) ?

D) On a 3600 trial, map the most possible, and the least possible winning values for the first and second player. ----> I think they talk about the matrix for 2 dice the most possible winning value would be 7 and the least possible would be 2 and 12. Should I make a matrix and get the values?

E) Least winning combinations for the player.

Dice Rolling Game Assignment in C:

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main (void)

{

int i, k, t, sum_total, win_sum;

int j, x, y, z,sum_xy, sum_xz, sum_yz;

printf("=============Random Dice Rolling=======\n");

srand(time(NULL));

for(j = 0; j<=3600; j++)

{

win_sum = 3 + rand()%7;

}

printf("Winning sum = %d\n\n", win_sum);

for (i = 1; i<=3600; ++i)

{

x = 1 + rand() % 3;

y = 1 + rand() % 3;

z = 1 + rand() % 3;

}

sum_total = x+y+z;

sum_xy = sum_total - z;

sum_xz = sum_total - y;

sum_yz = sum_total - x;

if (sum_total == win_sum)

{

printf(" 1st winner: %d + %d + %d = %d\n",x,y,z, win_sum);

}

if (sum_xy == win_sum - z)

{

printf("xy winner: %d == %d\n", sum_xy, win_sum - z);

} else if (sum_xz == win_sum - y)

// I'm not sure if I should use If or else If

{

printf("xz winner: %d == %d\n", sum_xz, win_sum - y);

} else if (sum_yz == win_sum - x)

{

printf("yz winner: %d == %d\n", sum_yz, win_sum - x);

}

if (sum_total != win_sum && sum_xy != win_sum -z && sum_xz != win_sum - y && sum_yz != win_sum -x)

{

printf("No winners yet\n");

}

}

P.S I'm using the following book: C How to Program by Deitel. Tried to find some example and some more info here but I couldn't.

Thanks for your help,

BusyOcean.

Last edited on

Topic archived. No new replies allowed.