Hello I am trying to make a program that "rolls" 5 dice randomly and computes the sum and mean. I have the program running, but it is not working the way I want it to.
The problem I am guessing is in the printf statements where I want to assign that value that is randomly chosen to a variable and then do the math with the variables. Even though it still runs, I can not get the right sum and mean.
Is it possible to do the calculations I want the program to do with the way I am specifying them; or do I need to write a separate function that assigns the random values and then proceeds from there? Thanks
Code:#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_VAL 6 #define MIN_VAL 1 int main(void) { srand(time(NULL)); printf("Here are five rolled dice:"); int a,b,c,d,e; printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, a); printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, b); printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, c); printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, d); printf("%i \n", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, e); int sum; double average; sum = a + b + c + d + e; average = (a + b + c + d + e)/5; printf("The sum of the dice is %i, and their average is %d.1", sum, average);



LinkBack URL
About LinkBacks



