Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (anonymous):

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

OpenStudy (anonymous):

I can't seem to get the format on how to display the spacing correct, any ideas?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

mk,

OpenStudy (anonymous):

http://codepad.org/RA399grb

OpenStudy (anonymous):

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)

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

The question is, how do I do that? :P

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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

OpenStudy (anonymous):

you're compiling it in C :-P choose C++ as the language.

OpenStudy (anonymous):

Ohhhhh...

OpenStudy (anonymous):

So now do you have any ideas on the spacing/number counting part?

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

But how would I know how many spaces to print if i dont know the number of numbers they want yet?

OpenStudy (anonymous):

You should write your program so that it figures out the number of spaces to print, according to user input from std::cin

OpenStudy (anonymous):

Here's my solution to it, but it doesn't scale well to numbers greater than 9 http://ideone.com/fqqg2

OpenStudy (anonymous):

wow, your good

OpenStudy (anonymous):

If you're not opposed to using C's printf(), then maybe the solution will also scale to larger numbers.

OpenStudy (anonymous):

http://ideone.com/HNK78

OpenStudy (anonymous):

when the double digits come into play it pushes them over a bit

OpenStudy (anonymous):

yeah.

OpenStudy (anonymous):

maybe this? http://ideone.com/jdIMu

OpenStudy (anonymous):

Me thinks so

OpenStudy (anonymous):

yes yours works right

OpenStudy (anonymous):

Mk, just checking, Thanks for the help!

OpenStudy (anonymous):

While writing the solution to this, I ended up getting this kind of output: Number Parallelogram! http://ideone.com/sGWpB

OpenStudy (anonymous):

lol? I like that one :P

OpenStudy (anonymous):

http://ideone.com/loJdi

OpenStudy (anonymous):

Oh snap,

OpenStudy (anonymous):

Lol, I'm going to print that out and hand it to the professor :P

OpenStudy (anonymous):

1 question though, how come you like to use std::cout instead of just using namespace std; and then cout

OpenStudy (anonymous):

I like writing std:: everywhere. Helps me learn C++.

OpenStudy (anonymous):

lol, are you being serious?

OpenStudy (anonymous):

yes

OpenStudy (anonymous):

how does that help?

OpenStudy (anonymous):

No need to worry too much about that stuff when you're just learning C++ though :-P

OpenStudy (anonymous):

ah

OpenStudy (anonymous):

I'm lazy, I'll use cout << "Until its necessary to switch :P " << endl;

OpenStudy (anonymous):

Classes are over now! This semester is finished! Now its time to party!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!