Draw the shape of the letter X with an asterisk (*)

I want to write a program to draw the shape of the letter X using an asterisk (*)

#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[]){
  int i, j;

  for(i = 1; i <= 9; i++){
    for(j = 1; j <= 12; j++){
      if(i == j){
        cout << "***";
      }else{
        cout << " ";
      }
    }
    cout<< endl;
  }

  return 0;
}

      

I am very new to programming

i just did (\) how can i make integer X

***------***
-***----***-
--***--***--
---******---
--***--***--
-***----***-
***------***  

      

that what i did uptill now

include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
int i, d, a=1,b=12,c;
for(i = 1; i <= 6; i++)
{
for (d=1; d<i;d++)  {cout <<" ";}
cout<<"***";
for (c=a+1; c<b;c++) {cout <<" ";}
{cout<<"***";}
for(b=12-i;b<i;b++) 
{cout<<"***";}
cout<<endl;
a++;
}
return 0;
}       

      

i split the vertex (\ //) into three parts [Space] [] [space] []

+3


source to share


8 answers


I wrote the following function / method in java. You can convert it to C ++;

public static void printX(int x) {
    char[] chars = new char[x];
    for (int i = 0; i < x; i++) {
        chars[i] = '*';
        chars[x - 1 - i] = '*';
        for (int j = 0; j < x; j++) {
            if (j == i || j == (x - 1 - i)) {
                continue;
            }
            chars[j] = ' ';
        }
        System.out.println(new String(chars));
    }

}

      



If you call the above function / method as printX (5); The output will be 5x5 and contain an X.

*   *
 * * 
  *  
 * * 
*   *

      

+3


source


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

      

First, excuse my very uneven X. For you as a beginner, I would highlight algo for you to ponder, not a spoonful when feeding the code.



  • The interpreter does not know how to return to a line that was already a printer. Therefore, you would need to draw both sides of the X in one iteration of the loop.

  • After that you decrease the number of stars (star) and draw line # 2.

  • Repeat until the midpoint mark when your stars are 0.

  • When your code sees that the stars are 0, start with the same loop, but this time with star ++ on each iteration.

  • This repeats up to the starting point of the star ie 4 in my case.

If you have any problems, you can post your code on the site :)

+1


source


You have to dynamically evaluate the spacing on each line. Draw by hand the desired shape on a piece of paper and try to create a function that takes a line number as an argument and returns the number of spaces required on a particular line. For example:

*   *
 * * 
  *
 * *
*   *

      

The number of spaces in each line is:

0 [*] 3 [*]
1 [*] 1 [*]
2 [*] 
1 [*] 1 [*]
0 [*] 3 [*]

      

Note that inside each row, you will need two loops: first for the start and middle spaces.

0


source


The solution I wrote 2 decades ago (when I was still studying):

  • Make an array of row-columns eg. char screen[80][25];

  • Clean it up by setting all entries to ' '

  • Draw Point at x, y by setting screen[x][y]='*';

  • When finished, paint the whole thingscreen[80][25]

    , calling cout

    2080 times. (2000 times for characters and 80 times for endl

    )

In your case, you know how to draw \

. You can adapt this easily. But with my method you can draw /

on the same array screen

. And when you're done, in the last step, you have overlapping /

and \

:X

I used this method as we had to draw a circle and it was much more difficult. And yes, nowadays I would probably use std::vector<std::string> screen

, but then the screens were really 80x25 :)

0


source


#include "stdafx.h"
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
int  i, d, a=1,b=12,c ,e=1;
        for(i = 1; i <= 6; i++)
            {
                for (d=1; d<i;d++) {cout <<" ";}
                cout<<"***";
                for (c=a+1; c<b;c++) {cout <<" ";}
                {cout<<"***";}
                for(b=12-i;b<i;b++) 
                {cout<<"***";}
                cout<<endl;
                a++;
        }
for( i = 1; i <= 3; i++)
        {
            for ( d=6; d>i;d--) {cout <<" ";}
            cout<<"***";
            for (c=0; c<e-1;c++) {cout <<"  ";}
            {cout<<"***";}
            cout<<endl;
            e++;
    }

return 0;
}   

      

0


source


int n=11,i=0,k=0,j=0;
for(i=0;i<n;i++)
{
if(i<(n/2)) 
 {
    cout<<endl;
    for(j=0;j<i;j++)
    {
        cout<<" ";
    }
    cout<<"*";
    for(k=n/2;k>i;k--)
    {
        cout<<"  ";
    }
    cout<<"*";
 }
else
 {
    cout<<endl;
    for(k=n-1;k>i;k--)
    {
        cout<<" ";
    }
    cout<<"*";
    for(j=n/2;j<i;j++)
    {
        cout<<"  ";
    }
    cout<<"*";
 }  
}

      

0


source


#include<iostream>
using namespace std;
int main()
{
    int i, j;
    for(i = 1;i<= 5;i++)
    {
        for(j = 1;j<= 5;j++)
        {
            if((i == j)||(j==(5+1)-i))
            {
                cout << "*";
            }
            else{   
                cout << " ";
            }
        }
        cout<< endl;
    }
    system("pause");
}

      

0


source


#include "stdafx.h"
#include <iostream>
using namespace std;;


int _tmain(int argc, _TCHAR* argv[])
{
    int i,k,j;
    for (i=1;i<8;i++)
    {
        for (int k=0;k<i;k++)
        {
            cout<<" ";
        }
        cout<<"*";
        for (int k=8;k>i;k--)
        {
            cout<<"  ";
        }
        cout<<"*";
        cout<<endl;
    }
    for (i=1;i<8;i++)
    {
        for (int k=8;k>i;k--)
        {
            cout<<" ";
        }
        cout<<"*";

        for (int k=0;k<i;k++)
        {
            cout<<"  ";
        }
        cout<<" *";
        cout<<endl;
    }

    system("Pause");
    return 0;
}

      

-1


source







All Articles