Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Here is my homework problem for C++:

The following algorithm is a solution to a problem that uses nested loops to display a diamond pattern. Translate the algorithm below in to a C++ program. Build, run and test the program.

Algorithm Solution:

Start

Declare numRows as constant integer = 7
Declare maxPlus as constant integer = 7
Declare numSpaces as integer
Declare numPluses as integer
Declare row as integer
Declare space as integer
Declare plus as integer

Set row = 1

Repeat while row >= 1 AND row <= numRows
    Set numPluses = 2 * row - 1
    if(numPluses > maxPlus) then
        Set numPluses = 14 - numPluses
    endif

    Set numSpaces = (maxPlus - numPluses) / 2

    Set space = 1
    Repeat while space >= 1 AND space <= numSpaces
        Display( ' ')
        Set space = space + 1
    End Repeat

    Set plus = 1
    Repeat while plus >= 1 AND plus <= numPluses
        Display( '*')
        Set plus = plus + 1
    End Repeat  

    Set row = row + 1

    Display a new line
End Repeat

Stop

My code:

   #include <iostream>
   using namespace std;

   int main() {

    const int numRows = 7;
    const int maxPlus = 7;
    int numSpaces;
    int numPluses;
    int row;
    int space;
    int plus;

    row = 1;

    while((row >=1) && (row <= numRows)){
        numPluses = 2 * row - 1;
        if(numPluses > maxPlus){
            numPluses = 14 - numPluses;
        }
        numSpaces = (maxPlus - numPluses)/ 2;
        space = 1;
        while((space >= 1) && (space <= numSpaces)){
           cout << " ";
           space++;
        }
        while((plus >= 1) && (plus <= numPluses)){
            cout << "*";
            plus++;
        }
        row++;
        cout << endl;
    }

    return 0;
    }

My question is why am I not getting the diamond pattern? I feel like I translated the algorithm correctly, but all I get is a bunch of blank space. Did I read the problem wrong or did I code wrong? screenshot of C++ code

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
141 views
Welcome To Ask or Share your Answers For Others

1 Answer

Before the last while loop:

Set plus = 1

You missed that in your translation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...