Thread: My very first program...ever

  1. #1
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72

    My very first program...ever

    I took the advice of some of you and googled college level homework assignments for c programming and was able to locate a few sources. Here is my very first program, written on my own, using my own logic and without any help.

    Before you tear me up too much, please realize that I am sure this could be written much smaller and more efficiently. Your input is what I can use to learn not only to work with the logic of C, but the beginning of thinking on how to write more efficient code and how to write code in the most commonly accepted format.

    If you want to lead me to a more compact design for this program, please try to hint your way around it so I can try and figure out what you mean. It will help me to gain a deeper understanding of the logic than if you come right out and tell me how to do something. Plus, if I get stumped...I will ask outright. LOL.

    Thanks and double thanks for bearing with a newbie whom is trying to teach himself how to program.



    Code:
    /* develop a program that accepts a dollar amount (in cents) and then computes
    the number of quarters, dimes, nickels, and pennies needed.  The solution must
    produce the smallest number of coins possible for the given dollar amount*/
    
    
    #include <stdio.h>
    
    main()
    {
    
        int c,q = 0, d = 0, n = 0, p = 0;     /* I had to initialize my actual coin 
                                              counts to zero, else I got some pretty
                                              amazing numbers*/
        
        printf( "Please enter an amount ( in cents ):" );
        scanf( "&#37;d", &c );
        getchar();
        
        while ( c >= 25 ){
              q++;
              c = c - 25;                               /*Could I have used c -= 25 for this and following statements*/
              }
        printf( "* You will need %d quarters.\n", q);
                  
        while ( c >= 10 ){
              d++;
              c = c - 10;
              }
        printf( "* You will need %d dimes.\n", d);
        
        while ( c >= 5 ){
              n++;
              c = c - 5;
              }
        printf( "* You will need %d nickels.\n", n);
        
        while ( c >= 1 ){
              p++;
              c = c - 1;
              }
        printf( "* You will need %d pennies.\n", p);
        
        getchar();
    }

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    That is certainly one way to do it. Another option is to use division and the modulus operator.

    q = c / 25;
    c = c % 25;

    The first equation will give you the number of times the coin amount goes into c (this will be rounded down for you if you are using ints, so 3.14 would be 3). Example:

    147 / 25 = 5.88 which will round down to 5

    The second equation uses the modulus operator which will return the remainder of a division. So:

    147 % 25 = 22

    This accomplishes the same thing as your while loops. If you want to still use the while loops you could move the logic into a function. Notice that you are doing the same thing for each coin with only the coin value changing. That is prime behavior that could be moved into a function. You might try this when you feel more comfortable with the basics.

    Looks good for your first program. Have fun!

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Could I have used c -= 25 for this and following statements

    Yes.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I agree. Good start. I think you're on your way.

    The rest of this post is me being pedantic:

    main() should be defined this way (with only one potential minor variation usually), unless you're in a freestanding environment (ie. not your usual computer programming):

    Code:
    int main(void)
    {
        ...
        return 0;
    }
    That should make your code entirely compliant to either C standard.

  5. #5
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72

    Thnak you

    I thought there was a way to put all of my loops into a single function to better serve what I was trying to accomplish, but I'm just beginning to read about functions and how they work, so that could take some time for me to work out.

    What exactly is difference between main(), void main(), and int main()?

    Also, what effect does return 0 have on a program and how does its absence affect the same?

    I didn't even think of using modulus. Thanks for that one.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Dogmasur View Post
    What exactly is difference between main(), void main(), and int main()?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Quote Originally Posted by Dogmasur View Post
    Also, what effect does return 0 have on a program and how does its absence affect the same?
    I've written a number of posts on this. The basic idea is that the calling application (possibly cmd.exe, for example, on Windows) is ready and waiting to accept an integer from your application. If you don't return one from main(), the calling application will look for one anyway, and will receive an arbitrary value from your program.

    This can be difficult during debugging sessions, since the return code from main() by general convention is 0 for success (absence of errors) and non-0 for failure, usually with an indication by number of what went wrong (using some possibly internal convention for the error codes). Thus if you don't return a value, someone debugging your application wouldn't have a quick way of telling if it succeeded or not. This is not really desirable.

    For C99, however, if you do not return a value from main(), 0 is automatically returned. This is special to C99 and only for main(). If you declare main() as returning void, then this may not work for you.

  7. #7
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Okay thanks.

  8. #8
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Here is an example of how you could do this a little differently and reuse your while approach. This introduces functions and pointers (although you have already used a pointer with scanf).
    Code:
    #include <stdio.h>
    
    int payChange(int* change, int coin); /* function prototype */
    
    int main()
    {
    
        int change;
    
        printf( "Please enter an amount ( in cents ):" );
        scanf( "%d", &change);
        getchar();
    
        printf( "* You will need %d quarters.\n", payChange(&change, 25));
        printf( "* You will need %d dimes.\n", payChange(&change, 10));
        printf( "* You will need %d nickels.\n", payChange(&change, 5));
        printf( "* You will need %d pennies.\n", payChange(&change, 1));
        
        getchar();
    
        return(0);
    }
    
    /* takes in a pointer to the change to be paid and the coin to use to pay it */
    int payChange(int* change, int coin)
    {
    	int amount = 0;
    	while (*change >= coin)
    	{
    		amount++;
    		*change = *change - coin;
    	}
    	return(amount);
    }
    Look into pointers and see if this makes sense. Maybe you have already looked into this if you wondered why scanf needed an & in front of the variable.

  9. #9
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72

    I'll play with it a bit

    Thanks, I'll toy around with it and see how it goes. Another question. If I wanted my output to only print lines that had a coin count higher than zero, would I be best to try to make a function or to use conditional statements? Either way, please don't show me the code...just kind of *point me in the right direction..thanks.

    It's good to know there are those that are interested in helping people out there. Thats a rarity found in life these days.

  10. #10
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Dogmasur View Post
    Thanks, I'll toy around with it and see how it goes. Another question. If I wanted my output to only print lines that had a coin count higher than zero, would I be best to try to make a function or to use conditional statements? Either way, please don't show me the code...just kind of *point me in the right direction..thanks.

    It's good to know there are those that are interested in helping people out there. Thats a rarity found in life these days.
    An if statment would work well. Try to make your program as simple as possible. Ask yourself, what if they suddenly discontinued dimes? Would I need to edit 10 different lines of my program? Or could I just change one line slightly? What if they added a 50 cent piece?

    One thing you might try is modifying your coin program to require the smallest modification to support a new type of coin.

  11. #11
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72

    Hey Noops

    I rewrote my code to match yours above and it worked fine. I asked about using a conditional expression to modify my program so that it will not print lines that end up having a coin count of zero. You made some suggestions and I have been toying around for a day and have to admit I am completely stumped. I don't see it. Can you show it to me and explain the process.

    Thanks for your time.

  12. #12
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Dogmasur View Post
    I rewrote my code to match yours above and it worked fine. I asked about using a conditional expression to modify my program so that it will not print lines that end up having a coin count of zero. You made some suggestions and I have been toying around for a day and have to admit I am completely stumped. I don't see it. Can you show it to me and explain the process.

    Thanks for your time.
    You'd probably need to use some variables in this case.
    Code:
    int quarters;
    quarters = payChange(&change, 25);
    if (quarters > 0)
         printf(" * You will need %d quarters.\n", quarters);
    Did this answer your question?

  13. #13
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Actually I thought of something similar to this. Would I then need to use an if for each coin iteration? Your last reply made it seem like I should try to use as little code as possible, so I was thinking of a way of trying to use one code block ( or maybe a function particular to this operation ) to handle all of my printf iterations. Am I not understanding your most recent reply or am I reading too much into it?

    Thanks

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The shortest way of doing it would be to store everything different in sets of arrays and iterate through in a loop.

    Example.... this could be your print line inside the loop:

    Code:
    printf( "* You will need &#37;d %s.\n", payChange(&change, values[i]), denominations[i]);
    Create values as an array of integers, initialized to 25, 10, 5, and 1. A similar array named denominations would be declared as an array of char *'s and would contain "quarters", "dimes", "nickels", "pennies". The loop should obviously iterate for as many elements are in the array.

    If you wanted to really clean this up, instead of synchronized arrays, you could use an array of structs initialized to the appropriate values, but it's the same exact idea.

    This entire thing I'm suggesting may be a little complex to understand at first, and I sort of rushed it, so don't worry about it if this doesn't make perfect sense.

  15. #15
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    That makes some sense. I read a little bit about structures, and have to admit that I think those are beyond me yet. I'll play around with the array idea for a bit and see if I can figure this out.

    Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM