Thread: Need help with problem set

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    Need help with problem set

    Ok, so my beginning to C programming class has started and I have understood the concepts thus far. There is this one programming excercise I am having an issue with. The excercise asks you to create a program that results in the following output:

    Smile!Smile!Smile
    Smile!Smile!
    Smile!

    The only requirement is that I use a function that prints smile once, and then calling upon that function as many times as required to produce the above output. The following is what I have s o far

    #include <stdio.h>
    int main (void)
    {
    smile(), smile(), smile();
    smile(), smile();
    smile();
    return 0;
    }
    void smile (void)
    {
    printf("Smile!");
    }

    This is producing an output of

    Smile!Smile!Smile!Smile!Smile!Smile!

    I know I need a line break ("\n") some where in there, but where. I have tried putting it everywhere and cannot seem to get the require output.

    BTW im using Ubuntu Intrepid Ibex server with GCC . My prog is compiling without error, I know I am missing something.

    Your help is greatly appreciated!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should consider using a loop, and a counter, which decreases and keeps track of how many time to call your function before it writes a newline.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    between the calls to smile();

    Also,
    Code:
    smile(), smile(), smile();
    is more conventionally written
    Code:
    smile(); smile(); smile();

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Code:
    #include <stdio.h>
    
    void smile(void);   // Function prototype ; required if you put your functions 
                                 //after main
    
    int main (void)
    {
       int count1;
       int count2;
       
       for(count1 = 3; count1 > 0; --count1)
      {
          for(count2 = count1; count2 > 0; --count2) 
          {   
           smile();
           }
       printf("\n");
       }
    return 0;
    }
    void smile (void)
    {
    printf("Smile!");
    }
    Last edited by strictlyC; 04-17-2009 at 10:24 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What about if you had a for loop in main(). It could call smile() every time through the loop, and you could set it up to count backward: 3,2,1


    In main()

    Code:
    for( i = 3; i > 0; i--)
       smile(i);
    In smile:

    Code:
    void smile(int j)  {
    
    for( ; j > 0; j--)
       printf("smile");
    
    print your newline, here.
    
    }
    So the effect is the same as a double for loop to do this, in main(), but it shows how you want to break apart problems, into smaller sub-problems, and split them up among different smaller functions.

    Do you see how the variable i, is turned into j inside smile() ? That's why there's no assignment in smile's for loop, for j. It's already got the right value.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Please don't do people's homework for them. They learn more figuring it out themselves.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    God damn you people irritate me. There's a reason I didn't just do the loop for him.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Code:
    #include <stdio.h>
    
    void smile(int i);
    
    int main (void)
    {
       smile(6);
    }
    
    
    void smile (int i)
    { 
       if(i == 1 || i == 3)
          printf("\n");
       
       if(i){
          printf("Smile!");
          smile(--i);
       }
    }

    Just for fun... and apparrently to irritate quzah
    Edited a little now I'm happy.
    Last edited by strictlyC; 04-17-2009 at 10:15 PM.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Code:
    #include <stdio.h>
    
    void smile (void)
    { 
       printf("Smile!");
    }
    
    int main (void)
    {
       for(int i = 6; i != 0; --i)
       {
          if(i == 1 || i == 3)
             printf("\n");
          smile();
       }
    }
    Much better than the first one I posted...

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    I appreciate all of the ideas, but I think you are all complicating the problem. We were instructed not to use anything that the class hasnt covered yet which would include loops and counters. There has to be a simpler way of doing this. Thanks for all of your responses, I will take it from here. I dont want to upset anyone with my homework.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by On51aught
    We were instructed not to use anything that the class hasnt covered yet which would include loops and counters. There has to be a simpler way of doing this.
    In that case you should break up your function calls. First, call smile() three times, then call printf() (or puts()) to print a newline. Then call smile() twice, etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    Thats exactly what I just finished doing. I guess I was also making this problem harder than it really was. I just used a printf function to print a new line. The following is what I got

    Code:
    #include <stdio.h>
    void smile (void);
    int main (void)
    {
    smile();smile();smile();printf("\n");
    smile();smile();printf("\n");
    smile();printf("\n");
    return 0;
    }
    void smile (void)
    {
    printf("Smile!");
    }
    Again thanks for the help, I am not looking for people to do my homework for me by any stretch, just some boots in the rear towards the right direction. Thanks again to all that responded. I might play with the loop and counter idea just to see if I can get it to work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. class, set, < problem
    By hk_mp5kpdw in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2003, 09:25 AM