Hey everybody. I'm writing a program that simulates rolling two dice and adds them up, eventually printing out the number of times each possibility (2 - 12) was rolled. The number of times the two dice are rolled is specified by the user. The code I've got so far compiles, but the output is way off. Can anybody give me some clues as to where I might have gone wrong?
Here's my code:

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

void roll(int numtimes, int *ptr[]);
int main() 
{
    int numtimes = 0;
    int control = 0;
    int timesrolled [11];
    int *arrayptr [11];
    for (control = 0; control < 12; control++)
        arrayptr[control] = &timesrolled[control];
    printf("How many times would you like to roll?");
    scanf("%d", &numtimes);
    roll(numtimes, arrayptr);
    for (control = 0; control <= 10; control++)
        printf("The number of %d's was %d\n", control+2, *arrayptr[control]);
    getch();
}
void roll (int numtimes, int *ptr[])
{
     int die1 = 0;
     int die2 = 0;
     int total = 0;
     int control = 0;
     for (control = 0; control < numtimes; control++)
         {
                 die1 = rand()%7;
                 die2 = rand()%7;
                 total = die1 + die2;
                 *ptr[total - 2]++;
         }
}

these are only some of your problems!


You don't want 11 pointers, you want 11 tallies!

//     int *arrayptr [11];
    int arrayptr [11];

Shouldn't you pre-clear your tallies?

//    for (control = 0; control < 12; control++) 
//       arrayptr[control] = &timesrolled[control];
   for (control = 0; control < 11; control++)
           arrayptr[control] = 0;

So you're using two seven sided dice?
Change your...

//                 die1 = rand()%7;   // 0...6
//                 die2 = rand()%7;
                 die1 = rand()%6;   // 0...5
                 die2 = rand()%6;   // 0...5
                total = die1 + die2;  // 0...10
//               *ptr[total - 2]++;
                 *ptr[ total ]++;
commented: Very helpful post. Helped me understand where my mistakes were. +1

That fixed it! Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.