C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-07-2006, 08:43 AM   #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!!!!!
abs.emailverify is offline   Reply With Quote
Old 11-07-2006, 08:50 AM   #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!!!!!
abc123 is offline   Reply With Quote
Old 11-07-2006, 09:14 AM   #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?
abs.emailverify is offline   Reply With Quote
Old 11-07-2006, 09:51 AM   #4
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
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.
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 11-07-2006, 10:02 AM   #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?
abs.emailverify is offline   Reply With Quote
Old 11-07-2006, 10:36 AM   #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?
abc123 is offline   Reply With Quote
Old 11-07-2006, 10:45 AM   #7
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
> 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.

Salem is offline   Reply With Quote
Old 11-07-2006, 11:21 AM   #8
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
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;
__________________
On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
--Charles Babbage, 1792-1871

09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
hk_mp5kpdw is offline   Reply With Quote
Old 11-08-2006, 08:43 AM   #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.
abs.emailverify is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Program Plan Programmer_P C++ Programming 0 05-11-2009 01:42 AM
Compile Public Domain Pocket PC C Program m1l Projects and Job Recruitment 2 07-20-2007 04:02 AM
Using variables in system() Afro C Programming 8 07-03-2007 12:27 PM
new to C--can't get program to compile samerune C Programming 12 04-02-2007 09:44 AM
how do i compile a program that deals w/classes? Shy_girl_311 C++ Programming 5 11-11-2001 02:32 AM


All times are GMT -6. The time now is 11:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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