# Thread: Help with some array stuff

1. ## Help with some array stuff

Basically, 36000 rolls of the dice. I want to output the sum of the two dice (done) the frequency of each sum (done) and the expected along with actual. Both of the later should be in percentage. I know how they would be calculated. Actual is freq/36000 and expected is just stats stuff (1k, 2k, 3k, 4k, 5k, 6k, 5k, 4k, 3k, 2k, 1k).

When I try to do freq/36000 I just can't get it to work.

Any suggestions for getting both of these other needed values into arrays and printed?

Code:
```#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 13

int rollDice(void);

int main ()
{
int roll, sum, freq[SIZE] = {0};
float expected[SIZE] = {0}, actual[SIZE] = {0};

srand(time(NULL));

for (roll= 1; roll <= 36000; roll++)
{
sum = rollDice();
++freq[sum];
}
printf("%s%10s%12s%12s\n", "Sum", "Total", "Expected", "Actual");

for (sum =2; sum <= SIZE -1; sum++)
{
printf("%2d%10d%12f%12f\n", sum, freq[sum], expected[sum], actual[sum]);
}
return 0;
}

int rollDice(void)
{
int die1;
int die2;
int worksum;

die1 = 1 + ( rand() % 6 );
die2 = 1 + ( rand() % 6 );
worksum = die1 + die2;

return worksum;
}```

2. Assign some values to the arrays you are trying to print (expected and actual)?

3. Originally Posted by Dave_Sinkula
Assign some values to the arrays you are trying to print (expected and actual)?
That is where I am having some trouble

4. Well you'll have to elaborate on what you mean by expected and actual...

5. Actual is going to be the frequency of each sum / 36000

Expected is as stated above. The statistical chance of each pair of dice.

6. Originally Posted by khskenny
Actual is going to be the frequency of each sum / 36000

Expected is as stated above. The statistical chance of each pair of dice.
Perhaps you should increment an array member when its result occurs?

7. Originally Posted by Dave_Sinkula
Perhaps you should increment an array member when its result occurs?
Seems to work. I just had to sit and stare at it for a while. Just need to tweak a bit to get %

Code:
```#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 13

int rollDice(void);

int main ()
{
int roll, sum, freq[SIZE] = {0};
float expected[SIZE] = {0,0,1,2,3,4,5,6,5,4,3,2,1}, actual[SIZE] = {0};

srand(time(NULL));

for (roll= 1; roll <= 36000; roll++)
{
sum = rollDice();
++freq[sum];
++actual[sum];
}
printf("%s%10s%12s%12s\n", "Sum", "Total", "Expected", "Actual");

for (sum =2; sum <= SIZE -1; sum++)
{
printf("%2d%10d%12f%12f\n", sum, freq[sum], expected[sum]/36, actual[sum]/36000);
}
return 0;
}

int rollDice(void)
{
int die1;
int die2;
int worksum;

die1 = 1 + ( rand() % 6 );
die2 = 1 + ( rand() % 6 );
worksum = die1 + die2;

return worksum;
}```

8. I've tried a few different ways to display these computed fields as percentages with no luck. Any tricks?

9. Something like this?
Code:
```      printf("%2d%10d%12f%%%12f%%\n", sum, freq[sum],
100*expected[sum]/36, 100*actual[sum]/36000);```

10. Originally Posted by Dave_Sinkula
Something like this?
Code:
```      printf("%2d%10d%12f%%%12f%%\n", sum, freq[sum],
100*expected[sum]/36, 100*actual[sum]/36000);```
That works. Thanks

My instructor insisted that only one "%" was needed to make the symbol appear. Obviously two is the trick.

11. >My instructor insisted that only one "%" was needed to make the symbol appear.
% is a special character in printf format strings. Your instructor is either confused or shouldn't be teaching C at all.

12. Your instructor is either confused or shouldn't be teaching C at all.
Or maybe he was talking about something else but not printf

13. >Or maybe he was talking about something else but not printf
I think that falls under "confused".

14. Originally Posted by Prelude
>Or maybe he was talking about something else but not printf
I think that falls under "confused".
I thought about possibility of misinterpretation of tutors words...

15. > My instructor insisted that only one "%" was needed to make the symbol appear.
Which would be fine when using fputs(), but in printf(), two is needed.

But then again, fputs() only outputs fixed strings, whereas printf() can do lots of conversion and formatting.