I am almost finished with my project but am having trouble trying to scale my gr
ID: 3551386 • Letter: I
Question
I am almost finished with my project but am having trouble trying to scale my graph to a max of 20 astericks. i also need to output the amount each asterick equals. im sure its very simple but cannot think of how to do it. Here is my code so far.
// Galton machine simulation
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
int main()
{
using namespace std;
srand(time(0));
int i, j, slot,levels, balls, count, *slots;
char again;
do{
while (1) {
cout << "Simulates a Bell Curve ";
cout << "************************* ";
cout << "Levels: ";
cin >> levels;
if (levels > 100)
cout << "Maximum 100 levels. Please re-enter ";
else if (levels < 1)
cout << "Levels must be greater than 0. Please re-enter ";
else break;
}
while (1) {
cout << "Balls: ";
cin >> balls;
if (balls < 1)
cout << "Balls must be greater than 0. Please re-enter ";
else break;
}
slots = new int[levels+1];
for (i = 0; i < levels+1; ++i)
slots[i] = 0;
for (i = 0; i < balls; ++i) {
slot = 0;
for (j = 0; j < levels; ++j)
if (rand()%2) ++slot;
++slots[slot];
}
for (i = 0; i < levels+1; ++i)
cout << "Slot " << i+1 << ": " << slots[i]
<< " balls" << " ";
//Creating Graph
cout << "Graph results: (scale: one * represents " <<
for (i = 0; i < levels+1; ++i) {
slots[i] = slots[i]/5;
floor(slots[i] + .05);
}
for (i = 0; i < levels+1; ++i) {
cout << "Slot: " << i +1 << " ";
for (count = 0; count < slots[i]; ++count)
cout << "*";
cout << endl;
}
Explanation / Answer
//Arranges the n asterisk in a two dimensional array
for(i = 0; i < max; i++)
{
int count = 0;
for(j = max - 1; j >= 0; j--)
{
if(count - a[i])
graph[j][i] = ' ';
else graph[j][i] = '';
count++;
}
}
//Asterisks will be displayed in a graph vertically
for(i = 0; i < n; i++)
{
for(j = 0; j < max; j++)
{
cout << graph[i][j] << " ";
}
cout << " ";
}
cout << " Units produced in thousands of units: ";
for(i = 0; i < max; i++)
// for(i = max-1; i >= 0; i--)
// {cout << setw(3) << i << " ";
// for(j = 0; j < n; j++)
// cout << graph[i][j] << " ";
// cout << endl;
// }
// for(i = 0; i < n; i++)
// {
// if(i == 0)
//cout << " ";
//cout << " plant ";
// }
// cout << endl;
//
// for(i = 0; i < n; i++)
// {
// if(i == 0)
//cout << " ";
//cout<<" "<< i + 1 << " ";
// }
cout << endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.