making triangle for C++

our prof ask us to fill the in the following functions:
-string makeLine(int length)
-string makeIncreasingPart(int rows)
-string makeDecreasingPart(int rows)
-string makeTriangle(int width)

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

this is the program I'm working on, but something is not right here, i hope someone from here can help me. THANK YOU!

#include <string>
#include <iostream>
#include <cassert>
using namespace std;


string makeLine(int length)
{
string result = "";
int currentChar = 0;
while (currentChar < length)
{
result = result + "*";
currentChar++;
}
return result;
}

string makeIncreasingPart(int rows)
{
string result = "";
int currentRow = 1;
while (currentRow <= rows)
{
result = result + makeLine(currentRow) + "\n";
currentRow++;
}
return result;
}

string makeDecreasingPart(int rows)
{
string result = "";
int currentRow = 5;
while (currentRow <= rows)
{
result = result + makeIncreasingPart(currentRow) + "\n";
currentRow--;
}
return result;
}

string makeTriangle(int width)
{
string result = "";
int currentChar = 5;
while (currentChar > width)
{
result = result + "*";
currentChar++;
}
return result;
}

void testMakeLine()
{
assert(makeLine(1) == "*");
assert(makeLine(2) == "**");
assert(makeLine(3) == "***");
assert(makeLine(4) == "****");
assert(makeLine(5) == "*****");
assert(makeLine(20) == "********************");
}


void testMakeIncreasingPart()
{
assert(makeIncreasingPart(1) == "*\n");
assert(makeIncreasingPart(2) == "*\n**\n");
assert(makeIncreasingPart(3) == "*\n**\n***\n");
assert(makeIncreasingPart(4) == "*\n**\n***\n****\n");
assert(makeIncreasingPart(5) == "*\n**\n***\n****\n*****\n");
}


void testMakeDecreasingPart()
{
assert(makeDecreasingPart(5) == "*****\n****\n***\n**\n*\n");
assert(makeDecreasingPart(4) == "****\n***\n**\n*\n");
assert(makeDecreasingPart(3) == "***\n**\n*\n");
assert(makeDecreasingPart(2) == "**\n*\n");
assert(makeDecreasingPart(1) == "*\n");
}

void testMakeTriangle()
{
assert(makeTriangle(1) == "*\n");
assert(makeTriangle(2) == "*\n**\n*\n");
assert(makeTriangle(3) == "*\n**\n***\n**\n*\n");
assert(makeTriangle(4) == "*\n**\n***\n****\n***\n**\n*\n");
assert(makeTriangle(5) == "*\n**\n***\n****\n*****\n****\n***\n**\n*\n");
}


void runTests()
{
testMakeLine();
testMakeIncreasingPart();
testMakeDecreasingPart();
testMakeTriangle();
cout << "All good" << endl;
}


int main()
{
runTests();

for ( int i = 0; i < 10; i++)
{
cout << makeTriangle(i);
}

}
Last edited on
your code, indented
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <cassert>
#include <iostream>
#include <string>
using namespace std;

string makeLine(int length) {
	string result = "";
	int currentChar = 0;
	while(currentChar < length) {
		result = result + "*";
		currentChar++;
	}
	return result;
}

string makeIncreasingPart(int rows) {
	string result = "";
	int currentRow = 1;
	while(currentRow <= rows) {
		result = result + makeLine(currentRow) + "\n";
		currentRow++;
	}
	return result;
}

string makeDecreasingPart(int rows) {
	string result = "";
	int currentRow = 5;
	while(currentRow <= rows) {
		result = result + makeIncreasingPart(currentRow) + "\n";
		currentRow--;
	}
	return result;
}

string makeTriangle(int width) {
	string result = "";
	int currentChar = 5;
	while(currentChar > width) {
		result = result + "*";
		currentChar++;
	}
	return result;
}

void testMakeLine() {
	assert(makeLine(1) == "*");
	assert(makeLine(2) == "**");
	assert(makeLine(3) == "***");
	assert(makeLine(4) == "****");
	assert(makeLine(5) == "*****");
	assert(makeLine(20) == "********************");
}

