How to print diamond shape w/ c++

8.4k views Asked by At

I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:

  *
 ***
*****

but I decided to challenge myself and see if I could create a simple diamond shape like this:

  *
 ***
*****
 ***
  *

Here is my code so far. I should also add that the value you input, for example 5, determines how big the diamond is.

#include <iostream>
#include <sstream>
using namespace std;

int main() {




int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;


for (int i = 0; i < value; i++) {
    //print spaces v v v
    for (int x = 0; x < (value - i - 1); x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < (2 * i + 1); y++) {
        cout << "*";
    }
    cout << endl;
}

for (int i = 0; i < value; i++) {
    int number = 0;
    number+= 2;
    //print spaces v v v
    for (int x = 0; x < (value - value + i + 1); x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
        cout << "*";
    }
    cout << endl;
}



return 0;
}

What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.

I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.

I also apologize if my code is messy and hard to read.

3

There are 3 answers

0
ZerosAndOnes On BEST ANSWER

int number = 0; and number+= 2;

value - value inside for (int x = 0; x < (value - value + i + 1); x++) {

are not required.


Inside the parenthesis, you can use

2*(value-i-1)-1

However, I would suggest you to first analyze the problem and then try to solve it instead of trying random things. For instance, let's consider the cases of even and odd inputs i.e., 2 and 3.

Even Case (2)

 *
***
***
 *

The Analysis

Row Index    Number of Spaces    Number of Stars
0            1                   1
1            0                   3
2            0                   3
3            1                   1

For row index < value

  1. Number of Spaces = value - row index - 1
  2. Number of Stars = 2 * row index + 1

For row index >=value The number of spaces and stars are simply reversed. In the odd cases, the situation is similar too with a small exception.

Odd Case (3)

  *
 ***
*****
 ***
  *

The Analysis

Row Index    Number of Spaces    Number of Stars
0            2                   1
1            1                   3
2            0                   5
3            1                   3
4            2                   1

The small exception is that while reversing, we have to ignore the row index = value.


Now, if we put the above analysis in code we get the solution

//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
    //print spaces v v v
    for (int x = 0; x < value - rowIndex -1; x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < 2 * rowIndex + 1; y++) {
        cout << "*";
    }
    cout << endl;
}

And then inside main

//Row index < value
for (int i = 0; i < value; i++) {
    PrintDiamond(i,value);
}

//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
    PrintDiamond(i,value);
}
0
Aaron Cyrman On

This code should resolve the problem:

#include <sstream>
using namespace std;

void printSpaces(int howMany) {
    for(int i = 0; i < howMany; i++) cout << " ";
}

void figure(int size) {
    bool oddSize = size % 2 == 1;
    int center = size / 2;
    int spaces = size / 2;
    // If figure is of an odd size adjust center
    if (oddSize) {
        center++;
    } else { // Else if figure is of even size adjust spaces
        spaces--;
    }

    for (int i = 1; i <= center; i++) {
        printSpaces(spaces);
        for(int j = 0; j <  1 + (i - 1) * 2; j++) cout << "*";
        cout << endl;
        spaces--;
    }

    spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
    center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
    for(int i = center; i >= 1; i--) {
        printSpaces(spaces);
        for(int j = 0; j <  1 + (i - 1) * 2; j++)
           cout << "*";
        cout << endl;
        spaces++;
    }

}

int main() {
    int value = 0;
    while(value < 3) {
        cout << "Please enter in a value (>= 3): ";
        cin >> value;
        cout << endl;
    }
    figure(value);
    return 0;
}
0
AudioBubble On
#include <iostream>
#include <sstream>
using namespace std;

int main() {

int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;


for (int i = 0; i < value; i++) {
    //print spaces v v v
    for (int x = 0; x < (value - i - 1); x++) {
        cout << " ";
    }
    //print * v v v
    for (int y = 0; y < (2 * i + 1); y++) {
        cout << "*";
    }
    cout << endl;
}

for (int i = 0; i < value-1; i++) {
//    int number = 0;
//    number+= 2;
//    //print spaces v v v

    for (int x = 0; x < i+1; x++) {
        cout << " ";
    }
    //print * v v v
     for (int y = 0; y < (2*(value-1-i)-1); y++) {
        cout << "*";
        }
    cout << endl;
}



return 0;
}

I hope that you will get this .Also in the second for loop you were iterating it one extra time by iterating the loop upto value. But since the pyramid is symmetric so the no of rows in the pyramid will be 2*value-1.So I in the second loop i should vary upto value -1.