I need help! I don't know what's the problem

can anyone help me!?
I keep getting errors for my function makeDecreasingPart.

This is my code:

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
94
95
96
97
98
99
100
101
102
103
104
#include <string>
#include <iostream>
#include <cassert>
using namespace std;

string makeLine(int length)
{	
	string j;
	for(int i=1; i <= length; i++)
	{
        j+="*";
    }
    return j;
}

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

string makeDecreasingPart(int rows)
{
    string result = "";
	int curRow = 1;
	while(curRow <= rows)
	{
		result = result + makeLine(curRow) + "\n";
		curRow--;
	}
    return result;
}

string makeTriangle(int width)
{	 
	return makeIncreasingPart(width) + makeDecreasingPart(width);
}

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
First, please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post.

Second, what errors? Be specific.
So for my [string makeDecreasingPart(int rows)], I get error.
C:\Users\jaebo\Downloads\lab-8.5>a.exe
Assertion failed: makeDecreasingPart(5) == "*****\n****\n***\n**\n*\n", file triangle.cpp, line 75


this one
Ok, lets look at the code and think through, what happens on a "simpler" makeDecreasingPart(1) case:
1
2
3
4
5
6
7
8
9
10
11
string makeDecreasingPart(int rows)
{
    string result = "";
	int curRow = 1;
	while(curRow <= rows)
	{
		result = result + makeLine(curRow) + "\n";
		curRow--;
	}
    return result;
}

Line 1: rows==1
Line 4: curRow==1
Line 5: 1<=1 is true
Line 7: result=="*\n"
Line 8: curRow==0

Line 5: 0<=1 is true
Line 7: result=="*\n\n"
Line 8: curRow==-1

Line 5: -1<=1 is true
Line 7: result=="*\n\n\n"
Line 8: curRow==-2

...

That is a long loop.

You have to rethink the initial value of curRow and the condition that ends the loop.
Topic archived. No new replies allowed.