Skip to content

Instantly share code, notes, and snippets.

@DanielDe
Created November 3, 2012 21:54
Show Gist options
  • Save DanielDe/4008986 to your computer and use it in GitHub Desktop.
Save DanielDe/4008986 to your computer and use it in GitHub Desktop.
CS 10 SI Lab Session 5 Exercise 3 Solutions
#include <iostream>
using namespace std;
void print_rect(int, int); // we declare the function here
int main() {
int w, h;
// We generally prompt the user for input in the main function
cout << "Enter width: ";
cin >> w;
cout << "Enter height: ";
cin >> h;
print_rect(w, h);
return 0;
}
void print_rect(int width, int height) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
cout << "* ";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
void print_tri(int);
int main() {
int h;
cout << "Enter height: ";
cin >> h;
print_tri(h);
return 0;
}
void print_tri(int height) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
}
#include <iostream>
using namespace std;
void print_pyr(int);
int main() {
int h;
cout << "Enter height: ";
cin >> h;
print_pyr(h);
return 0;
}
void print_pyr(int height) {
for (int i = 0; i < height; ++i) {
for (int j = 0; j < height - i - 1; ++j) {
cout << " ";
}
for (int k = 0; k < 1 + 2 * i; ++k) {
cout << "* ";
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment