Arithmetic/Geometric/Harmonic

This is a discussion on Arithmetic/Geometric/Harmonic within the C Programming forums, part of the General Programming Boards category; Hey I'm currently stuck on a problem. I have to create a program that reads the input of two numbers ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    Arithmetic/Geometric/Harmonic

    Hey

    I'm currently stuck on a problem. I have to create a program that reads the input of two numbers and works out their Arithmetic/Geometric/Harmonic mean. It then calculates which mean is the highest and displays it on the screen.

    This is what I have so far...

    Code:
    #include <math.h> 
    
    int main()
    {
    	Double num1
    	Double num2
    	Double ari
    	Double geo
    	Double har
    
    	printf("enter num1");
    	scanf ("%d%*C" , &num1),
    
    	printf("enter num2");
    	scanf ("%d%*C" , &num2);
    
    	ari = ((num1+num2)/2);
    	printf("Arithmetic Mean =",ari);
    
    
    	printf("enter num1");
    	scanf ("%d%*C" , &num1),
    
    	printf("enter num2");
    	scanf ("%d%*C" , &num2);
    	
    	geo = ((num1*num2)
    
    	printf("enter num1");
    	scanf ("%d%*C" , &num1),
    
    	printf("enter num2");
    	scanf ("%d%*C" , &num2);
    
    	har =

    Is this correct so far? Is there a way I can make it shorter? For example, do I actually need printf/scanf before each mean?

    I also have no idea how to write the Harmoic code :/

    Thanks 4 any help

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You shouldn't ask for input, but use the same values for all three means (I would imagine). Do you have the formula for harmonic mean? (If not: the harmonic mean of x and y is 2xy/(x+y).)

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by DJ_Steve View Post
    Hey

    I'm currently stuck on a problem. I have to create a program that reads the input of two numbers and works out their Arithmetic/Geometric/Harmonic mean. It then calculates which mean is the highest and displays it on the screen.

    This is what I have so far...

    Code:
    #include <math.h> 
    
    int main()
    {
    	Double num1
    	Double num2
    	Double ari
    	Double geo
    	Double har
    
    	printf("enter num1");
    	scanf ("%d%*C" , &num1),
    
    	printf("enter num2");
    	scanf ("%d%*C" , &num2);
    
    	ari = ((num1+num2)/2);
    	printf("Arithmetic Mean =",ari);
    
    
    	printf("enter num1");
    	scanf ("%d%*C" , &num1),
    
    	printf("enter num2");
    	scanf ("%d%*C" , &num2);
    	
    	geo = ((num1*num2)
    
    	printf("enter num1");
    	scanf ("%d%*C" , &num1),
    
    	printf("enter num2");
    	scanf ("%d%*C" , &num2);
    
    	har =

    Is this correct so far? Is there a way I can make it shorter? For example, do I actually need printf/scanf before each mean?

    I also have no idea how to write the Harmoic code :/

    Thanks 4 any help
    1. There's nothing as Double in C. C is case sensitive and you should change it to double.
    2. For double the format specifier is %f not %d.
    3. You can combine the scanf's in one as
    Code:
    scanf("%f%f",&num1,&num2);
    4. I think the geometric mean is square root of num1*num2.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    ok, I'll get working on that, thanks for the help

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    Code:
    #include <math.h>
    
    int main()
    {
    	double num1
    	double num2
    	double ari
    	double geo
    	double har
    
    	printf("enter num1");
    	printf("enter num2");
    	scanf("%f%f",&num1,&num2);
    
        ari = ((num1+num2)/2);
    	printf("Arithmetric Mean =",ari);
    
    	geo = sqrt(num1*num2)
    	printf("Geometric Mean =",geo);
    }
    
    	return()
    this is so hard! It keeps saying that num1, num2, geo, ari are all undeclared?! But I declare them at the start of the program? :/

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by DJ_Steve View Post
    Code:
    #include <math.h>
    
    int main()
    {
    	double num1
    	double num2
    	double ari
    	double geo
    	double har
    
    	printf("enter num1");
    	printf("enter num2");
    	scanf("%f%f",&num1,&num2);
    
        ari = ((num1+num2)/2);
    	printf("Arithmetric Mean =",ari);
    
    	geo = sqrt(num1*num2)
    	printf("Geometric Mean =",geo);
    }
    
    	return()
    this is so hard! It keeps saying that num1, num2, geo, ari are all undeclared?! But I declare them at the start of the program? :/
    Except of course, you don't. Maybe if you put semicolons where they belonged, you would.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    Code:
    #include <math.h>
    
    int main()
    {
    	double num1;
    	double num2;
    	double ari;
    	double geo;
    	double har;
    
    	printf("enter num1\n");
    	scanf ("%f%*C" , &num1);
    	printf("enter num2\n");
    	scanf ("%f%*C" , &num2);
    
        ari = ((num1+num2)/2);
    	printf("Arithmetric Mean =\n",ari);
    
    	geo = sqrt(num1*num2);
    	printf("Geometric Mean =\n",geo);
    
    	return 0;
    }
    yay, I did it although at the moment the answers don't actually appear when I type in the 2 numbers

    but at least I'm getting there!


    Sorry about all these random questions but my tutor is no help and I pretty much have to learn everything from scratch by myself

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by DJ_Steve View Post
    Code:
    #include <math.h>
    
    int main()
    {
    	double num1;
    	double num2;
    	double ari;
    	double geo;
    	double har;
    
    	printf("enter num1\n");
    	scanf ("%f%*C" , &num1);
    	printf("enter num2\n");
    	scanf ("%f%*C" , &num2);
    
        ari = ((num1+num2)/2);
    	printf("Arithmetric Mean =\n",ari);
    
    	geo = sqrt(num1*num2);
    	printf("Geometric Mean =\n",geo);
    
    	return 0;
    }
    yay, I did it although at the moment the answers don't actually appear when I type in the 2 numbers

    but at least I'm getting there!


    Sorry about all these random questions but my tutor is no help and I pretty much have to learn everything from scratch by myself
    1. #include<stdio.h> is missing
    2. Your printf statements are wrong. They should be like this.
    Code:
    printf("Arithmetric Mean =%f \n",ari); // change for geometric mean also
    3. Why are you using %*C in your scanf?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    oh the question said to put #include <math.h> and didn't even explain why, so I just did what it told me to do

    I shall make the changes now

    um, i dunno why I'm using %*C...i thought it was right lol ack

    thanks 4 help

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    actually I just noticed I got rid of the %*C already, not sure why I had them in the first place

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    43
    does anyone know why I would get the answer

    Arimetic Mean = 1.#QNAN0
    Geo Mean = 1.#QNAN0

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Probably your attempt to read in a double with %f instead of %lf.

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    43

    thanks

    what is the difference?

Popular pages Recent additions subscribe to a feed

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