0
How do I print a hollow hexagon with all side of equal length?
C language, I am super new to programming, dont know what a toolkit is but I use gcc and vim
12 Antworten
+ 1
Language? Toolkit?
+ 1
Post the code of the solid hexagon, please.
+ 1
Jon, can you please share a saved code link instead? it's easier to view than having to copy/paste raw text.
If you don't know how, you can follow this guide on how to share links 👍
https://www.sololearn.com/post/74857/?ref=app
0
C language, I dont know what a toolkit is.
0
Have you learned loops (for, while) and conditionals?
Some ideas:
* Using some printf and patience...
* using a lopp and and some math
* use a matrix as a "virtual canvas", make a functions to draw a line in it... With some math again to draw sides.. Then print the matrix to console...
0
Yes I have learned loops and conditionals and have created shaes like rectangle and triangle, circle but I couldnt figure out how to draw a hollow hexagon, I had some progress with a solid hexagon
0
#include <stdio.h>
int main(){
int l, j, i, k,length;
printf("Enter length : ");
scanf("%d",&length);
for (i = 1, k = length, l = 2 * length - 1;
i < length; i++, k--, l++)
{
for (j = 0; j < 3 * length; j++)
if (j >= k && j <= l)
printf("*");
else
printf(" ");
printf("\n");
}
for (i = 0, k = 1, l = 3 * length - 2;
i < length; i++, k++, l--)
{
for (j = 0; j < 3 * length; j++)
if (j >= k && j <= l)
printf("*");
else
printf(" ");
printf("\n");
}
return 0;
}
0
I tried to make it, but the output doesn't look so good, because the character used as stroke and padding are different in width.
https://code.sololearn.com/c0GNBkrjsV7q/?ref=app
0
Thanks for the attempt but seems like you made an octagon instead of a hexagon, btw here is my attempted code at making a hollow hexagon but it has one extra line and the corners arent pointy.
#include <stdio.h>
int main() {
int length = 0;
printf("Enter the size of the hexagon: ");
scanf("%d",&length);
for(int i = 0, k = length,l = 2 * length - 1; i < length; ++i, --k, ++l)
{
for(int j = 0; j < 3 * length; ++j)
{
if(i == 0)
{
if(j >= k && j <= l)
printf(" * ");
else
printf(" ");
}
else
{
if(j == k || j == l)
printf(" * ");
else
printf(" ");
}
}
printf("\n");
}
for(int i = 0, k = 1, l = 3 * length - 2; i < length; ++i, ++k, --l)
{
for(int j = 0; j < 3 * length; ++j)
{
if(i == length - 1)
{
if(j >= k && j <= l)
printf(" * ");
else
printf(" ");
}
else
{
if(j == k || j == l)
printf(" * ");
else
printf(" ");
}
}
printf("\n");
}
return 0;
}
0
jon wick
Try to change the first for-loop like this
for (int i = 0, k = length, l = 2 * length - 1; i < length - 1; ++i, --k, ++l)
0
Ipang
I got this code to work 90% but I am having alignment issues I cant solve
#include <stdio.h>
int main() {
int length_hex = 0;
printf("\nEnter an integer for length of each side: ");
scanf("%d", &length_hex);
for(int i_hex = 0, k_hex = length_hex,l_hex = 2 * length_hex - 1; i_hex < length_hex; ++i_hex, --k_hex, ++l_hex)
{
for(int j_hex = 0; j_hex < 3 * length_hex; j_hex++)
{
if(i_hex == 0)
{
if(j_hex >= k_hex && j_hex <= l_hex)
printf("* ");
else
printf(" ");
}
else
{
if(j_hex == k_hex || j_hex == l_hex)
printf("#");
else
printf(" ");
}
}
printf("\n");
}
for(int i_hex = 1, k_hex = 2, l_hex = 3 * length_hex - 3; i_hex < length_hex; ++i_hex, ++k_hex, --l_hex)
{
for(int j_hex = 0; j_hex < 3 * length_hex; j_hex++)
{
if(i_hex == length_hex - 1)
{
if(j_hex >= k_hex && j_hex <= l_hex)
printf("* ");
else
printf(" ");
}
else
{
if(j_hex == k_hex || j_hex == l_hex)
printf("quot;);
else
printf(" ");
}
}
printf("\n");
}
return 0;
}
0
Jon, sorry for delay. I didn't really get what is meant by alignment issue. Can you elaborate what is aligned?