开发者

How to create a spiral and a checker board in C with the help of for loops

开发者 https://www.devze.com 2023-01-26 19:00 出处:网络
My coding is like this - it is for the spiral. I tried to do it but it do开发者_JAVA技巧esn\'t work. If any one has any idea how to do it please advise.

My coding is like this - it is for the spiral. I tried to do it but it do开发者_JAVA技巧esn't work. If any one has any idea how to do it please advise.

#include "stdio.h"
#include "conio.h"

void main ()
{
    int p,q;

    for(p=0; p<100; p++)
    {
        printf("alt+219");\\where alt+219 is an ASCII character in C\\
    }
    for(q=0; q<100; q++)
    {
        printf("alt+220");\\where alt+220 is an ASCII character in C\\
    }
}


One interesting way of doing these "text graphics" kind of problems I encountered (when such problems were in vogue) is to have a 2D loop, and then use a function to decide what to print using a function, rather than to have multiple loops.

For example, for a criss-cross pattern, we could define char_to_print(x, y) = 'x' if ((x+y)%2 == 0) else ' ' (this is not C/C++).

Just some food for thought.


If you want to put them into a literal string, hex for 219 is DB and for 220 is DC so you can output '\xDB' or '\xDC'.

You could also use %c in printf and put the numbers in thus printf("%c", 219)

If you are going to go into UTF-16 and use wprintf you can use %lc with the code you want. %c will automatically perform btowc on your value.

BTW, main should return int, not void.

0

精彩评论

暂无评论...
验证码 换一张
取 消