weyhei
weyhei

Reputation: 479

Print a diamond shape with a borderusing asterisks in C/C++

I need to print out a diamond shape with a border using asterisks in C/C++

The code below renders only the diamond, but what I need is one what a border. what should I do?

Expectation:

diamond with border

Reality:

diamond without border

#include <iostream>

void PrintDiamond(int iSide) {
    using namespace std;
    int iSpaces = iSide;
    int iAsterisks = 1;
    // Output the top half of the diamond
    for (int iI = 0; iI < iSide; ++iI) {
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        cout << endl;
        --iSpaces;
        iAsterisks += 2;
    }
    // Output the bottom half of the diamond
    for (int iI = 0; iI <= iSide; ++iI) {
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        cout << endl;
        ++iSpaces;
        iAsterisks -= 2;
    }
}

int main()
{
    // Print a diamond with side = 4
    PrintDiamond(4);

    return 0;
}

Upvotes: 0

Views: 9430

Answers (5)

Don Question
Don Question

Reputation: 11614

My proposed alternativ:

#include <iostream>
const int ROWS = 11, COLS = 11, HMID = COLS/2, VMID = ROWS/2;

int main(){
    int c, r, d = -1;
    for(r=0; r < ROWS; r++){
        for(c=0; c< COLS; c++){
            char pixel = ' ';
            if ((r*c) >= (r*(HMID-d))) pixel = '*';
            if ((r*c) > (r*(HMID+d))) pixel = ' ';
            if (c == 0 || r == 0 || r == ROWS-1 || c == COLS-1) pixel='*' ;
            std::cout << pixel;
        }
        if (r < VMID) d++; else d--;
        std::cout << std::endl;
    }
}

you could still shorten it by 1 line if you discard the CONSTANT declarations and put them directly into place, replacing COLS,ROWS, HMID, VMID with their values.

Upvotes: 0

Hicham
Hicham

Reputation: 979

code : (not a lot of modifications from original)

void PrintDiamond(int iSide) {
using namespace std;
int iSpaces = iSide;
int width = (iSide)*2+1+2 //number of spaces + number of '*' + the first and last '*'
int iAsterisks = 1;
// Output the top half of the diamond
//first line : 
for (int iI = 0; iI < width; ++iI) 
{
    cout<<"*";
}
cout << endl;

for (int iI = 0; iI < iSide; ++iI) {
    cout<<"*";
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    for (int iJ = 0; iJ < iAsterisks; ++iJ) {
        cout << "*";
    }
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    cout<<"*";
    cout << endl;
    --iSpaces;
    iAsterisks += 2;
}
// Output the bottom half of the diamond
for (int iI = 0; iI <= iSide; ++iI) {
    cout<<"*";
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    for (int iJ = 0; iJ < iAsterisks; ++iJ) {
        cout << "*";
    }
    for (int iJ = 0; iJ < iSpaces; ++iJ) {
        cout << " ";
    }
    cout << "*";
    cout << endl;
    ++iSpaces;
    iAsterisks -= 2;
}
//last line :
//first line : 
for (int iI = 0; iI < width; ++iI) 
{
    cout<<"*";
}
cout << endl;
}

Upvotes: 0

DanZimm
DanZimm

Reputation: 2568

It seems to me you are only missing the border. you could add in a static line at the top and bottom of "*"'s and then a * at the beginning and ending of each row

something like this...

#include <iostream>

void PrintDiamond(int iSide) {
    using namespace std;
    int iSpaces = iSide;
    int iAsterisks = 1;
    for (int k = 0; k < iSide*2+3; ++k) {
        cout << "*";
    }
    cout << endl;
    // Output the top half of the diamond
    for (int iI = 0; iI < iSide; ++iI) {
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;
        --iSpaces;
        iAsterisks += 2;
    }
    // Output the bottom half of the diamond
    for (int iI = 0; iI <= iSide; ++iI) {
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;
        ++iSpaces;
        iAsterisks -= 2;
    }
    for (int k = 0; k < iSide*2+3; ++k) {
        cout << "*";
    }
    cout << endl;
}

int main()
{
    // Print a diamond with side = 4
    PrintDiamond(4);

    return 0;
}

hope this helps!

EDIT: Oop had some issues didnt think it all the way through then when i figured it out the guy above me had the same thing xD

Upvotes: 0

pjhades
pjhades

Reputation: 2038

#include <iostream>

void PrintDiamond(int iSide) {
    using namespace std;
    int iSpaces = iSide;
    int iAsterisks = 1;
    // Output the top half of the diamond

    // ADDED: here you print the top border, the number of * is
    // calculated from iSide
    for (int i = 0; i < iSide*2+3; i++)
        cout << "*";
    cout << endl;


    for (int iI = 0; iI < iSide; ++iI) {
        // ADDED: print one * of the left border, and several blanks
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }

        // here is your original code that prints the main part
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }

       // ADDED: the same as the left border, we print blanks and then
       // one * of the right border
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;

        --iSpaces;
        iAsterisks += 2;
    }
    // Output the bottom half of the diamond
    for (int iI = 0; iI <= iSide; ++iI) {
        cout << "*";
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        for (int iJ = 0; iJ < iAsterisks; ++iJ) {
            cout << "*";
        }
        for (int iJ = 0; iJ < iSpaces; ++iJ) {
            cout << " ";
        }
        cout << "*";
        cout << endl;
        ++iSpaces;
        iAsterisks -= 2;
    }

    // ADDED: we end up with the bottom border, which is the same as the top one
    for (int i = 0; i < iSide*2+3; i++)
        cout << "*";
    cout << endl;
}

int main()
{
    // Print a diamond with side = 4
    PrintDiamond(4);

    return 0;
}

Upvotes: 3

Matteo Italia
Matteo Italia

Reputation: 126777

Well, that's not so difficult... for the top and the bottom of the box a for that prints a full row of asterisks is enough. As for the other sides, it's enough to add an asterisk at the beginning and at the end of each line.

Upvotes: 1

Related Questions