Help with Lab?

So I understand the point of void functions and functions in general but my professor assigned a lab that I am completely lost in, I missed around two weeks of the semester because I was hospitalized and in really bad shape mentally and physically. So basically end of semester is here and my professor is nice enough to let me take my time to finish these labs whenever I can until the semester ends.

Anyway the issue is I made this for the lab before this one, but unsure how to add a function that makes it spaced out so it makes the shape my professor is asking for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace std;

int main() {

	int n;

	cout << "Enter n:  ";
	cin >> n;

	for (int i = 0; i < n; i++) {
		int stars = i + 1;
		for (int j = 0; j < stars; j++) {
			cout << "*";

		}
		cout << endl;
	}
	for (int i = 0; i < n; i++) {
		int stars = n - 1 - i;
		for (int j = 0; j < stars; j++) {
			cout << "*";
		}
		cout << endl;
	}
}


This is a screenshot of what hes asking for : http://prntscr.com/lszwob
The instructions say you must create a function called printChars which accepts two parameters.

I don't see that in your code above. Do that.
1
2
3
4
	for (int j = 0; j < stars; j++) {
			cout << "*";

		}

You're supposed to replace this with a function like
printChars(stars,'*');

With something like
1
2
3
4
void printChars(int numberToPrint, char charToPrint) {
  for ( int j = 0 ; j < numberToPrint ; j++ )
    cout << charToPrint;
}



> but unsure how to add a function that makes it spaced out so it makes the shape my professor is asking for.
Well you have a calculation like this.
int stars = i + 1;

Now think about a similar expression
int spaces = ?

To the point where you can do
1
2
printChars(spaces,' ');
printChars(stars,'*');


Topic archived. No new replies allowed.