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] []
source to share
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.
* *
* *
*
* *
* *
source to share
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
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 :)
source to share
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.
source to share
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 thing
screen[80][25]
, callingcout
2080 times. (2000 times for characters and 80 times forendl
)
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 :)
source to share
#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;
}
source to share
#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;
}
source to share