Array

This is a discussion on Array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main (void) { printf("die1 \t die2 \t sum \n "); int x,num1,num2,sum; srand((unsigned) time(NULL)); for (x=1;x<100;x++) ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Array

    Code:
    #include <stdio.h>
    int main (void)
    {
       printf("die1 \t die2 \t sum \n ");
    
        int x,num1,num2,sum;
       
        srand((unsigned) time(NULL));
    
        for (x=1;x<100;x++)
        {
             num1=rand() % 6+1;
             num2=rand() % 6+1;
             sum = num1 + num2;
             printf("%4i \t %4i \t %3i \n", num1,num2,sum;
        }
    
    }
    
    
    
    How do i count the number of times 2 was
    rolled, 3 was rolled, 4 was rolled all the way to 12 using an array. Please help.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Before the end of the loop, place a conditional for each value you want to keep track of, then increment a counter variable accordingly.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Code:
    im a beginner and i just started in C. I have no idea how to do that

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       printf("die1 \t die2 \t sum \n ");
    
        int x,num1,num2,sum;  //etc.
        int tally[12] = { 0 };
       
        srand((unsigned) time(NULL));
    
        for (x=1;x<100;x++)
        {
             num1=rand() % 6+1;
             num2=rand() % 6+1;
             sum = num1 + num2;
             printf("%4i \t %4i \t %3i \n", num1,num2,sum;
             
             tally[sum]++;
        }
    
        for(x = 0; x < 13; x++)
            printf("%d's: %d\n", x, tally[x]);
    
    }
    How do i count the number of times 2 was
    rolled, 3 was rolled, 4 was rolled all the way to 12 using an array. Please help.
    Please post your code up *inside* code tags, and make other comments not directly affecting your code, outside the code tags.

    Nice style of coding, btw.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Adak, you just saved the original posted from even having to think about the problem. How is DWestbrook supposed to learn?

    Fortunately you also introduced two bugs into that code. I shall hold off saying what they are for now.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21