Newbie

This is a discussion on Newbie within the C Programming forums, part of the General Programming Boards category; hey guys I have couple of questions about this program. Code: #include <stdio.h> #define ROW 3 #define COLUMN 10 typedef ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie

    hey guys I have couple of questions about this program.

    Code:
    #include <stdio.h>
    #define ROW 3
    #define COLUMN 10
    typedef
    enum { red, blue, green} color;
    
    void printColor(char colorArray[][COLUMN]);
    void favoriteColor(char colorArray[][COLUMN], color hidden);
    
    int main(void)
    {
    char colorArray[ROW][COLUMN] = { "blue", "green", "red" };
    color hidden = red;
    printColor(colorArray);
    printf("%s", "My favorite color is ");
    favoriteColor(colorArray, hidden);
    return 0;
    }
    
    void printColor(char colorArray[] [COLUMN])
    {
    int j=0;
    for(j = 0; j < ROW; j++)
    puts(colorArray[j]);
    }
    
    void favoriteColor(char colorArray[][COLUMN], color hidden)
    {
    switch (hidden)
    {
    
    case red : puts(colorArray[red]);  break;
    case blue : puts(colorArray[blue]);  break;
    case green : puts(colorArray[green]);  break;
    default : puts("no match");
    }
    }
    Q1) what is the color printed by the colorArray[0], colorArray[1] and colorArray[2] ?
    Q2) what is the color printed by the colorArray[red], colorArray[blue] and colorArray[green] ?



    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Can't you just run it and find out?

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    >what is the color printed by the colorArray[0], colorArray[1] and colorArray[2] ?
    colorArray[0] --> blue
    colorArray[1] --> green
    colorArray[2] --> red
    > what is the color printed by the colorArray[red], colorArray[blue] and colorArray[green] ?
    and this prints the same of the above

    if you can see in the code

    Code:
    enum { red, blue, green} color;
    this is called enumeration constants where red is equal to = 0;
    green=1
    red=2;

    if you want red to strat from 1 u can do this
    Code:
    enum { red=1, blue, green} color;
    hope u can understand better now

    s.s.harish

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    yes i understand it better now .

    Just one little conufsion...so will it print like this?

    colorArray[red] = red
    colorArray[blue] = blue
    colorArray[green] = green


    Thanks
    Last edited by jat421; 04-16-2005 at 07:11 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Please anyone...I am preparing or an exam that is this coming thursday...

    For my Q2

    Q2) what is the color printed by the colorArray[red], colorArray[blue] and colorArray[green] ?

    Will it be like this:


    colorArray[red] = red
    colorArray[blue] = blue
    colorArray[green] = green



    thanks

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    No. Look at the order in which you're initializing things:
    Code:
    enum { red, blue, green} color;
    char colorArray[ROW][COLUMN] = { "blue", "green", "red" };
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    it will print like this

    colorArray[red] ==> blue this is equal to colorArray[0] as red refers to 0
    [Blue]==> green this is equal coloreArray[1]
    [green] ==> red this is eual to colorArray[2]

    s.s.harish

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    so basically all you have to do is change your code to this:
    Code:
    enum { red, blue, green} color;
    char colorArray[ROW][COLUMN] = { "red", "blue", "green" };
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21