Thread: Need help with program using arrays

  1. #1
    Registered User
    Join Date
    Nov 2009

    Need help with program using arrays

    I need some help finishing this program. I'm in a beginner's C++ course, and I dont fully understand how to use arrays.

    The program is to roll a single six-sided dice 6000 times to test whether the random number generator evenly distributes the random number it produces.

    Each face 1,2,3,4,5,6 is cumulated in the array, frequency.

    The output should be similar to this:
    Face                 Frequency
    1                          964
    2                         1013
    3                         1023
    4                          995
    5                          997
    6                         1008
    Here is what I have so far:
    #include <iostream>
    #include <iomanip>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    int main()
    	int num;
    	const int SIZE = 7;
    	int frequency[SIZE] = {0};
    	for (int i=0; i<6000; i++) //loop for rolling 6000 times
    		num = 1+rand()%6;
    		++ frequency[num];
    	cout <<"Face"<<setw(13) <<"Frequency"<<endl;
    	//print out the results 
    	return 0;
    What am I missing?

    Thanks in advance.
    Last edited by altrev; 11-15-2009 at 12:55 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    The code looks alright so far. Printing out the results should be easy enough with a for loop, just start at 1 instead of 0 and stop at 6 (since you don't use the first element of the array).

    for(unsigned int i = 1; i < 7; ++i)
    Also, use cstdlib and ctime instead of stdlib.h and time.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 04:50 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 0
    Last Post: 10-29-2001, 11:40 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM