Thread: C program compile but not doing the calculations

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Unhappy C program compile but not doing the calculations

    Hello all,
    I wrote a C program and I compile with Miracle C - a C compiler software, the program was compiled successfully but does not do any calculations under it. All the calculations I put there was not done, I included math.h and stdio.h, is there anything I am doing wrong??

    Below is the program:

    Code:
    /* program to calculate the geometric and arithmetric average of a set of numbers*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float y, x, xavg, xavg1, temp=1.0, sum=0.0;
       int count=1, n;
       printf ("\nEnter the total items to calculate\n");
       scanf ("%d", &n);
       while (count<=n)
         {
         printf ("\nEnter x\n");
         scanf ("%f", &x);
         temp*=x;
         sum+=x;
         ++count;
         };
       y = 1/n;
       xavg= pow(temp, y);
       xavg1= sum/n;
       printf ("\nThe geometric average is %f\n", &xavg);
       printf ("\nThe arithmetric average is %f\n", &xavg1);
    }
    I need help urgently!!!!!

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    The argument in printf statement went wrong

    Remove the '&' in printf statement for the variables

    Quote Originally Posted by abs.emailverify
    Hello all,
    I wrote a C program and I compile with Miracle C - a C compiler software, the program was compiled successfully but does not do any calculations under it. All the calculations I put there was not done, I included math.h and stdio.h, is there anything I am doing wrong??

    Below is the program:

    Code:
    /* program to calculate the geometric and arithmetric average of a set of numbers*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float y, x, xavg, xavg1, temp=1.0, sum=0.0;
       int count=1, n;
       printf ("\nEnter the total items to calculate\n");
       scanf ("%d", &n);
       while (count<=n)
         {
         printf ("\nEnter x\n");
         scanf ("%f", &x);
         temp*=x;
         sum+=x;
         ++count;
         };
       y = 1/n;
       xavg= pow(temp, y);
       xavg1= sum/n;
       printf ("\nThe geometric average is %f\n", &xavg);
       printf ("\nThe arithmetric average is %f\n", &xavg1);
    }
    I need help urgently!!!!!

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Thank you so much, it works.

    But i want to calculate x^y(x raise to power y) and dont how to write the statement in C, anyone here to help?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by abs.emailverify
    Thank you so much, it works.

    But i want to calculate x^y(x raise to power y) and dont how to write the statement in C, anyone here to help?
    You do know how (you just might not realize it), you did it here:
    Code:
    xavg= pow(temp, y);
    So apply that knowledge to what you are trying to do.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by hk_mp5kpdw
    You do know how (you just might not realize it), you did it here:
    Code:
    xavg= pow(temp, y);
    So apply that knowledge to what you are trying to do.
    I did that but I am not getting the correct answer, I use my calculator and I got a different answer. No matter the values I entered I got 1.00 as answer or maybe I am doing something wrong?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    The function you used goes wrong

    FYI

    #include <math.h>

    double pow(double x, double y);

    float powf(float x, float y);

    long double powl(long double x, long double y);


    So try to change code as per your requirement.

    Quote Originally Posted by abs.emailverify
    I did that but I am not getting the correct answer, I use my calculator and I got a different answer. No matter the values I entered I got 1.00 as answer or maybe I am doing something wrong?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and I compile with Miracle C - a C compiler software
    Well there's your problem, this is a toy, not a C compiler.

    Trust me, it won't take long before you realise that it's a PoS.
    Just do yourself a big favour and get dev-C++.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    float y, x, xavg, xavg1, temp=1.0, sum=0.0;
    int count=1, n;
    ...
    y = 1/n;
    That's another problem, integer division (1 divided by an integer n) results in an integer which is then assigned to a float. You need to adjust that calculation a bit so that you are doing floating-point division:

    Code:
    y = 1.0 / n;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Thank you a lot. I have done it and is working. I so much appreciate your effort in helping me solve this problem.

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. Compile Public Domain Pocket PC C Program
    By m1l in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 07-20-2007, 04:02 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. new to C--can't get program to compile
    By samerune in forum C Programming
    Replies: 12
    Last Post: 04-02-2007, 09:44 AM
  5. how do i compile a program that deals w/classes?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2001, 02:32 AM