Re: Hey Dudes !! C'mon !!
Quote:
Originally posted by imbecile in C
Code:
char *continent[] ={"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
Ok.. How do you declare the continent right ??
The [] was missing. Without it, the original attempt:
char *continent = {"one two three"};
.. creates a single string consisting of many words, it does not create a 2d array.
With the [] and comma delimited list, you are creating an array of char pointers, and assigning them default values, pointing to the string literals.
Here's a simple program to show you:
Code:
#include <stdio.h>
/* There are better ways to express the array size,
but I won't cover them here, I'll keep things simple */
#define NUM_CONTINENTS 6
int main(void)
{
char *continents[NUM_CONTINENTS] = {"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
int i;
for (i = 0; i < NUM_CONTINENTS; i++)
puts (continents[i]);
return(0);
}
/* Output
Australia
Asia
Africa
Europe
NAmerica
SAmerica
*/
And now a more useful sample for you to study:
Code:
#include <stdio.h>
#include <string.h>
/* There are better ways to express the array size,
but I won't cover them here, I'll keep things simple */
#define NUM_CONTINENTS 6
int main(void)
{
char *continents[NUM_CONTINENTS] = {"Australia", "Asia", "Africa", "Europe", "NAmerica", "SAmerica"};
int i;
char buf[BUFSIZ]; /* BUFSIZ is a number, at least 256 in value */
printf ("Enter a continent: ");
fflush(stdout);
if (fgets(buf, sizeof(buf), stdin))
{
strtok (buf, "\n\r"); /* Remove the newline character from the input */
for (i = 0; i < NUM_CONTINENTS; i++)
{
if (strcmp (continents[i], buf) == 0)
{
printf ("Found a match for %s\n", continents[i]);
break;
}
}
if (i == NUM_CONTINENTS)
{
puts ("No match found");
}
}
return(0);
}
Correction.... advise please....
Here's the revised code again:
Code:
#include <stdio.h>
#define N 12
int main(void)
{
char m[80], *months[N] = {"january", "february", "march", "april", "may",
"june", "july","august", "september", "october",
"november", "december"},
*zodiac[N] = {"CAPRICORN", "AQUARIUS", "PISCES", "ARIES",
"TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO",
"LIBRA", "SCORPIO", "SAGITTARIUS"};
int i, d, *limit[N] = { 19, 18, 20, 19, 20, 20, 22, 22, 22, 22, 21, 21 };
clrscr();
printf("enter MONTH of birth: "); gets(m);
printf("enter DATE of birth: "); scanf("%i", &d);
for(i=0; i<N && strcmp(m, months[i]) != 0; i++);
if(d<=limit[i])
printf("%s\n", zodiac[i]);
else
printf("%s\n", zodiac[(i+1)%N]);
getch();
}
I wish to understand....
if(d<=limit[i])
printf("%s\n", zodiac[i]);
else
printf("%s\n", zodiac[(i+1)%N]
i tried it on december 25.. and u are correct !! some garbage..
and then it occurred to me.. ohhh yeah....!!
but why %N ? what's that for ?? I know it corrects the code... but what's its purpose ?
In-bounds index ....... modulus.....
/*sebastiani*/
The modulus (%) obtains the remainder of a division....
Thus, that code insures that an in-bounds index is generated.
-------------
oh yes,.. of course, of course....
modulus operator (mod) of course.. makes the index iterate within the array,..
I couldn't really have thought of that thing....
How did your brain's neurotransmitters connect to that concept ?
Your brains are all wired differently !! *envy*
Great !! I really learned a lot !!
Merci !!!
Re: Correction.... advise please....
Quote:
Originally posted by imbecile in C
Code:
...
for(i=0; i<N && strcmp(m, months[ i ]) != 0; i++);
if(d<=limit[ i ])
printf("%s\n", zodiac[ i ]); /*indent for readability,
make the code tags useful */
else
printf("%s\n", zodiac[(i+1)%N]); /*indent for readability */
getch();
}
OK, let's look at this logically:
After for(i=0; i<N && strcmp(m, months[ i ]) != 0; i++); what does the value i represent? The month. And the zodiac IF day < limit[ i ]. Seem reasonable?
So, instead of if(d<=limit[ i ]) how about
if(d>=limit[ i ]) i++; /* go to the next month */
BUT, i might be > N at this point, so the year wrapped:
if(i >= N) i -= N; /* go to the first month */
Now you can simply:
printf("%s\n", zodiac[ i ]);
always another way,.... !!
Im copying your comments dudes.... Im learning more on those "indentations for readability" ...(imbecile me)...
now i have to *really* listen..
int *limit[N] ---> wrong
int limit[N] -----> right....!!
i should understand the purpose of an array of "int pointers", and an array of "ints only"...
Im analyzing the points given by Walt P... and yeah ! they're awesome ideas..
Code:
#include <stdio.h>
#define N 12
int main(void)
{
char m[80], *months[N] = {"january", "february", "march", "april", "may",
"june", "july","august", "september", "october",
"november", "december"},
*zodiac[N] = {"CAPRICORN", "AQUARIUS", "PISCES", "ARIES",
"TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO",
"LIBRA", "SCORPIO", "SAGITTARIUS"};
int i, d, limit[N] = { 19, 18, 20, 19, 20, 20, 22, 22, 22, 22, 21, 21 };
clrscr();
printf("enter MONTH of birth: "); gets(m);
printf("enter DATE of birth: "); scanf("%i", &d);
for(i=0; i<N && strcmp(m, months[i]) != 0; i++);
if(d<=limit[i])
printf("%s\n", zodiac[i]);
else
printf("%s\n", zodiac[(i+1)%N]);
getch();
}
--------
or as recommended by Walt P
Code:
#include <stdio.h>
#define N 12
int main(void)
{
char m[80], *months[N] = {"january", "february", "march", "april", "may",
"june", "july","august", "september", "october",
"november", "december"},
*zodiac[N] = {"CAPRICORN", "AQUARIUS", "PISCES", "ARIES",
"TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO",
"LIBRA", "SCORPIO", "SAGITTARIUS"};
int i, d, limit[N] = { 19, 18, 20, 19, 20, 20, 22, 22, 22, 22, 21, 21 };
clrscr();
printf("enter MONTH of birth: "); gets(m);
printf("enter DATE of birth: "); scanf("%i", &d);
for(i=0; i<N && strcmp(m, months[i]) != 0; i++);
if(d>=limit[i]) i++;
if(i>=N) i-=N;
printf("%s\n", zodiac[(i)]);
getch();
}
Great !! WHew !!
===========
that which one learns with difficulty would not be easily forgotten...
especially by an imbecile one..
merci !!