I'm having trouble with printing out a "*" based on the number that a user inputs.
for example:
if the user inputs: 1 4 2 3 0<-- the zero is used to finish the input.
the output should be:
*
* *
***
****
instead my output is showing:
*
****
**
***
I think it's the issue in my second for loop.
I think it might be because it's printing out the exact amount of numbers that is in that memory address of the array. How do I fix this though so that it prints out like normal?
Your problem is that the program is outputting the number of asterisks requested by the user in the order the user entered them. 1 4 2 3 0 corresponds with 1 on the first row, 4 on the second row, etc... It seems what you *actually* want is for the program to output the number of asterisks requested by the user from smallest to largest, so if the user input "5 3 4 1 6 0", the program would then output 1, 3, 4, 5, and 6 starts for each row.
Is that what you want? If so, you would have to sort the array before you call your for loop to output.
That actually won't work, I just found out. It's actually not supposed to be in order. Here's another example for help.
1 2 3 4 5 6 7 8 9 10 11
For example, if the user entered, 1 2 3 4 3 2 1 10 0, my program should print:
*
*
*
*
*
*
* *
*** *
***** *
********
I put it into the code block because it would automatically keep formatting the aestriks into a funky way. but is there any other way I could do the for loop you saw Keene? because when I enter the numbers: 1 2 3 4 3 2 1 10 0, this shows up:
1 2 3 4 5 6 7 8
*
**
***
****
***
**
*
**********
which is almost the same as what's needed above, but not 100 percent, due to it being flipped around.
Ooh... that's a really interesting problem. You've got to rotate your current output 90 degrees. So, what you've currently got is saying "the number of integers the user enters represents the number of rows I should print, and each integer represents the length each row." You need to flip that around and have your code say, "the number of integers the user enters represents the number of columns I should print, and each integer represents the height of each column."
Exactly, I would have had this solved by now but it all my output needs to be rotated basically :/
That makes sense, in fact that's what I was thinking of doing, but I just can't really get anything to work that I try.
Like if I use cout << endl; to get me to the bottom of a column and then print out an aestrik then the rest of my code will loop back and keep printing all the aestriks underneath each other.
This is the closest I could get my for loop to making my output look like that, which I believe is the same you saw earlier, but for some reason I can't get anything else to work.
//start from the tallest
for (int i = largestNumber; i > 0; --i)
{
//iterate through each column
for (int j = 0; j < amountEntered; ++j)
{
if ( input[j] >= i ) cout << "*"; //it's this tall!
else cout << " "; //don't put a *, but instead a ' ' for formatting
}
cout << endl;
}