Hey guys,
I have a rough code here that displays a Square asterisk, Oval shaped asterisk, Arrow asterisk and a Diamond asterisk, and it displays vertically, but I cant figure out how to display my output horizontally.

here's my code so far,

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int i=0, j=0, NUM=3,r=0, s=0, iNUM=3, x, h, k, b;
   for(int col=0; col<4; col++) 
   {    
       cout <<endl<<endl;

       for(int row=0; row<4; row++)
           {
             if((col==0 || col==4-1) || (row==0) || (row ==4-1))  
             {                                                              
               cout << " * ";
             }
             else cout<<"   ";
           }
   }
/*Oblong*/   
    cout<<"\n\n";
       for(x=1;x<=6;x++)
    {
        if(x==1 || x==6)
        {
            for(h=1;h<=7;h++)
            {
                if(h==3 || h==4 || h==5)
                    cout<<"*";
                else
                    cout<<" ";
            }
            cout<<endl;
        }
        else
            if(x==2||x==5)
            {
                for(k=1;k<=7;k++)
                {
                    if(k==2||k==6)
                        cout<<"*";
                    else
                        cout<<" ";
                }
                cout<<endl;
            }
            else
            {
                for(b=1;b<=7;b++)
                {
                    if(b==1||b==7)
                        cout<<"*";
                    else
                        cout<<" ";
                }
                cout<<endl;

            }
    }

/*arrow*/
int count = 0; 
cout<<"\n";
for (int width = 0; width <= 3; width++) 
{ 
for (int alignLeft = width; alignLeft <= 2; alignLeft++) 
{ 
cout << " "; 
} 
for (int space = 1; space < count; space++) 
{ 
cout << "*"; 
} 
cout << endl; 
count += 2; 

} 
cout<<"  *  \n";
cout<<"  *  \n";
cout<<"  *  \n";
cout<<"  *  \n";

/*diamond*/
cout<<"\n";
for(r=-iNUM; r<=iNUM; r++)
{
for(s=-iNUM; s<=iNUM; s++)
{
if( abs(r)+abs(s)==iNUM) 

 { 
 cout<<"*"; }
else { cout<<" ";}
}
cout<<endl;
}

    getch();
    return 0;

}

I would be glad if you guys can help out.
still a rookie here. :))
-Dan

Dani AI

Generated

the reason everything stacks vertically is that each shape prints all of its lines and then ends with a newline. If you want them side-by-side, render each shape into lines first, then print row 0 of every shape, newline, row 1 of every shape, and so on. Padding shorter shapes to the same width keeps columns aligned.

Here is a small pattern you can drop in. Define each shape as text, then stitch them horizontally:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using std::string; using std::vector; using std::cout;

void printSideBySide(const vector<vector<string>>& shapes, size_t gap = 3) {
    size_t H = 0; vector<size_t> W;
    for (const auto& s : shapes) {
        size_t w = 0; for (const auto& line : s) w = std::max(w, line.size());
        W.push_back(w); H = std::max(H, s.size());
    }
    string sep(gap, ' ');
    for (size_t r = 0; r < H; ++r) {
        for (size_t i = 0; i < shapes.size(); ++i) {
            string line = (r < shapes[i].size()) ? shapes[i][r] : "";
            line += string(W[i] - line.size(), ' ');
            cout << line << (i + 1 < shapes.size() ? sep : "");
        }
        cout << '\n';
    }
}

int main() {
    vector<string> square = {"****","*  *","*  *","****"};
    vector<string> oval   = {" *** ","*   *","*   *"," *** "};
    vector<string> arrow  = {"  *  "," *** ","*****","  *  ","  *  "};
    vector<string> diamond= {"  *  "," * * ","*   * "," * * ","  *  "};
    printSideBySide({square, oval, arrow, diamond});
}

If by "horizontally" you meant rotate a shape 90 degrees, is right: flip row/column indices. The typical mapping is out[x][H-1-y] = in[y][x] inside your nested loops. Also, you can drop conio.h and getch(); using standard I/O keeps the code portable.

To make anything rotate from upright to on its side just flip column/row values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.