Thread: Help with for loops and arrays

  1. #1
    Registered User Strik3r's Avatar
    Join Date
    Nov 2011
    Location
    Sweden
    Posts
    7

    Smile Help with for loops and arrays

    Hi

    I'm really new at C-programing so i would be very happy if someone could help me.

    I have a project on C HW/SW where I am supposed to build a program for a VGA-display.

    One of the things the program shall do is to print a letter on the display with one color and the rest of the screen with another color.

    I only need 3 letters: A,B,C

    so i thought that I could use Arrays built up on 1/0 to create the letter, like this.

    insert
    Code:
     
    unsigned char a[8][8] = {
     {0,0,0,1,1,0,0,0},
     {0,0,1,0,0,1,0,0},
     {0,1,0,0,0,0,1,0},
     {0,1,0,0,0,0,1,0},
     {0,1,1,1,1,1,1,0},
     {0,1,0,0,0,0,1,0},
     {0,1,0,0,0,0,1,0},
     {0,1,0,0,0,0,1,0}
    };
    1 symbolizes the letter A and shall be printed with a color
    0 symbolizes the background and shall be printed with a background colour

    As far as i understand it, I need 2 for loops for this
    one that handles the one's and one that handles the zero's
    and if it is 0 it shall be printed with a backgroundcolor and if it is 1 then it shall be printed with a colour

    Now to my question. How do i write this code?
    Do I have to write it in a different way because it is related to an array?
    It seems to me as well that i need if statements?

    Any help would be much appreciated=)

  2. #2
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    You can use two nested loops, where the outer one loops through the rows and the inner one the columns, where the counter variables represent the current row and column. Something like:
    Code:
    int i, j;
    
    for (i = 0; i < 8; ++i) {
        for (j = 0; j < 8; ++j) {
            switch (a[i][j]) {
            case 0:
                /* print color 1 */
                break;
            case 1:
                /* print color 2 */
                break;
            default:
                /* faulty data in the array */
                break;
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Loops
    By sunandstars in forum C Programming
    Replies: 1
    Last Post: 02-17-2011, 04:34 PM
  2. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 03:33 PM
  3. Help with Arrays and For loops
    By Phil- in forum C++ Programming
    Replies: 5
    Last Post: 09-07-2009, 10:34 AM
  4. while loops with arrays
    By volk in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 07:22 PM
  5. loops and arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-03-2001, 03:12 PM