7 Replies - 7291 Views - Last Post: 13 September 2010 - 06:04 PM Rate Topic: -----

#1 RFID98L77S   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 13-September 10

Array into Histogram

Posted 13 September 2010 - 02:24 PM

I have to take the data in the array and output it into a histogram (vertical and horizontal). I also have to have the output listed. Here's my code thus far. Could anyone assist me with the rest?

#ifndef DIE_H					// allows for additional
#define DIE_H

class Die

{
public:										// available outside of class
		Die();								// SMF to set value
		void Roll();							// member functions
		int GetFaces();

private:									// not available outside class
		int Face;
};

#endif



#include "Die.h"				// for processing die face
#include <cstdlib>					// for the library rand() function

Die::Die()						//Initializes Face data member
{
 Face = 1;
}

void Die::Roll()
{
    Face = rand() % 6 + 1;
	
}
 
int Die::GetFaces()
{
    return Face;
}


#ifndef DIEROLL_H			// allows for additional
#define DIEROLL_H				

#include "Die.h"

class DiceRoll					// classs that specifies a collection of 3 contained Die objects

{
public:
		void RollDice();				// Calls Roll() function on each contained die
		int GetRollFaces();		// Returns the sum of the current Face value of Die1, Die2, & Die3

private:
		Die Die1;				// The three die contained in this -i.e
		Die Die2;				// objects of this class automatically contain
		Die Die3;				// three dice
};
#endif


#include <iostream>  				// for cin,cout
#include <cstdlib>					// for the library rand() function
#include "DieRoll.h"				// for processing die roll
#include "Die.h"

	void DiceRoll::RollDice()		
	{
		Die1.Roll();
		Die2.Roll();
		Die3.Roll();
	}
	int DiceRoll::GetRollFaces()
	{
		int result;
		result = Die1.GetFaces()+Die2.GetFaces()+Die3.GetFaces();
		return result;
	}


#ifndef SHELL_H   // Avoid duplicate compilations
#define SHELL_H   //

void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[]); // 1st global function prototype
void DisplayResults(int ResultsArray[ ], int ResultsArraySize ); //2nd global function prototype
void createHistogram(int frequency[], int range);				 // function to display histogram

#endif



#include <iostream>  				// for cin,cout
#include <iomanip>
#include <cstdlib>					// for the library rand() function
#include <cmath>
#include <ctime>
#include "FreeFunction.h"			// for processing the roll
#include "Die.h"				// for processing die face
#include "DieRoll.h"				// for processing die roll


using namespace std;
void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[])
{
	for ( int i = 0; i <200; i++)
	{
		switch ( RollsArray[ i ] ) 
		{
		case 3: ResultsArray[ 0 ] += 1; break;
		case 4: ResultsArray[ 1 ] += 1; break;
		case 5: ResultsArray[ 2 ] += 1; break;
		case 6: ResultsArray[ 3 ] += 1; break;
		case 7: ResultsArray[ 4 ] += 1; break;
		case 8: ResultsArray[ 5 ] += 1; break;
		case 9: ResultsArray[ 6 ] += 1; break;
		case 10: ResultsArray[ 7 ] += 1; break;
		case 11: ResultsArray[ 8 ] += 1; break;
		case 12: ResultsArray[ 9 ] += 1; break;
		case 13: ResultsArray[ 10 ] += 1; break;
		case 14: ResultsArray[ 11 ] += 1; break;
		case 15: ResultsArray[ 12 ] += 1; break;
		case 16: ResultsArray[ 13 ] += 1; break;
		case 17: ResultsArray[ 14 ] += 1; break;
		case 18: ResultsArray[ 15 ] += 1;
		}
	}
}

void DisplayResults(int ResultsArray[ ], int ResultsArraySize )
{
	for ( int i = 0; i < 200; i++)
	{
		cout<< "\n\nOut of 200 Rolls of 3 Dice: \n\n"<< endl;
		cout << "The number " << i + 1 << " was rolled " << ResultsArray[i] << " times." << endl;
	}
}

//Creates Histogram of scores
void createHistogram(int frequency[], int range)
{
       for(int i = 0;i<=range;i++)
	   {
		   cout<<setw(3)<<i<<setw(3)<<frequency[i];
		   for(int j = 1;j<=frequency[i];j++)
		   {
			   cout<<"*";
			   cout<<endl;
		   }
	   }
}



#include <iostream>  				// for cin,cout
#include <iomanip>
#include <cstdlib>					// for the library rand() function
#include "FreeFunction.h"			// for processing the roll
#include "DieRoll.h"				// for processing die roll


using namespace std;
int main ()
{
	const int NUM_RANGE = 15;
	int frequency[NUM_RANGE+1];
	int RollsArray[100];
	DiceRoll TheIvories;
	for (int i = 0; i < 100; i++)
	{
	TheIvories.RollDice();						// Roll the dice
	RollsArray[i] = TheIvories.GetRollFaces(); // Record the result of the roll end loop
	}
		for (int j = 0; j< 100; j++)
	{
		cout << RollsArray[j]<< endl;
	
	}


return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Array into Histogram

#2 Splatocaster   User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Array into Histogram

Posted 13 September 2010 - 02:55 PM

I don't understand the problem? Please refine your initial question.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Array into Histogram

Posted 13 September 2010 - 03:05 PM

See also here and here for history.
Was This Post Helpful? 0
  • +
  • -

#4 RFID98L77S   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 13-September 10

Re: Array into Histogram

Posted 13 September 2010 - 03:48 PM

View PostJackOfAllTrades, on 13 September 2010 - 02:05 PM, said:

See also here and here for history.


Right, to clarify: I started off asking for help on other sites, but when I stopped getting a response I opted to try another site and hope for the best.

Originally, my assignment was to create a die rolling game that would take two objects (die and diceroll), put them through a rng, loop as though three dice were being rolled, sum the results, and display the results on the screen. Also requested was to then take the data and put it into a histogram (bar chart) both horizontal and vertical.

I've done 98% of the assignment, and am asking for a hand with the last graphical display part, please.
Was This Post Helpful? 0
  • +
  • -

#5 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Array into Histogram

Posted 13 September 2010 - 04:58 PM

For some reason if you ask elsewhere it's considered "poor manners" and they won't help you.

Do you mean a text histogram? That's easy enough, see the stream manipulator setw().

Play around with it, and make sure you look at the example on that page.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Array into Histogram

Posted 13 September 2010 - 05:08 PM

No, it's quite obvious he stopped getting the help he wanted and came here. There's nothing wrong with that. Just wanted to give the post some context.

It's considered rude when people cross-post across forums within minutes of each other.

This post has been edited by JackOfAllTrades: 13 September 2010 - 05:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Array into Histogram

Posted 13 September 2010 - 06:00 PM

I suppose it does make sense to not give it priority, because of the extra help.

It seems to me you need to be a bit more creative, there is usually more than one way to do something, and no one to tell you right from wrong. Plus less hand holding is good for you, and us.
Was This Post Helpful? 0
  • +
  • -

#8 RFID98L77S   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 13-September 10

Re: Array into Histogram

Posted 13 September 2010 - 06:04 PM

You're absolutely right. I'll step off and work on my assignments either by myself or ask my instructor from this point onward. Thanks for the advice and the info on stream manip.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1