Can't figure out why this program won't work

This is a discussion on Can't figure out why this program won't work within the C Programming forums, part of the General Programming Boards category; This program is supposed to calculate the average, geometric mean, and harmonic mean of n number of numbers. Code: #include ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Can't figure out why this program won't work

    This program is supposed to calculate the average, geometric mean, and harmonic mean of n number of numbers.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
    	double average, harmonic_mean, geometric_mean; 
    	int num = 1;
    	int sum = 0;
    	int num_count = 0;
    	double invsum = 0.0;
    	int product = 1;
    
    while (num!=0)
    {
    printf("Enter a number(0 to quit): ");
    scanf("%d", &num);
    	if(num!=0)
    	{
    	sum = sum + num;
    
    	printf("The sum is: ");
    	printf("sum: %d\n", sum);
    	num_count = num_count + 1;
    	printf("num_count: %d\n", num_count);
    	product = product*num;
    	printf("product: %d\n", product);
    	invsum = invsum + (1/num);
    	printf("invsum: %f\n", invsum);
    
    	average = sum/num_count;
    	harmonic_mean = num_count/invsum;
    	geometric_mean = pow(product, 1.0/num_count);
    	}
    }
    
    
    printf("Average: %f\n",sum/(num_count));
    printf("Harmonic mean : %f\n",num_count/(invsum));
    printf("Geometric mean: %f\n", pow(product,1/(num_count))); 
    
    return(0);
    }
    I did the printf of sum, num_count, product, and invsum to see why the average, harmonic mean, and geometric mean weren't coming out right, and it seems the problem comes from invsum, but I can't figure out why.

    Could someone please help me? The average is obviously the sum of all the numbers divided by the number of numbers, and in case it is not clear what the harmonic mean and geometric mean are, here is what they are:

    Harmonic: n/ [(1/x1) + (1/x2) + ... (1/xn)]
    Geo: (x1 * x2 * ... *xn) ^ (1/n)

    TIA for any help.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    1/num is an int
    if num== 1 result is 1, if num is bigger - result is 0

    use double arithmetics: 1.0 / num
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    type cast the number num

    invsum = invsum + (1/(double)num);

    and getch()

    will work

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    I recompiled it with num as a doubles, and for some reason it always thinks the product is 1 now, anyone have any ideas? It also thinks sum and num are the same thing (sum = num, instead of equaling 5 if you entered 4 and 1, it says sum = 2).

    It could just be my crappy compiler, could someone do a test run for me?

    If you enter 3, 3, 2, average should be 2.67, harmonic should be 2.57, and geometric should be 2.62.

    Thanks

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
    	double average, harmonic_mean, geometric_mean; 
    	double num = 1.0;
    	int sum = 0;
    	int num_count = 0;
    	double invsum = 0.0;
    	int product = 1;
    
    while (num!=0)
    {
    printf("Enter a number(0 to quit): ");
    scanf("&#37;d", &num);
    	if(num!=0)
    	{
    	sum = sum + num;
    
    	printf("The sum is: ");
    	printf("sum: %d\n", sum);
    	num_count = num_count + 1;
    	printf("num_count: %d\n", num_count);
    	product = product*num;
    	printf("product: %d\n", product);
    	invsum = invsum + (1/num);
    	printf("invsum: %f\n", invsum);
    
    	average = sum/num_count;
    	harmonic_mean = num_count/invsum;
    	geometric_mean = pow(product, 1.0/num_count);
    	}
    }
    
    
    printf("Average: %f\n",sum/(num_count));
    printf("Harmonic mean : %f\n",num_count/(invsum));
    printf("Geometric mean: %f\n", pow(product,1/(num_count))); 
    
    return(0);
    }
    Last edited by Northstar; 11-25-2007 at 10:56 PM.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perhaps you should read in your double with a double type of format specifier, then?

    Code:
    scanf("%lf", &num);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Ah, duh!

    Ok, but now apparently there is another problem...

    I entered 3, 3, 2. It said that the sum was 8 (correct), the product was 18 (correct), num count was 3 (correct), invsum was 1.167 (correct) and then I entered 0 to compute the others, and Harmonic was 2.57 which is correct, but the average was a HUGE number (about 40 numbers long), and the geometric mean was 1. Does this mean that num_count needs to be a double?

    Still, I am not sure why the average does not work.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    Yes! That was the problem! Thanks for all of your guys' help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. RSA C Program doesnt work
    By szpengchao in forum C Programming
    Replies: 0
    Last Post: 04-17-2009, 12:47 AM
  3. My program doesn't work on others comps
    By Livijn in forum C# Programming
    Replies: 8
    Last Post: 10-29-2008, 06:03 AM
  4. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 09:59 PM
  5. Program doesn't work after changing computers.
    By RealityFusion in forum C++ Programming
    Replies: 7
    Last Post: 08-29-2005, 05:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21