I'm taking a C++ class with a not-so in depth professor. He's given us this assignment...
...and at first I was thinking I used the pyramid function to print the actual pyramid, but then when I was compiling it it said the function type was void, which I guess I should've realized on my own. Anyway, I'm not really sure what I should be doing from here.
Code:
I mostly need to know what to do with this void function, and then how to also print the numbers out in ascending order on the left of the center column of 1's.
Quote:
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:
[should be centered]
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
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). You must prompt the user for the number of lines in the main() and then call a function called printPiramid() from the main that has the following prototype: void printPyramid(int numLines);
You should follow good programming practices (Program header, meaningful variable names, and adequate comments, proper indentation) while writing your program. If you do not implement the pyramid drawing function printPyramid(), you will not get credit for this assignment.
[should be centered]
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
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). You must prompt the user for the number of lines in the main() and then call a function called printPiramid() from the main that has the following prototype: void printPyramid(int numLines);
You should follow good programming practices (Program header, meaningful variable names, and adequate comments, proper indentation) while writing your program. If you do not implement the pyramid drawing function printPyramid(), you will not get credit for this assignment.
...and at first I was thinking I used the pyramid function to print the actual pyramid, but then when I was compiling it it said the function type was void, which I guess I should've realized on my own. Anyway, I'm not really sure what I should be doing from here.
Code:
#include <iostream>
void printPyramid(int numLines);
int main (){
std::cout << "Please enter an integer between 1 and 15." << std::endl;
int numLines;
std::cin >> numLines;
std::cout << std::endl;
if (numLines < 1 || numLines > 15){
std::cout << "Incorrect value. Integer must be between 1 and 15." << std::endl;
std::cin >> numLines;
std::cout << std::endl;
} else {
void printPyramid(numLines);
}
return 0;
}
void printPyramid(int numLines){
for (int i = 1; i <= numLines; i++){
for (int j = 1; j <= i; j++) {
std::cout << " " << j << " ";
}
std::cout << std::endl;
}
}
I mostly need to know what to do with this void function, and then how to also print the numbers out in ascending order on the left of the center column of 1's.