Thread: Simple C Programs That I Require Help With

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    7

    Simple C Programs That I Require Help With

    I'm very very new at C programming/writing and I'm so confused, I have no idea what I'm doing. I was given this problem to do and I'm so confused on what to put on the Printf() statement... I don't know what kind of set up it should have either. What should be on the printf(.....) statement to calculate 123 seconds into minutes and seconds (2 minutes and 3 seconds.)
    Please help:

    Code:
    /* Convert seconds to minutes and seconds. */
    Code:
    #include<stdio.h>
    
    int main(void)
    {
    int input_value, minutes, seconds;
    
    printf("Input the number of seconds:");
    scanf("%d", &input_value);
    minutes=input_value / 60;
    seconds=input_value % 60;
    printf(.....);
    return 0;
    } 



    2)I have two other I don't quite understand, if someone can please give me some help, and explain just the basics of the codes, but if not the answers are fine:
    Write an interactive program that asks the user to input the length and width of a rectangular lawn. The dimensions should be in yards. Your program should compute the area of the lawn in square yard, square feet, and square inches. Print all the information neatly on the scree. Use the following declaration:

    Code:
    int cv_factor = 36 * 36; /* conversion:sq in per yrd */
    Code:
    or 
    int cv_factor= 1296; /* conversion: sq in per yrd */


    3)Here is a part of an interactive program that computes the value of some coins. The user is asked to input the number of half dollars, quarters, dimes, and so on.

    Code:
    #include<stdio.h>
    Code:
    int main(void)
    {
    int h, /* number of half dollars */
    q, /* number of quarters */
    d, /*number of dimes */
    n, /*number of nickels */
    p; /*number of pennies*/
    .....
    
    printf("Value of your change will be computed. \n\n");
    printf("How many half dollars do you have?");
    scanf("%d", &h);
    printf("How many quarters do you have?");
    scanf("%d" , &q);


    Complete the program, causing it to print out relevant information. For example, you may want to create out that looks like this:

    You entered: 0 half dollars
    3 quarters
    2 dimes
    17 nickels
    1 pennies
    The values of your 23 coins is equivalent to 181 pennies



  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    to printf an int, use %d
    printf("minutes: %d seconds:%d\n",minutes,seconds);

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Were the answers you received on another forum satisfactory?

    FYI it's considered rude to cross-post questions like you did - some people here might take their own time to help you, only to be wasting their time because someone else on a different forum already answered your questions.

    Here's some reading for you: How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Quote Originally Posted by Matticus View Post
    Were the answers you received on another forum satisfactory?

    FYI it's considered rude to cross-post questions like you did - some people here might take their own time to help you, only to be wasting their time because someone else on a different forum already answered your questions.

    Here's some reading for you: How To Ask Questions The Smart Way
    I'm sorry. I was rushing a tad bit, so I kind of was acting with no rationale. They were both really helpful, and I'm truly sorry if I angered someone. I was just in a rush so I thought it would be a little more efficient to post in both. Again I'm sorry for my ignorance.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    How do I do this part? if anyone is nice enough to explain it to me.
    The values of your 23 coins is equivalent to 181 pennies

    Code:
    int main(void){
    int h, /* number of half dollars */
    q, /* number of quarters */
    d, /*number of dimes */
    n, /*number of nickels */
    p; /*number of pennies*/
    
    
    
    
    printf("Value of your change will be computed. \n\n");
    printf("How many half dollars do you have?", h);
    scanf("%d", &h);
    printf("How many quarters do you have?", h/2);
    scanf("%d" , &q);
    printf("How many dimes do you have?", h/5);
    scanf("%d" , &d);
    printf("How many nickels do you have?", h/10);
    scanf("%d" , &n);
    printf("How many pennies do you have?", h/5);
    scanf("%d" , &p);
    
    
    printf("you entered= %d half dollars", h);
    printf("you entered= %d quarters", q);
    printf("you entered= %d dimes", d);
    printf("you entered= %d nickels", n);
    printf("you entered= %d pennies", p);
    
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have to show your attempt - these forums have a homework policy, and nobody is going to do the work for you.

    It appears that you want to receive a certain amount of coins, and determine how many cents they are collectively worth.

    Imagine I handed you three quarters, a dime, and two pennies. How would you go about figuring out how many cents they were worth?

    At this point, you shouldn't be writing code. Instead, grab a pen and paper. Write out, step by step, how you would calculate this value in your head. These steps can be used as a guide when you're ready to start writing your code.

    Write a little bit of code, compile, and test. (For instance, your code so far is the first step - reading inputs and verify the values are correct.) When that bit works, add a little more code, compile, and test.

    When you get stuck with a more specific problem, come back and post your latest code, along with compiler warnings/errors and specific questions. We can then go from there.

    I'm sorry. I was rushing a tad bit, so I kind of was acting with no rationale. They were both really helpful, and I'm truly sorry if I angered someone. I was just in a rush so I thought it would be a little more efficient to post in both. Again I'm sorry for my ignorance.
    I don't think you "angered" anyone. And there's nothing wrong with ignorance, as long as you learn from it. That link I posted seems rude to some, but it's more about directness - and it contains some very good information.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Code:
    int main(void)
    Code:
    {int h, /* number of half dollars *
    /q, /* number of quarters */
    d, /*number of dimes */n, /*number of nickels *
    /p; /*number of pennies*/
    
    printf("Value of your change will be computed. \n\n");
    printf("How many half dollars do you have?", h);
    scanf("%d", &h);
    printf("How many quarters do you have?", h/2);
    scanf("%d" , &q);printf("How many dimes do you have?", h/5);
    scanf("%d" , &d);
    printf("How many nickels do you have?", h/10);
    scanf("%d" , &n);
    printf("How many pennies do you have?", h/5);
    scanf("%d" , &p);
    printf("you entered= %d half dollars", h);
    printf("you entered= %d quarters", q);
    printf("you entered= %d dimes", d);
    printf("you entered= %d nickels", n);
    printf("you entered= %d pennies", p);
    int sum= h+q+d+n+p;
    printf("the value of your %d coins is equivalent to %d pennies");
    
    return 0;
    }
    It doesn't have any errors when I ran it, but it doesn't give me the right amount of coins... When I compile it it says that my amount of coins in -13413092
    Last edited by KatiNescher9; 02-14-2013 at 03:56 PM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    No errors, but I got plenty of warnings when I compiled your code.

    Code:
    main.c||In function 'main':|
    main.c|8|warning: incompatible implicit declaration of built-in function 'printf'|
    main.c|10|warning: too many arguments for format|
    main.c|11|warning: implicit declaration of function 'scanf'|
    main.c|11|warning: incompatible implicit declaration of built-in function 'scanf'|
    main.c|13|warning: too many arguments for format|
    main.c|16|warning: too many arguments for format|
    main.c|19|warning: too many arguments for format|
    main.c|22|warning: too many arguments for format|
    main.c|33|warning: too few arguments for format|
    main.c|31|warning: unused variable 'sum'|
    ||=== Build finished: 0 errors, 11 warnings ===|
    The warnings about "implicit declaration" is because you don't have (or didn't provide us) with #include <stdio.h>. The warnings about "too many arguments for format" is because you have extra arguments in some of your "printf()" statements. In the "How many..." print statements, you're not printing any values, so why do you have the second argument in them?

    Also, make sure you paste your code as "plain text," and make sure the formatting is preserved (indentation is important to seeing the flow of the program).

    --------

    When I compile it it says that my amount of coins in -13413092
    Code:
    int sum= h+q+d+n+p;
    printf("the value of your %d coins is equivalent to %d pennies");
    This time, your "printf()" statement does contain values - in this case, you'd want to add the appropriate arguments at the end. You have two values (format specifiers), so you should be providing two arguments.

    It looks like the first one is the total amount of coins - that should go in there.
    You also aren't calculating the total value of the coins anywhere. You need to calculate this value (preferable into a variable) and add that to the "printf()" argument list, too.

    FYI: "sum" is probably a misleading variable name. Is it referring to the total amount of coins, or the total value of the coins? You're able to use more descriptive variable names - so go for it.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    This is the last one I created, I did what you told me to, and I came out with these equations. No compiler errors but it doesn't give me the right amount of pennies or coins...
    Code:
    int main(void){
    int h, /* number of half dollars */
    q, /* number of quarters */
    d, /*number of dimes */
    n, /*number of nickels */
    p; /*number of pennies*/
    
    
    
    
    printf("Value of your change will be computed. \n\n");
    printf("How many half dollars do you have?", h);
    scanf("%d", &h);
    printf("How many quarters do you have?", h/2);
    scanf("%d" , &q);
    printf("How many dimes do you have?", h/5);
    scanf("%d" , &d);
    printf("How many nickels do you have?", h/10);
    scanf("%d" , &n);
    printf("How many pennies do you have?", h/5);
    scanf("%d" , &p);
    
    
    printf("you entered= %d half dollars", h);
    printf("you entered= %d quarters", q);
    printf("you entered= %d dimes", d);
    printf("you entered= %d nickels", n);
    printf("you entered= %d pennies", p);
    int sum= h+q+d+n+p;
    int add= h*50 + q*25 + d*10 + n*5 + p;
    printf("the value of your %d coins is equivalent to %d pennies");
    
    
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It looks like you posted before seeing my response. However, this quote of mine is the key to your problem:

    This time, your "printf()" statement does contain values - in this case, you'd want to add the appropriate arguments at the end. You have two values (format specifiers), so you should be providing two arguments.
    [edit]
    http://www.cprogramming.com/tutorial...t-strings.html

    Read the section on "format specifiers"
    [/edit]

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Thank You so much Matticus I finally got the right output:
    This was the final code
    Code:
    int main(void){
    int h, /* number of half dollars */
    q, /* number of quarters */
    d, /*number of dimes */
    n, /*number of nickels */
    p; /*number of pennies*/
    
    
    
    
    printf("Value of your change will be computed. \n\n");
    printf("How many half dollars do you have?", h);
    scanf("%d", &h);
    printf("How many quarters do you have?", h/2);
    scanf("%d" , &q);
    printf("How many dimes do you have?", h/5);
    scanf("%d" , &d);
    printf("How many nickels do you have?", h/10);
    scanf("%d" , &n);
    printf("How many pennies do you have?", h/5);
    scanf("%d" , &p);
    
    
    printf("you entered= %d half dollars", h);
    printf("you entered= %d quarters", q);
    printf("you entered= %d dimes", d);
    printf("you entered= %d nickels", n);
    printf("you entered= %d pennies", p);
    int sum= h+q+d+n+p;
    int add= h*50 + q*25 + d*10 + n*5 + p;
    printf("the value of your %d coins is equivalent to %d pennies", sum, add);
    
    
    return 0;
    }


    Again Thank You so much.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This is what the code should look like when posted - just paste as text and the forum software will take care of all the details. I also threw in some indentation as a freebie.

    Code:
    int main(void)
    {
        int h, /* number of half dollars */
        q, /* number of quarters */
        d, /*number of dimes */
        n, /*number of nickels */
        p; /*number of pennies*/
    
        printf("Value of your change will be computed. \n\n");
        printf("How many half dollars do you have?", h);
        scanf("%d", &h);
        printf("How many quarters do you have?", h/2);
        scanf("%d" , &q);
        printf("How many dimes do you have?", h/5);
        scanf("%d" , &d);
        printf("How many nickels do you have?", h/10);
        scanf("%d" , &n);
        printf("How many pennies do you have?", h/5);
        scanf("%d" , &p);
    
        printf("you entered= %d half dollars", h);
        printf("you entered= %d quarters", q);
        printf("you entered= %d dimes", d);
        printf("you entered= %d nickels", n);
        printf("you entered= %d pennies", p);
    
        int sum= h+q+d+n+p;
        int add= h*50 + q*25 + d*10 + n*5 + p;
    
        printf("the value of your %d coins is equivalent to %d pennies", sum, add);
    
        return 0;
    }
    Some notes:

    - I don't see "#include <stdio.h>" in your code
    - Lines 10, 12, 14, 16, and 18: You don't need the second argument in those "printf()" statements
    - Is it difficult to see the results, since you don't have any newlines to neatly display the output?

    Again Thank You so much.
    You're welcome.

  13. #13
    Registered User
    Join Date
    Feb 2013
    Posts
    7
    Quote Originally Posted by Matticus View Post
    This is what the code should look like when posted - just paste as text and the forum software will take care of all the details. I also threw in some indentation as a freebie.

    Code:
    int main(void)
    {
        int h, /* number of half dollars */
        q, /* number of quarters */
        d, /*number of dimes */
        n, /*number of nickels */
        p; /*number of pennies*/
    
        printf("Value of your change will be computed. \n\n");
        printf("How many half dollars do you have?", h);
        scanf("%d", &h);
        printf("How many quarters do you have?", h/2);
        scanf("%d" , &q);
        printf("How many dimes do you have?", h/5);
        scanf("%d" , &d);
        printf("How many nickels do you have?", h/10);
        scanf("%d" , &n);
        printf("How many pennies do you have?", h/5);
        scanf("%d" , &p);
    
        printf("you entered= %d half dollars", h);
        printf("you entered= %d quarters", q);
        printf("you entered= %d dimes", d);
        printf("you entered= %d nickels", n);
        printf("you entered= %d pennies", p);
    
        int sum= h+q+d+n+p;
        int add= h*50 + q*25 + d*10 + n*5 + p;
    
        printf("the value of your %d coins is equivalent to %d pennies", sum, add);
    
        return 0;
    }
    Some notes:

    - I don't see "#include <stdio.h>" in your code
    - Lines 10, 12, 14, 16, and 18: You don't need the second argument in those "printf()" statements
    - Is it difficult to see the results, since you don't have any newlines to neatly display the output?



    You're welcome.
    Just did that. Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me in simple programs
    By saweb in forum C Programming
    Replies: 4
    Last Post: 02-13-2012, 12:34 PM
  2. Replies: 6
    Last Post: 07-14-2011, 09:48 AM
  3. WIN API simple programs
    By Blizzarddog in forum Windows Programming
    Replies: 4
    Last Post: 03-24-2003, 02:58 AM
  4. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 10:38 AM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 09:45 PM