-
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
-
Can't you just run it and find out?
-
>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
-
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
-
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
-
No. Look at the order in which you're initializing things:
Code:
enum { red, blue, green} color;
char colorArray[ROW][COLUMN] = { "blue", "green", "red" };
-
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
-
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" };