Help Please With Arrays.

This is a discussion on Help Please With Arrays. within the C++ Programming forums, part of the General Programming Boards category; I wrote the following program to answer the question: Write a program that simulates the rolling of two dice. The ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    1

    Help Please With Arrays.

    I wrote the following program to answer the question:

    Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. Note: Since each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. There are 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one sixth of all the rolls should be 7).
    But its not executing properly please help URGENTLY.
    #include<iostream.h>
    #include<iomanip.h>
    #include<stdlib.h.>
    #include<time.h>
    int rollDice(void);
    int sum;

    int main()
    {
    const int arraysize=12;
    int frequencyOfSum[]={0};


    for (int roll=1;roll<=36000;roll++)
    { rollDice();
    ++frequencyOfSum[rollDice()];
    }

    cout<<"Sum"<<setw(10)<<"Frequency"<<endl;
    for(sum=2;sum<=arraysize;sum++)
    cout<<setw(4)<<sum<<setw(14)<<frequencyOfSum[rollDice()]<<endl;
    return 0;
    }

    int rollDice(void)
    {
    srand(time(0));
    int die1,die2;

    die1=1+rand()%6;
    die2=1+rand()%6;
    sum=die1+die2;
    return sum;
    }

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208

    Re: Help Please With Arrays.

    Originally posted by lyna

    Code:
    int main()
    {
    	const int arraysize=12;
    	int frequencyOfSum[arraysize]={0};
    	 
    
    	 for (int roll=1;roll<=36000;roll++)
    	 { rollDice();	<--thiscalldoes nothing
    	 ++frequencyOfSum[rollDice()];
    	 }
    	 
    	 cout<<"Sum"<<setw(10)<<"Frequency"<<endl;
    	  for(sum=2;sum<=arraysize;sum++)
    		  cout<<setw(4)<<sum<<setw(14)<<frequencyOfSum[sum]<<endl;
         return 0;
    }
    
    int rollDice(void)
    {
    	srand(time(0));
    	int die1,die2;
    
    	die1=1+rand()%6;
    	die2=1+rand()%6;
    	sum=die1+die2;
    	return sum;
    }

    I changed whats in red,
    and about your rand calls; using % takes away the randomness because it only looks at the upper bits,; you should do a search on google for srand and rand and see other , better ways to use them.
    Last edited by beege31337; 12-08-2002 at 04:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

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