Write a C++ program which will ask the user to enter an integer from 1 to 15 and displays a number pyramid (implement input validation). For example, if the input is 4 the output will be as follows: 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4
I can't seem to get the format on how to display the spacing correct, any ideas?
post your code at a site like http://ideone.com so we can take a look at how it works, and work from there to get it right :-D
mk,
They also gave me this as a hint: The user input represents the total number of lines in the pyramid. Each number occupies three spaces. You must use nested loops to implement your program. (Hint: Each line has three parts)
The hint suggests that you use an outer loop that walks through each line, and one or more inner loops which prints the numbers on each line.
The question is, how do I do that? :P
nested for loops. You can start by prompting the user to enter a number from 1 to 15. Your outer for loop will work for that number of iterations.
Why is this saying cout is undefined? http://codepad.org/XBzfcyfd Line 19: error: iostream: No such file or directory Line 18: error: iomanip: No such file or directory Line 3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace' In function 'main': Line 6: error: 'cout' undeclared (first use in this function) Line 6: error: (Each undeclared identifier is reported only once Line 6: error: for each function it appears in.) Line 8: error: 'cin' undeclared (first use in this function) Line 10: error: expected ';' before 'num' Line 11
you're compiling it in C :-P choose C++ as the language.
Ohhhhh...
So now do you have any ideas on the spacing/number counting part?
To get them spaced correctly, you can have one loop inside the outer loop just printing spaces, and then a second loop (inside the outer loop but after the first loop that just prints spaces) actually print the numbers.
But how would I know how many spaces to print if i dont know the number of numbers they want yet?
You should write your program so that it figures out the number of spaces to print, according to user input from std::cin
Here's my solution to it, but it doesn't scale well to numbers greater than 9 http://ideone.com/fqqg2
wow, your good
If you're not opposed to using C's printf(), then maybe the solution will also scale to larger numbers.
when the double digits come into play it pushes them over a bit
yeah.
Me thinks so
yes yours works right
Mk, just checking, Thanks for the help!
While writing the solution to this, I ended up getting this kind of output: Number Parallelogram! http://ideone.com/sGWpB
lol? I like that one :P
Oh snap,
Lol, I'm going to print that out and hand it to the professor :P
1 question though, how come you like to use std::cout instead of just using namespace std; and then cout
I like writing std:: everywhere. Helps me learn C++.
lol, are you being serious?
yes
how does that help?
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c
No need to worry too much about that stuff when you're just learning C++ though :-P
ah
I'm lazy, I'll use cout << "Until its necessary to switch :P " << endl;
Classes are over now! This semester is finished! Now its time to party!
Join our real-time social learning platform and learn together with your friends!