Thread: Program won't calculate mean or sleep

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Program won't calculate mean or sleep

    Code:
    #include <stdio.h>
    
    int main(void)
    {
     printf("Please enter three numbers and I shall return the average.\n");
     
     int num1 = 0;
     int num2 = 0;
     int num3 = 0;
     double mean = 0;
     
     printf("Enter the first number\n");
     scanf("&#37;i",&num1);
     
     printf("Enter the second number\n");
     scanf("%i",&num2);
     
     printf("Enter the third number\n");
     scanf("%i",&num3);
     
     
     mean = (double) num1 + (double) num2 + (double) num3;
     mean /= 3.0;
     
    // It will perform everything upto this point. Then it sleeps for three seconds and exits // //without displaying the following printf. But when the printf is displayed the mean is 0 for //some reason
    
     printf("The mean of the three numbers you entered is%i", mean);
     system("sleep 3s"); 
    
     return 0;
    }
    Any help will be greatly appreciated.
    Last edited by Deerman; 07-16-2007 at 06:29 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Print it as a double, not an int. Use either &#37;f or %lf. I can't remember which printf() takes, but it's definitely not %i or %d for doubles.

    Also, why not just call sleep() or Sleep() yourself? For Windows include <Windows.h>, and call Sleep() with the number of milliseconds to sleep, which in your case would be 3000.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    58
    Quote Originally Posted by MacGyver View Post
    Print it as a double, not an int. Use either %f or %lf. I can't remember which printf() takes, but it's definitely not %i or %d for doubles.

    Also, why not just call sleep() or Sleep() yourself? For Windows include <Windows.h>, and call Sleep() with the number of milliseconds to sleep, which in your case would be 3000.
    printf and scanf both use f for float and lf for double.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Re:

    That <Windows.h> still doesn't fix the problem it gives the same result as system("Sleep 3s").
    The final printf is still not showing.

    The sum of num1 + num2 + num3 is 0 for some reason.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Deerman View Post
    That <Windows.h> still doesn't fix the problem it gives the same result as system("Sleep 3s").
    The final printf is still not showing.
    Because you didn't put "\n" at the end of the string.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    58
    printf("The mean of the three numbers you entered is&#37;i", mean);

    You're using %i, it should be %lf. McGyver told you the same earlier.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Govalant View Post
    printf and scanf both use f for float and lf for double.
    Not right

    f - double - Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
    So printf uses f for double (and for floats that are silently promoted to double)...

    scanf - cannot do the same because it gets pointers to floats and double, so it have to know the exact size of the pointed container...
    That's why scanf has different formats for float and double
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    By the way, this isn't standard C, you aren't declaring your variable at the good place (you are doing like those silly C++ programmers who are declaring variables everywhere ). C let you only declare variables after an '{' or after another variable declaration (in the case it's not global variable).

    That said... you could change this
    Code:
     mean = (double) num1 + (double) num2 + (double) num3;
     mean /= 3.0;
    for this

    Code:
    mean = (double) (num1 + num2 + num3);
    mean = mean / 3.0;
    or, simple, this

    Code:
    mean = (num1 + num2 + num3) / 3.0;
    or

    Code:
    mean = (double) (num1 + num2 + num3) / 3;
    Anyway, just to point out that there is "better" way to use cast than others.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Your mean variable should first be initialized to double standard 0.00, and

    mean = (num1 + num2 + num3) / 3.0; should give you the ideal result with a printf("&#37;lf", mean) or %.2lf for precise 2 decimals.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to calculate the program running took?
    By DaniiChris in forum C Programming
    Replies: 17
    Last Post: 07-07-2008, 01:56 PM
  2. Program to calculate charge for parked cars
    By strider496 in forum C Programming
    Replies: 6
    Last Post: 03-20-2005, 06:24 PM
  3. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM