Thread: Christmas spirit

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Christmas spirit

    Hello folks:

    I'm supposed to write a wack code that prints out the song: "Twelve days of Christmas"

    using loops and two switch statements. I have a substantial code ( I think), but the output is
    unorganized. With verses scattered everywhere.

    This is what I have:

    /* This program uses repetition and Switch statements
    to print the song "The twelve days of Christmas."
    One switch statement is used to print the day
    ( i.e "First," "Second," , etc). A separate switch statement is
    used to print the remainder of the verse*/
    Code:
     #include <stdio.h>
     
     int main(void)
    {
     int i, j;
     printf("   The Twelve Days of Christmas   "); 
     for( i = 1; i <= 12; i++){
          printf("On the ");   
          switch(i){
          case 1: 
              printf("first");
              break;
          case 2: 
              printf("second");
              break;
          case 3:
              printf("third");
              break;
          case 4: 
              printf("fourth");
              break;     
          case 5: 
              printf("fifth");
              break;     
          case 6: 
              printf("sixth");
              break;     
          case 7: 
              printf("seventh");
              break;     
          case 8: 
              printf("eigth");
              break;     
          case 9: 
              printf("ninth");
              break;     
          case 10: 
              printf("tenth");
              break;     
          case 11: 
              printf("eleventh");
              break;     
          case 12: 
              printf("twelfth");
              break;
          }
          printf(" day of Christmas\n"
                 "my true love sent to me:\n");
          for(j = 1; j <= 12; j++){
                switch(j){
                case 1:
                printf("A Partridge in a Pear Tree\n\n");
                break;
                case 2:
                printf("Two Turtle Doves\n"
                       " and a Partridge in a Pear Tree\n\n");
                case 3:
                printf("Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                case 4:
                printf("Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                case 5:
                printf("Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");      
                break;
                case 6:
                printf("Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                case 7:
                printf("Seven Swans a Swimming\n"
                       "Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");       
                break;
                case 8:
                printf("Eight Maids a Milking\n"
                       "Seven Swans a Swimming\n"
                       "Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                case 9:
                printf("Nine Ladies Dancing\n"
                       "Eight Maids a Milking\n"
                       "Seven Swans a Swimming\n"
                       "Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                case 10:
                printf("Ten Lords a Leaping\n"
                       "Nine Ladies Dancing\n"
                       "Eight Maids a Milking\n"
                       "Seven Swans a Swimming\n"
                       "Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                case 11:
                printf("Eleven Pipers Piping\n"
                       "Ten Lords a Leaping\n"
                       "Nine Ladies Dancing\n"
                       "Eight Maids a Milking\n"
                       "Seven Swans a Swimming\n"
                       "Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                case 12:
                printf("12 Drummers Drumming\n"
                       "Eleven Pipers Piping\n"
                       "Ten Lords a Leaping\n"
                       "Nine Ladies Dancing\n"
                       "Eight Maids a Milking\n"
                       "Seven Swans a Swimming\n"
                       "Six Geese a Laying\n"
                       "Five Golden Rings\n"
                       "Four Calling Birds\n"
                       "Three French Hens\n"
                       "Two Turtle Doves\n"
                       "and a Partridge in a Pear Tree\n\n");
                break;
                }
          }
     }       
     system("pause");
     return 0;
    }
    Last edited by Salem; 09-19-2006 at 11:34 PM. Reason: One-time courtesy code tagging - learn to use them yourself

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Nest your for loops. The first loop would complete before the second one would even run otherwise.

    Also, please use code tags and indent code before someone looses their mind.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your second switch/case is supposed to use "fallthrough" to make the code much much shorter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just to elaborate on Salem's advice for the OP.
    Swap the order of your switch statement around. Make it in DESCENDING order from 12 to 1, inside the switch statement. Then, when the switch statement hits the right day, all the lower days can be printed out properly if you remove the "break" statements, and let the program "fall through" to the next lower switch code.

    Code:
    switch(j)  {
       ... <all other cases larger than 3 up above this>
       case 3:
           printf("\n "Three French Hens ")
       case 2:
           printf("\n "Two Turtle Doves ")
       case 1:
           printf("\n And a Partridge In a Pear Tree ")
       break
       }
    So when it's day 3 or 2, all the lower days presents will also be printed out.

    Neat, eh?

    Adak

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I was kinda hoping they'd look up the answer, or at least try to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Salem
    I was kinda hoping they'd look up the answer, or at least try to.
    blasphemy

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Another hopeless fool vying to contest the invenerable, championed phillips...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem
    I was kinda hoping they'd look up the answer, or at least try to.

    It was after 11:00 pm., trust me, this guy *needed* his beauty sleep!

    Thing is, most of the books I've seen show the "fall through" of the switch statement, but don't show how it can best be used by the *descending* values "trick".

    And what could he google for? "Descending values trick in a switch statement" ?

    Your posted idea was too good to let maybe slip away, unnoticed by the OP.

    Didn't mean to intrude. I know the repetitive homework type questions can get tiresome.

    jafet: That phillips link is a holy terror! (and a laugh riot). Can you imagine trying to debug some &^%$#@!@#$% code like that??

    Adak
    Last edited by Adak; 09-20-2006 at 04:10 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    Thumbs up Re: Christmas Spirit

    Thank you all for your input. I will try the descending order trick and see if I can minimize my code!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Christmas movies/traditions..
    By kermit in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 12-25-2008, 12:08 PM
  2. Merry Christmas!
    By sean in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 12-25-2004, 11:40 PM
  3. Merry Christmas Everyone!!
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-27-2001, 03:34 PM
  4. Christmas Songs
    By Betazep in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 12-21-2001, 07:44 PM
  5. Merry Christmas!!
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-12-2001, 10:49 PM