void testMakeIncreasingPart() {
	assert(makeIncreasingPart(1) == "*\n");
	assert(makeIncreasingPart(2) == "*\n**\n");
	assert(makeIncreasingPart(3) == "*\n**\n***\n");
	assert(makeIncreasingPart(4) == "*\n**\n***\n****\n");
	assert(makeIncreasingPart(5) == "*\n**\n***\n****\n*****\n");
}

void testMakeDecreasingPart() {
	assert(makeDecreasingPart(5) == "*****\n****\n***\n**\n*\n");
	assert(makeDecreasingPart(4) == "****\n***\n**\n*\n");
	assert(makeDecreasingPart(3) == "***\n**\n*\n");
	assert(makeDecreasingPart(2) == "**\n*\n");
	assert(makeDecreasingPart(1) == "*\n");
}

void testMakeTriangle() {
	assert(makeTriangle(1) == "*\n");
	assert(makeTriangle(2) == "*\n**\n*\n");
	assert(makeTriangle(3) == "*\n**\n***\n**\n*\n");
	assert(makeTriangle(4) == "*\n**\n***\n****\n***\n**\n*\n");
	assert(makeTriangle(5) == "*\n**\n***\n****\n*****\n****\n***\n**\n*\n");
}

void runTests() {
	testMakeLine();
	testMakeIncreasingPart();
	testMakeDecreasingPart();
	testMakeTriangle();
	cout << "All good" << endl;
}

int main() {
	runTests();

	for(int i = 0; i < 10; i++) {
		cout << makeTriangle(i);
	}
}


> but something is not right here
you need to be more specific
the program freezes


running through a debugger (^C is a manual interrupt)
$ gdb a.out
> run
^C
> backtrace
#0  0x00007ffff7279999 in brk () from /usr/lib/libc.so.6
#1  0x00007ffff7279a79 in sbrk () from /usr/lib/libc.so.6
#2  0x00007ffff7215e19 in __default_morecore () from /usr/lib/libc.so.6
#3  0x00007ffff720fdda in systrim.isra () from /usr/lib/libc.so.6
#4  0x00007ffff7210ff7 in _int_free () from /usr/lib/libc.so.6
#5  0x0000000000401148 in makeDecreasingPart[abi:cxx11](int) (rows=5) at foo.cpp:30
#6  0x0000000000401789 in testMakeDecreasingPart () at foo.cpp:64
#7  0x0000000000401bd4 in runTests () at foo.cpp:82
#8  0x0000000000401c06 in main () at foo.cpp:88
> frame 5
#5  0x0000000000401148 in makeDecreasingPart[abi:cxx11](int) (rows=5) at foo.cpp:30
30                      result = result + makeIncreasingPart(currentRow) + "\n";
(gdb) list
25
26      string makeDecreasingPart(int rows) {
27              string result = "";
28              int currentRow = 5;
29              while(currentRow <= rows) {
30                      result = result + makeIncreasingPart(currentRow) + "\n";
31                      currentRow--;
32              }
33              return result;
34      }
there is the culprit code that's causing an infinite loop.
1
2
3
4
while(currentRow <= rows){
	//...
	currentRow--;
}

the string makeDecreasingPart and string makeTriangle codes i put weren't right i guess. that's why I'm asking for help because i did everything already and i can't still figure out whats the right code to put.
Look at the snippet of code again

Probably you don't need to create the "increasing" bit of the triangle while you're making the decreasing bit.

1
2
3
4
5
6
7
8
9
10
string makeDecreasingPart(int rows) {
  string result = "";
  int currentRow = 5;
  while(currentRow > 0) {
    result = result + makeIncreasingPart(currentRow) + "\n";
    result += makeLine(currentRow) + "\n";
    currentRow--;
  }
  return result;
}


You need to create a number (rows) lines of stars - you already have a function to do that. Check your loop bounds.
Last edited on
Topic archived. No new replies allowed.