C programming questions including the use of arrays

This is a discussion on C programming questions including the use of arrays within the C Programming forums, part of the General Programming Boards category; Hello! Sorry to make the first post as a question, but I am struggling on the following question: "Write a ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    3

    C programming questions including the use of arrays

    Hello! Sorry to make the first post as a question, but I am struggling on the following question:

    "Write a program to simulate the rolling of a dice. The sum of the two dice is to be calculated. If the dice is rolled 10,000 times, using arrays record the number of times each possible sum occurs."

    for the simulating the rolling of dice and it's sum it is very simple, but how would you go about recording (using arrays) the amount of times the same sum occurs (inside the loop) ?

    Thank you all in advance !

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Just create an array of type int and loop through each of your 10000 cases storing the result in the location that sum has (2 - 12). Pseudo code below:

    Code:
    int results[13] = { 0 }, sum = 0, index; //12 is the highest possible sum
    
    for(index = 0; index <= 10000; index++){ //for each of the 10,000 iterations
    	sum = getSum();
    	results[sum]++;
    }
    That's how I would go about doing it.

    Then to read this info back to stdout, you could just loop through your array like so:

    Code:
    for( index = 0; index <= 12; index++ )
    	printf("There were %d %d's\n", result[index], index );
    Last edited by Syscal; 12-02-2012 at 09:07 AM.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    Quote Originally Posted by Syscal View Post
    Just create an array of type int and loop through each of your 10000 cases storing the result in the location that sum has (2 - 12). Pseudo code below:

    Code:
    int results[13] = { 0 }, sum = 0, index; //12 is the highest possible sum
    
    for(index = 0; index <= 10000; index++){ //for each of the 10,000 iterations
        sum = getSum();
        results[sum]++;
    }
    That's how I would go about doing it.

    Then to read this info back to stdout, you could just loop through your array like so:

    Code:
    for( index = 0; index <= 12; index++ )
        printf("There were %d %d's\n", result[index], index );
    Thanks! Just to clear up a small misunderstanding from my side - what is getSum() being used for in your code? As well as in your second example - result[]?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,185
    Quote Originally Posted by Volodya93 View Post
    Thanks! Just to clear up a small misunderstanding from my side - what is getSum() being used for in your code? As well as in your second example - result[]?
    Your original problem statement said that you need to find the sum of two rolls of a die. It means you should write a function called getSum() which rolls two dice and returns the sum of the two rolls.

    results[] is an array which stores the results. For example, if you roll two dice and get the sum 12, then you could keep a tally of this fact by incrementing results[12].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some questions about arrays
    By student111 in forum C++ Programming
    Replies: 15
    Last Post: 02-17-2012, 07:51 AM
  2. 2 questions in C programming
    By Flow-Style in forum C Programming
    Replies: 7
    Last Post: 07-06-2010, 07:08 PM
  3. Questions on Character Pointers and Arrays
    By 7heavens in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2010, 03:00 AM
  4. A couple of questions concerning arrays
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2006, 05:47 PM
  5. questions about arrays
    By laasunde in forum C++ Programming
    Replies: 3
    Last Post: 08-14-2003, 05:53 AM

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