your "horrorscope"......

This is a discussion on your "horrorscope"...... within the C Programming forums, part of the General Programming Boards category; Sorry, but I dont get it.... should I use the switch again...? but i do understand that i have to ...

  1. #16
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Ooopps...

    Sorry, but I dont get it....

    should I use the switch again...?

    but i do understand that i have to declare:
    Code:
    char *zodiac[N] = { "CAPRICORN", "AQUARIUS", "PISCES", "ARIES", "TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO", "LIBRA", "SCORPIO", "SAGITTARIUS"};
    
    int *date[4] ={18, 19, 20, 22};

    I couldn't discern it... pls show me the way...

  2. #17
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    sorry,... 21 , lastdate

    sorry, got a correction.. int 21 and limit

    Code:
    char *zodiac[N] = { "CAPRICORN", "AQUARIUS", "PISCES", "ARIES", "TAURUS", "GEMINI", "CANCER", "LEO", "VIRGO", "LIBRA", "SCORPIO", "SAGITTARIUS"};
    
    int *limit[4] ={18, 19, 20, 21, 22};

    i couldn't get the idea of using these info's for the switch...
    please share some hints and ideas...

  3. #18
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Here's an example of using arrays and structures to do the job, using an easy-to-use indexing scheme.

    Enjoy.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    struct date {
     int month;
     int day;
    };
    
    
    
    
    void init_date(date * d, int month, int day)
    {
     d->month = month, d->day = day;
    }
    
    
    
    
    enum signs { frog = 0, rat = 1, horse = 2 };
    
    
    
    
    struct zodiak {
     signs sign;
     date begin;
     date end;
    };
    
    
    
    
    void init_zodiak(zodiak * z, signs sign, int begin_month, int begin_day, int end_month, int end_day)
    {
     z->sign = sign;
     init_date(&z->begin, begin_month, begin_day);
     init_date(&z->end, end_month, end_day);
    }
    
    
    
    
    int within(zodiak z, int month, int day)
    {
     if(  (month == z.begin.month && day >= z.begin.day)
        ||(month == z.end.month && day <= z.end.day) )
       return 1;
    
     return 0;
    }
    
    
    
    
    zodiak * lookup(int month, int day, zodiak array[], int array_length)
    {
     int i;
    
     for(i = 0; i < array_length; ++i)
      if(within(array[i], month, day))
       return &array[i];
    
     return 0;
    }
    
    
    
    
    #define MAX 3
    
    
    
    
    int main(int argc, char *argv[])
    {
     zodiak zdk[MAX];
    
     init_zodiak(&zdk[frog], frog, 1, 15, 2, 14);
     init_zodiak(&zdk[rat], rat, 2, 15, 3, 14);
     init_zodiak(&zdk[horse], horse, 3, 15, 4, 14);
    
     char * names[MAX];
     char * horoscopes[MAX];
    
     names[frog] = "Frog";
     names[rat] = "Rat";
     names[horse] = "Horse";
    
     horoscopes[frog] = "Frogs tend to be slimeballs.";
     horoscopes[rat] = "Rats can't be trusted.";
     horoscopes[horse] = "Horses are great in bed.";
    
     int month, day;
    
     printf("Enter Month: ");
    
     scanf("%d", &month);
    
     printf("Enter Day: ");
    
     scanf("%d", &day);
    
     zodiak * found = lookup(month, day, zdk, MAX);
    
      if(found)
     {
      int index = found->sign;
    
      printf("%s\n", names[index]);
    
      printf("%s\n", horoscopes[index]);
     }
      else printf("Unsupported or invalid date.");
    
     system("pause");
    
     return 0;
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #19
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Got it simplified...!!

    Hey Dudes !!

    Oi ! /*Salem*/ !!

    I don't know what you call it... but I woke up this evening, and got the idea for the problem... i got it on paper then typed it on my P.C. - and Yeah it worked !!

    But I got startled when I checked this board and found you
    got the same idea too... Cool !!

    "From a long bunch of flies to a pile of maggots" - that's what happened to this "horrorscope" --> real horror !!

    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"},
    	      *limit[N] = { 19, 18, 20, 19, 20, 20, 22, 22, 22, 22, 21, 21 };
      int i, d;
      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(zodiac[i]);
      else
      printf(zodiac[i+1]);
      getch();
    }
    Oi !! /*Sebastiani*/ !!

    You got some fine idea dude ! I gotta copy it and research more on it !! Thanks !!

    =========
    Thanks Dudes !! I learned a lot from this work !! More than 10 hours of lecture on class....

    If you got some comments and anything to jerk out, please yell it out !!!

    --------------------------------------------------------------

    I just want to say LOVE YOU SAN!!
    billy gates why do you make this possible ? Stop making money and fix your software!!

  5. #20
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    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 ?

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    The modulus (%) obtains the remainder of a division. For instance:

    10 % 3 = 1

    Thus, that code insures that an in-bounds index is generated.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #22
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    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 !!!

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Salem
    You're still not listening
    limit is an array of int not an array of char *
    So... based on Salem's comment you (
    imbecile in C) made it :
    >>int *limit[N] = ....
    which is still wrong, this is an array of int pointers, not an array of ints. You need:
    >>int limit[N] = ...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #24
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: Correction.... advise please....

    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 ]);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #25
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    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 !!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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