I'm kinda new to the whole C/C++ thing, and I have this assignment that I'm having trouble with. I have to develop a C program that approximates pi using Abraham Sharp's formula. I developed a succesful program at university. using a Borland compiler. I printed out the contents of the source file, so that I could have a copy of the program on my computer at home. I copied out the programming into a new source file using Dev-C++ v.4.9.9.2 and when I compile the program using C, I get the following error message:
After reading through this FAQ, I have resolved the problem with the filename and have removed all spaces from the path. I don't know why it's still coming up with that path.
Anyway, the compile log contains the following messages:
gcc.exe: installation problem, cannot exec `cc1': No such file or directory
make.exe: *** [Craig_McFadyen.o] Error 1
Execution terminated
I have searched for the gcc.exe file, it is present and it opens, displaying a blank C++ window. I have also searched for and found the cc1.exe file, and it does exist.
The program works using C++, but it seems to come up with different answers to what I was getting at university, even though the code is exactly the same, and I cannot get it to even compile using C, and the program needs to be written in C.
Does anybody have any idea why this fault is occuring?
Here is a copy of my code with all comments, in case there are any mistakes in it that might be causing this fault.
/This program uses Sharp's formula and the corresponding series to approximate the value of pi/
include <stdio.h>
include <math.h>
int main(void)
{
float sign=1, estimate, pow3=1, sum=1.0, term, variation; /This line initialises all the float terms used in the algorithm/
int k, n; /This line initialises the variables k and n/
printf("ThisprogramapproximatespiusingSharp's series,\n"); /*This line describes to the user what function the program carries out*/printf("(2*sqrt3)*(1-(1/(3*3))+(1/((3*3)*5))-(1/((3*3)*7))+...)\n"); /*This line displays the start of Sharp'sseries*/printf("Pleaseenterthenumberoftermsyouwouldliketouse:\n");/*This line requests an input from the user*/scanf("%d",&n);/*This line reads in the value 'n' which represents the number of terms in the series to use to approximate pi*/for(k=1;k<=n;k++);/*This line sets k to the value 1 and increments it by 1 for every loop until k is equal to n*/{pow3=pow3*3;/*This line calculates 3 to the power k*/sign*=-1.0;/*This line causes the sign before each term to alternate between + and - for each loop*/term=sign/(pow3*(2*k+1));/*This line represents the bottom line of Sharp's formula*/sum+=term;}estimate=(2*sqrt(3))*sum;/*This line represents the top line of Sharp's formula*/printf("Theapproximationofpiis%f\n",estimate);/*This line prints the approximation of pi*/variation=M_PI-estimate;/*This line calculates the variation between the approximation of pi and the stored value of pi*/printf("Thevariationfromthestoredvalueofpiis%f\n",variation);/*This line prints the variation between the approximation of pi and the stored value of pi*/printf("Thankyouforusingthisprogram!\n");return(0);
}
Thanks for your time,
Craig
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Note also that M_PI is a double whereas estimate is a float. You are attempting to display the difference as a float with the default display precision. If the approximation is any good, difference will always display as zero.
I implemented the formula using double precision on VC++ 2008 and got the following result:
Sharp's pi = 3.1415926535897936
compare with M_PI: 3.1415926535897931
Converged in 32 iterations
Press any key to continue . . .
So the approximation is good for 15 decimal places for k = 0 to 31.
Of course I cannot post my code ;-) I'll post the comment to the loop (ignore the dots, I inserted them to preserve the formatting:
// Abraham Sharp's formula (ref. http://mathworld.wolfram.com/PiFormulas.html ):
// pi = SUM[for k = 0...#INF] { 2 * (-1)^k * 3^(0.5 - k) . }
// .......................... {. ____ ..... }
// .......................... { ........ 2k + 1 ........... }
// .......................... { ........................... }
//
// Approximate by converging asymptotically until incremental change is zero
// within the precision of the double precision floating point representation
Note that is pretty much the only comment in the code. The loop body is just three lines so I would say that was sufficient comment.
I ran yours (which incidentally does not compile without warnings) and got:
This program approximates pi using Sharp's series,
(2sqrt3)(1-(1/(33))+(1/((33)5))-(1/((33)*7))+...)
Please enter the number of terms you would like to use:
31
The approximation of pi is 3.446337
The variation from the stored value of pi is -0.304744
Thank you for using this program!
Press any key to continue . . .
which rather suggests to me that it does not really work!
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
... if it worked with the Borland compiler I am guessing that the likelihood is that you have transcribed the code incorrectly. I wonder why you printed and retyped it rather than simply putting it on a memory stick/card or floppy or even e-mailing it to yourself?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Well you obviously still have paths with spaces because that is what the build log says, and it had no trouble finding the makefile at that location so it must exist. So what you believe and what the log is telling us are different, and the log is certainly correct. I suggest that you remove or rename those paths entirely, recreate teh project file from scratch and then add your existing code to the project (removing any default code provided - you don't want two main() functions!)
Your problem may also be that you are running this on Vista. If so you need to read the "read first" thread again with respect to its directions on installing on Vista.
Alternatively do yourself a favour and use VC++ 2008 Express Edition - it is free and has a debugger that works.
An opinion you did not ask for follows, so feel free to ignore me...
I would suggest perhaps you have gone a bit overboard on the commenting. Good commenting style is generally to comment blocks of code that implement a distinct stage in the algorithim. Think about it; does for example - "This line prints the approximation of pi" really add much to what the code is already telling you? If the reader has no idea what printf() does, they have no business reading the code! Similarly for the for-loop comment. Overuse of end-line comments rather than block comments is also bad form. For example a comment at the head of a loop telling you what the entire loop is for is far more informative and useful that an end-line comment explaining the syntax of a for-loop; the reader should already understand that! You comment every line in assembler (arguably), not C.
You should also aim to comment the code with reference to the part or term of the formula it calculates, because I am looking at the formula (on mathworld.wolfram.com ), and at your code, and it is not immediately clear to me, and that surely is the point of commenting? For example I am having trouble matching your "top-line" and "bottom-line" terms with the formula I have found. Perhaps your comment should include the formula or a web-link to teh refernce you are using?
I would suggest that the phrase "This line..." is redundant in all cases for a line comment, its kind of implicit don't you think?
It may sound pedantic, and you may think that too much commenting is harmless and better than too little, but looking at your code, I would say that you are making it both hard to read and hard to maintain. If someone is going to have to mark this, making it hard to read is a bad idea, and if you want to ensure your comments are accurate, making it hard to maintain is not good either.
So for example I would comment thus:
/ Calculate 3^k cumulatively /
pow3 = pow3 * 3 ;
/ Toggle sign /
sign *= -1.0 ;
/...I admit to not being sure about this one!(clifford).../
term = sign/(pow3(2k+1));
/ Accumulate /
sum += term ;
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi there,
I'm kinda new to the whole C/C++ thing, and I have this assignment that I'm having trouble with. I have to develop a C program that approximates pi using Abraham Sharp's formula. I developed a succesful program at university. using a Borland compiler. I printed out the contents of the source file, so that I could have a copy of the program on my computer at home. I copied out the programming into a new source file using Dev-C++ v.4.9.9.2 and when I compile the program using C, I get the following error message:
D:\Uni Files\Praxis 2\Assignments\Makefile.win [Build Error] [Craig_McFadyen.o] Error 1
After reading through this FAQ, I have resolved the problem with the filename and have removed all spaces from the path. I don't know why it's still coming up with that path.
Anyway, the compile log contains the following messages:
Compiler: Default compiler
Building Makefile: "D:\Uni Files\Praxis 2\Assignments\Makefile.win"
Executing make...
make.exe -f "D:\Uni Files\Praxis 2\Assignments\Makefile.win" all
gcc.exe -c Craig_McFadyen.c -o Craig_McFadyen.o -I"C:/Dev-Cpp/include"
gcc.exe: installation problem, cannot exec `cc1': No such file or directory
make.exe: *** [Craig_McFadyen.o] Error 1
Execution terminated
I have searched for the gcc.exe file, it is present and it opens, displaying a blank C++ window. I have also searched for and found the cc1.exe file, and it does exist.
The program works using C++, but it seems to come up with different answers to what I was getting at university, even though the code is exactly the same, and I cannot get it to even compile using C, and the program needs to be written in C.
Does anybody have any idea why this fault is occuring?
Here is a copy of my code with all comments, in case there are any mistakes in it that might be causing this fault.
/
Name: Craig McFadyen
Course: Mechanical Engineering
B31PY2 Assignment Number: 1
*/
/This program uses Sharp's formula and the corresponding series to approximate the value of pi/
include <stdio.h>
include <math.h>
int main(void)
{
float sign=1, estimate, pow3=1, sum=1.0, term, variation; /This line initialises all the float terms used in the algorithm/
int k, n; /This line initialises the variables k and n/
}
Thanks for your time,
Craig
Note also that M_PI is a double whereas estimate is a float. You are attempting to display the difference as a float with the default display precision. If the approximation is any good, difference will always display as zero.
I implemented the formula using double precision on VC++ 2008 and got the following result:
Sharp's pi = 3.1415926535897936
compare with M_PI: 3.1415926535897931
Converged in 32 iterations
Press any key to continue . . .
So the approximation is good for 15 decimal places for k = 0 to 31.
Of course I cannot post my code ;-) I'll post the comment to the loop (ignore the dots, I inserted them to preserve the formatting:
// Abraham Sharp's formula (ref. http://mathworld.wolfram.com/PiFormulas.html ):
// pi = SUM[for k = 0...#INF] { 2 * (-1)^k * 3^(0.5 - k) . }
// .......................... {. ____ ..... }
// .......................... { ........ 2k + 1 ........... }
// .......................... { ........................... }
//
// Approximate by converging asymptotically until incremental change is zero
// within the precision of the double precision floating point representation
Note that is pretty much the only comment in the code. The loop body is just three lines so I would say that was sufficient comment.
I ran yours (which incidentally does not compile without warnings) and got:
This program approximates pi using Sharp's series,
(2sqrt3)(1-(1/(33))+(1/((33)5))-(1/((33)*7))+...)
Please enter the number of terms you would like to use:
31
The approximation of pi is 3.446337
The variation from the stored value of pi is -0.304744
Thank you for using this program!
Press any key to continue . . .
which rather suggests to me that it does not really work!
Clifford
... if it worked with the Borland compiler I am guessing that the likelihood is that you have transcribed the code incorrectly. I wonder why you printed and retyped it rather than simply putting it on a memory stick/card or floppy or even e-mailing it to yourself?
Well you obviously still have paths with spaces because that is what the build log says, and it had no trouble finding the makefile at that location so it must exist. So what you believe and what the log is telling us are different, and the log is certainly correct. I suggest that you remove or rename those paths entirely, recreate teh project file from scratch and then add your existing code to the project (removing any default code provided - you don't want two main() functions!)
Your problem may also be that you are running this on Vista. If so you need to read the "read first" thread again with respect to its directions on installing on Vista.
Alternatively do yourself a favour and use VC++ 2008 Express Edition - it is free and has a debugger that works.
An opinion you did not ask for follows, so feel free to ignore me...
I would suggest perhaps you have gone a bit overboard on the commenting. Good commenting style is generally to comment blocks of code that implement a distinct stage in the algorithim. Think about it; does for example - "This line prints the approximation of pi" really add much to what the code is already telling you? If the reader has no idea what printf() does, they have no business reading the code! Similarly for the for-loop comment. Overuse of end-line comments rather than block comments is also bad form. For example a comment at the head of a loop telling you what the entire loop is for is far more informative and useful that an end-line comment explaining the syntax of a for-loop; the reader should already understand that! You comment every line in assembler (arguably), not C.
You should also aim to comment the code with reference to the part or term of the formula it calculates, because I am looking at the formula (on mathworld.wolfram.com ), and at your code, and it is not immediately clear to me, and that surely is the point of commenting? For example I am having trouble matching your "top-line" and "bottom-line" terms with the formula I have found. Perhaps your comment should include the formula or a web-link to teh refernce you are using?
I would suggest that the phrase "This line..." is redundant in all cases for a line comment, its kind of implicit don't you think?
It may sound pedantic, and you may think that too much commenting is harmless and better than too little, but looking at your code, I would say that you are making it both hard to read and hard to maintain. If someone is going to have to mark this, making it hard to read is a bad idea, and if you want to ensure your comments are accurate, making it hard to maintain is not good either.
So for example I would comment thus:
/ Calculate 3^k cumulatively /
pow3 = pow3 * 3 ;
/ Toggle sign /
sign *= -1.0 ;
/...I admit to not being sure about this one!(clifford).../
term = sign/(pow3(2k+1));
/ Accumulate /
sum += term ;
Clifford