Im a first year C++ programer using unix and have no idea how to do this question. I think ill either have to use a 2D array or a 1D array with multiple do for and while loops. Im mainly strugling with plotting the histogram in vertical colums this is the question
Implement a C++ program that reads a sequence of positive integer numbers terminated by a negative number (sentinel) and displays a histogram of all numbers. A histogram shows the total number of occurrences for each number in a sequence. For example a sequence of numbers can be delimetered by whitespace or newline bytes.
1 2 2 1 4 5 4 2 3 5 6 7 6 4 1 2 -1
has the following histogram.
*
* * *
* * * * *
* * * * * * *
-------------
1 2 3 4 5 6 7
You program should display a histogm
You may find that people on here are fairly unhelpful if you basically just ask them to write a program for you, especially if it sounds like it's a homework exercise ;)
You are much more likely to get constructive help if you actually post the code you have been working on and ask specific questions about the parts you have trouble with. :D
Regards
-Xander314
PS: And if you put code on here, remember to enclose it in [code][/code] tags so you get syntax highlighting:
[code]int main()[/code] -> int main()
#include <iostream>
usingnamespace std;
int main ()
{
int number[10] = {0}; /* define a 1D array */
int i = 0; /* set i to zero so they can be used in the loop*/
int num;
int biggest = 0;
cout << "Enter a sequence of one or more positive integer numbers terminating";
cout << " with a (-1) to indicate the end of your sequence. ";
cin >> num;
do
{
for (i=0; i<10; i++)
{
cin >> number[num - 1];
number[num - 1]++;
biggest = number[i];
for (i = 0; i < 10; i++)
{
for (int y = biggest; y>= 0; y--)
{
if (number[i] >= y)
cout << "*";
else
cout << " ";
}
}
}
}while (num != -1); /* -1 is the sentinel */
return 0;
}
You input a value into num which is subsequently never changed. Thus your do ... while loop will last for forever, unless the user enters -1 at the start in which case it will run once.
You are reading every input into the same slot in the array as num does not change. Do you mean to increment the value num? Even so, I don't really see why this variable is necessary: you have the loop counter i.
also my advice for actually creating the graph. since its impossible to go back a line after using endl or \n instead start a for loop with the variable i having the value of the largest number (decrement i until its equal to zero). then go through each number entered and if its greater than or equal too i print an * for it, otherwise dont.
Im fairly new at programming too but ive had a crack at the problem. I think im close probally something to do with the storing of my largest variable, maybe someone could fix it for me. Hope this helps.
finding the largest element of an array is easy. looks like this:
1 2 3 4 5 6 7
int nums[5] = { 2, 5, 8, 50, 19 };
int largest = nums[0];
for (int i = 1; i < 5; i++)
{
if (largest < nums[i])
largest = nums[i];
}
your code doesnt allow for the user to enter -1 to terminate, it just gets 10 numbers. i dont understand at all what your second for loop is doing, it would be very easy for it to go out of bounds of the array if the user enters something other than 10. also you dont need nested for loops or anything.
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
using std::setw;
using std::cin;
int main(void)
{
constint max = 10;
int list[max] = { 0 };
cout << "Enter up to 10 numbers. To stop enter a negative number. " << endl;
cin >> list[0];
for (int i = 1; i < 10 && list[i - 1] != -1; i++)
{
cin >> list[i];
}
int largest = list[0];
for (int i = 1; i < max; i++)
{
if (largest < list[i])
largest = list[i];
}
for (int i = largest, numCount = 0; i > 0; i--, numCount++)
{
cout << endl << "|";
for (int y = 0; y < 10; y++)
{
if (list[y] >= i)
cout << setw(3) << "*";
}
}
cout << endl;
for (int i = 0; i < 10; i++)
{
cout << "----";
}
cout << endl;
return 0;
}