-
Dice Program
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);
-
> printf("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL, a);
How does this assign a value to a?
> printf("The sum of the dice is %i, and their average is %d.1", sum, average);
Read the printf manual page on what format is used to print a double.
What compiler are you using?
Try using one which can warn you about a load of dumb stuff.
Code:
$ gcc -W -Wall -Wextra bar.c
bar.c: In function ‘main’:
bar.c:17:7: warning: too many arguments for format [-Wformat-extra-args]
bar.c:19:7: warning: too many arguments for format [-Wformat-extra-args]
bar.c:21:7: warning: too many arguments for format [-Wformat-extra-args]
bar.c:23:7: warning: too many arguments for format [-Wformat-extra-args]
bar.c:25:7: warning: too many arguments for format [-Wformat-extra-args]
bar.c:34:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat]
bar.c:17:13: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
bar.c:19:13: warning: ‘b’ is used uninitialized in this function [-Wuninitialized]
bar.c:21:13: warning: ‘c’ is used uninitialized in this function [-Wuninitialized]
bar.c:23:13: warning: ‘d’ is used uninitialized in this function [-Wuninitialized]
bar.c:25:13: warning: ‘e’ is used uninitialized in this function [-Wuninitialized]
-
Also....you need to learn how prinf is supposed to be....the following is WRONG!!!
Code:
printf("The sum of the dice is %i, and their average is %d.1", sum, average);
no matter what you do here, you will always get .1 at the end...
ALSO
Code:
average = (a + b + c + d + e)/5;
your adding up ints, dividing by ints, then assigning to a double....
means you will never get the average, you will only get a whole number.
need to learn int/double math
-
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int num,loop,total,mean;
srand((unsigned)time(NULL));
for(loop=0;loop<5;loop++)
{
num=rand()%6+1;
printf("Dice roll #%d = %d\n",loop+1,num);
total+=num;
}
mean=total/5;
printf("\nSum = %d.\n",total);
printf("Average = %d.\n",mean);
return(0);
}
-
Quote:
Originally Posted by
LTA85
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int num,loop,total,mean;
srand((unsigned)time(NULL));
for(loop=0;loop<5;loop++)
{
num=rand()%6+1;
printf("Dice roll #%d = %d\n",loop+1,num);
total+=num;
}
mean=total/5;
printf("\nSum = %d.\n",total);
printf("Average = %d.\n",mean);
return(0);
}
You neglected to initialize "total."