Thread: Shows Blank screen while printing 2d array

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    24

    Shows Blank screen while printing 2d array

    Guys I am trying to write a code for solving the 8 queen problem But the Problem is I cannot Print out the Result. There is something really weird going on. the function showsol is not doing its job. It is messed up for some reason. Can anyone tell me why the 2d Array is not being printed by the "Showsol" Function?. It leaves the whole screen blank.


    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int cb[8][8]={{0},{0},{0},{0},{0},{0},{0},{0}};
    
    int test(r,c)
    {
    
        if(cb[r][c]!=0)
        {
            return 0;
        }
        else
            return 1;
    
    }
    void mark(int r,int c)
    {
        int a;
        int x=r,y=c;
        for(a=0;a<8;a++)
        {
            cb[r][a]=1;
    
    
        }
        for(a=0;a<8;a++)
        {
            cb[a][c]=1;
        }
        while(x>0&&y>0)
        {
            cb[--x][--y]=1;
        }
        x=r;
        y=c;
        while(x<8&&y<8)
        {
            cb[++x][++y]=1;
        }
        cb[r][c]=2;
    }
    
    void unmark(int r,int c)
    {
        int a;
        for(a=0;a<8;a++)
        {
            cb[r][a]=0;
        }
        for(a=0;a<8;a++)
        {
            cb[a][c]=0;
        }
        while(r>=0&&c>=0)
        {
            cb[--r][--c]=0;
        }
        while(r<8&&c<8)
        {
            cb[++r][++c]=0;
        }
        cb[r][c]=0;
    }
    
    void showsol()
    {
        int count1,count2;
    
        printf("The Result is\n");
    
        for(count1=0;count1<8;count1++)
        {
            for(count2=0;count2<8;count2++)
            {
                printf(" %d ",cb[count1][count2]);
            }
            printf("\n");
        }
    }
    
    
    void queen8(int n)
    {
        int i;
        for(i=0;i<8;i++)
        {
            if(test(n,i)==1)
            {
    
                mark(n,i);
                if(n<7)
                    queen8(n+1);
    
                else if(n==7)
                {
                    showsol();
                    getch();
                    exit(0);
                }
            }
    
        }
        unmark(n,i);
    }
    
    int main()
    {
    
        clrscr();
    
        getch();
        queen8(0);
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I have no idea what an 8 queen problem is, but after removing #include <conio.h>, removing the useless clrscr() and getch() calls and #include <stdlib.h> ( for the call to exit() ) I got this output
    Code:
    The Result is
     1  1  1  1  1  1  1  1 
     1  1  2  1  1  1  1  1 
     1  2  1  1  1  1  1  1 
     1  1  1  1  1  1  1  1 
     1  1  1  1  1  1  1  1 
     1  1  1  1  1  1  1  2 
     2  1  1  1  1  1  1  1 
     1  1  1  1  2  1  1  1
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    24
    Yea I got an Output but it looks like a 2d Array containing Invalid characters( Got some Smily Face). haha.

    Whats wrong....

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Are you sure you posted the same code that you are compiling? I suspect you are using printf with a %c conversion specifier instead of %d. If I remember correctly, some legacy operating systems (DOS and Windows) displayed smiley faces when you print characters 1 and 2.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    24
    Tried printing with 5 and 9. Got Spades Now haha. Is this my code or my Computer? or my compiler? And yeah thats the same code....

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sunny` View Post
    Tried printing with 5 and 9. Got Spades Now haha. Is this my code or my Computer? or my compiler? And yeah thats the same code....
    Re-read post #4 again then because it seems unfathomable that the bug could be anything but a wrong format specifier.
    Then retype line 76, changing some some spaces to other characters just to prove that it is compiling the code and not just running a previously built exe.
    Then if that still produces the problem, get a new compiler.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Close the IDE (if it is one), and reboot the computer. Then rebuild (not just recompile), the program. (and as mentioned above, recheck your format specifier). You're printing ASCII symbols, (they start with two faces, and then have the four card suit symbols: ☺☻♥♦♣♠, etc.). If you don't have an ASCII chart, you may want to get one from the net. Quite handy to have one for reference.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    The graphic symbols below 32 and above 126 are not part of ASCII but are specific to DOS. See Code page 437 - Wikipedia, the free encyclopedia

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sorry, but they ARE one of the ASCII character tables, and they are on not just DOS systems: WindowsXP and Windows 7, both use them. I believe Linux does, as well.

    Every system with a text display, has to have a character table of values. They're not called ASCII anymore (if the correct terminology is used today), but they are still generically called "ASCII", for want of a better name.

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    There is only one character set properly called "ASCII": the standard ASCII character set (aka ISO646 US). Some people call other derived or extended sets "ASCII" (like the non-standard DOS code page 437), but they're not really ASCII. Like I said, only characters 32 through 126 are graphical in ASCII.

    ASCII - Wikipedia, the free encyclopedia

    On Linux and most other systems the non-printable characters truly are non-printable. Most of the characters are used to control the position of the cursor or change attributes of the terminal in most terminals (VT-100 and later being the most common emulated terminal).

    Other standard character sets like ISO8859-1 are extensions of ASCII. ISO8859-1 defines graphics for characters 160-255 in addition to 32-126 from ASCII but still leave 0-31, 127, and 128-159 non-printable. This is, in fact, the default character set for HTML.

    Edit: ASCII and ISO8859-1 (and others) are actually character encodings. The standard character set is Unicode for all those encodings.
    Last edited by christop; 07-29-2012 at 02:51 PM. Reason: clarify character encoding vs set

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've already shown you that several lower value ascii values will indeed print a char.

    You can call them character "encodings" all you want. They used to be called character sets.

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Adak View Post
    I've already shown you that several lower value ascii values will indeed print a char.
    Those lower codes printed precisely because the character set in use was not ASCII. It's CP437, which has printable characters below 32. CP437 is not ASCII. It is an extension to ASCII. Shift JIS is also an extension to ASCII, but I certainly wouldn't call it ASCII. Incidentally, C's isprint() function/macro will return 0 for those characters on ASCII-based system because they are in fact unprintable in ASCII.

    You can call them character "encodings" all you want. They used to be called character sets.
    Fine, I won't argue about that. However, I will discuss the reasoning behind calling them character encodings.

    Having many different character sets complicates information interchange (even just different extensions to ASCII, like the various DOS codepages), so Unicode was designed to be a standard character set containing almost every character imaginable (the latest standard consists of >110000 characters). Common character sets like ASCII (true ASCII, not some DOS codepage claiming to be ASCII) are now considered to be character encodings of the Unicode character set, as they are a method of mapping Unicode character code points to data at the byte level. Since Unicode is based on ASCII, each ASCII code maps directly to a Unicode character (ASCII code 65, 'A', is Unicode code point U+0041 (65 decimal), "LATIN CAPITAL LETTER A"). So it should be no surprise that Unicode characters below U+0020 (decimal 32) are also unprintable. Of course, ASCII is a 7-bit encoding so it can map only the first 128 Unicode code points. Other encodings like UTF-8 (which is backwards-compatible with ASCII for single-byte encodings) and UTF-16 can encode a much wider range of characters.

    Now my rant mode is fully engaged.

    This leads nicely to wide characters in C, which is relevant to this forum (even if not this topic). I believe most Unix systems define wchar_t to be a 32-bit integer type, so it can directly express the full range of Unicode characters. In Windows it seems to be defined as a 16-bit integer type, which limits the range, but other than that it doesn't make much difference. Anyway....

    When you print a wide string from C like this:
    Code:
    printf("%ls\n", L"This is a wide string!");
    C has to encode the output so it can be printed by the terminal or written to a file in the appropriate character encoding (such as ASCII, Windows-1252, or even Shift JIS). The encoding that is used is determined by the locale settings in the C program. Unix systems typically have the LANG and LC_* (LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, LC_MONETARY, LC_NUMERIC, LC_TIME) environment variables to set the locale. These names correspond to the C constants used by the setlocale() function. I'm not sure how C programs on Windows systems determine which locale to use, but I'm sure it's some non-standard way.

    I speak English and live in the US, so I use one of the "en_US" locales. My current Linux system has the following en_US locales available:
    * en_US
    * en_US.iso88591
    * en_US.iso885915
    * en_US.utf8

    I use the last (utf8) so any program that prints a wide string will output the string in the UTF-8 character encoding. This is so I can use characters not available with the other encodings, such as... actually I can't think of any character outside of ISO8859-1 that I use regularly. Oh well. But if I wanted to learn Japanese or some other language with a large character set I'd be set!

    Slightly off-topic, I'm writing a small Unix-like system myself, and to keep the whole system simple (it runs on a computer with only 256KB of RAM and 4MB of Flash ROM) I'm going to support only the ISO8859-1 character encoding, at least in the virtual terminal. The glyph tables are relatively small (about 3KB per glyph set) and decoding characters to glyphs is trivial. (Of course, any user program can supply its own glyph table and decoder to display characters not available in ISO8859-1, but that's not my kernel's concern).

    Anyway, rant mode is off.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Generically, we've been calling the most common char set "ASCII" for over 30 years. And that's where our differences lie, clearly. You prefer the international standards, and I'm not about to refer students to dig through that stuff, when they can easily d/l an ASCII table and be off and running, so easily.

    The newer international char set standards certainly have a place, but I don't believe it's needed for this thread.

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    24
    Thanks guys my problem is solved. But Dont let me stop your discussions.

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Adak View Post
    You're printing ASCII symbols, (they start with two faces, and then have the four card suit symbols: ☺☻♥♦♣♠, etc.). If you don't have an ASCII chart, you may want to get one from the net. Quite handy to have one for reference.
    To backup christop:
    Assume a newbie searches for "ascii chart" on google to get one from the net, can you see smilies or card symbols on any of the pages you get as a result (I've just checked the first five pages and I'm pretty sure most people won't dig deeper). So how does it help a student to use the term "ASCII" as ambiguous as you do?

    See also ASCII (especially the first italic comment right below the heading) and Extended ASCII (especially the first paragraph) on Wikipedia.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blank screen, while executing the program
    By iamrizwan in forum C Programming
    Replies: 7
    Last Post: 03-31-2012, 09:36 AM
  2. Blank output screen after compiling C.
    By thunderdome in forum C Programming
    Replies: 2
    Last Post: 01-19-2006, 06:53 PM
  3. blank output screen !!
    By samirself in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2005, 08:26 AM
  4. get rid of my blank screen!
    By lambs4 in forum Game Programming
    Replies: 6
    Last Post: 01-22-2002, 09:15 AM
  5. Printing an Array to the screen
    By simhap in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2001, 11:16 AM