eep. whoops. looks like you're already there. nm.
This is a discussion on Sine (Sin) Algorithm Help within the C Programming forums, part of the General Programming Boards category; eep. whoops. looks like you're already there. nm....
eep. whoops. looks like you're already there. nm.
Nope, Illinois, then Missouri. When we moved down here in '84 I knew more than the teacher in computer class, so much so that he threw me out of class one day because I made him look like an idiot on several occasions. He was prattling on about every command taking exactly 2 bytes or something.
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
Good times. Yeah well certainly its no biggy if you weren't familiar with some notation. I am sure someone will jump in and argue that point, but its no thing at all. It would be nice to see books use it more, however. It would serve to closely extend the relationship between math and computer science. In other words, make math seem less scary looking for programmers who aren't as mathematically inclined as you and me.
Back on topic, can the program use inline assembly? I was thinking an SSE solution
Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.
I don't think so. But I'm not 100% sure. (I have no idea what it is actually..lol). But I'm having some issues with the main program. I'll post it and see what you guys think. It gets stuck after I enter in the number to figure out which place I should go in the switch statement. Anyways, here it is.
Code:#include <fstream> // fstream.h contains file sequential IO methods. #include <stdlib.h> // stdlib.h contains the exit method. #include <stdio.h> #include <string.h> #include "mathlib.cpp" using namespace std; int main( ) { char fileOutName[25]; char response; int n = 0; int choice = 0; int again = 1; float angle = 0.0; float sum = 0.0; float radius = 0.0; float surface = 0.0; float volume = 0.0; float height = 0.0; int filecount = 0; //The number of lines in the file. FILE *outFile; // Logical output file name in program. //Gets the file out name and then checks to see if it already exists //If so then it asks for you to choose whether or not to override. printf("What is the name of the output file (max 25 characters)?"); scanf("%s", &fileOutName); if((outFile = fopen(fileOutName, "r" )) != NULL ) { printf("\nA file by the name %s exists.\n", fileOutName); printf("Do you wish to overwrite (Y or N): "); //Skip <cr> in buffer with "%*c", i.e., one character scanf("%*c %c", &response); if( (response == 'n') || (response == 'N') ) { // should close file first printf("\nProgram aborted to prevent overwirte!"); exit(1); } } outFile = fopen( fileOutName, "wt" ); if( outFile == NULL ) { printf("Could not create the output file! Program terminating."); exit(1); } printf("What would you like to do? 1 for Sphere, 2 for Cylinder, 3 for float addition, 4 for sin.\n"); scanf("%d", choice); switch( choice ) { case 1: printf("What is the radius of the sphere?\n"); scanf("%f", radius); sphere(radius, surface, volume); printf("The sphere with a radius of %f has a surface of %f and a volume of %f.\n", radius, surface, volume); fprintf(outFile, "The sphere with a radius of %f has a surface of %f and a volume of %f. \n", radius, surface, volume); printf("Go again? (1 for yes, 2 for no)\n"); scanf("%d", again); break; case 2: printf("What is the radius of the cylinder?\n"); scanf("%f", radius); printf("What is the height of the cylinder?\n"); scanf("%f", height); volume = volCylinder(radius, height); printf("The cylinder with a radius of %f and a height of %f has a volume of %f.\n", radius, height, volume); fprintf(outFile, "The cylinder with a radius of %f and a height of %f has a volume of %f.\n", radius, height, volume); printf("Go again? (1 for yes, 2 for no)\n"); scanf("%d", again); break; case 3: printf("How many floats are being processed? \n"); scanf("%d", n); float x[10]; for (int i = 0; i < n; i++) { printf("What is the float?\n"); scanf("%f", x[i]); } for (int j = 0; j < n - 1; j++) { for (int k = j + 1; k < n; k++) { if (x[j] > x[k]) { int temp = x[j]; x[j] = x[k]; x[k] = temp; } } } sum = sumFloats(x, n); printf("The sum of the floats is %f. \n", sum); fprintf(outFile, "The sum of the floats is %f. \n", sum); printf("Go again? (1 for yes, 2 for no)\n"); scanf("%d", again); break; case 4: double answer; printf("What is the angle? \n"); scanf("%f", angle); answer = sine(angle); printf("The sin of angle %f is %f. \n", angle, answer); fprintf(outFile, "The sin of angle %f is %f. \n", angle, answer); printf("Go again? (1 for yes, 2 for no)\n"); scanf("%d", again); break; default: printf("Quitting."); again = 2; } fclose(outFile); return 0; }
Sigh... The sine function didn't work right. Any ideas on how to fix it?
Code:double sine(float angle) { #define EPSILON 0.000001 int d = 1; double guess = 0; int i = 1; float x2p1 = angle; for(guess = angle; guess * guess - angle < EPSILON || guess * guess - angle < -EPSILON; guess = (x2p1 / (d *((i * 2) +1)*(i *2)))) { x2p1 = x2p1 * angle * angle * -1; i = i +1; } return guess; }
Please people, I really really need some help on this, I tried several things and none have worked. Any ideas or any help would be very much appreciated.
is it right? are you not supposed to use &Code:scanf("%f", angle);
When using scanf,if the intake variable is not a pointer or an array, then you have to pass the address of the variable to the scanf function. Your entire main program is having this error. Did the other if cases went fine?Code:scanf("%f", &angle);
Last edited by chakra; 05-10-2008 at 07:31 PM.
scanf("%s", &fileOutName); - not correct.
fileoutname is an array,so your statement should be
scanf("%s", fileOutName);
Thank you chakra, I figured out that first one with the & a while back, but I never realised about the fileoutname thing. I had it in almost all of my code and it still worked, so I'll try and remove it and see what happens.
My biggest problem is the sine function. The rest of the program works great, just not the sine function.
n = 0 to infinity
sine(x) = (-1)^n * (x^(2n+1) / (2n+1)!)
can be expressed as:
given a factorial() function, and with infinity set to some suitable arbitrary value.Code:double sine(double x) { int i; int sign = 1; double sum = 0.0; for (i = 0; i < infinity; ++i) { sum += sign * (pow(x, 2 * i + 1) / factorial(2 * i + 1)); sign = -sign; } return sum; }
Of course there are problems: it is inefficient since it re-computes the power and factorial from scratch on each iteration. Then, because it computes them separately, they (especially the factorial) will become large very quickly and cause an overflow if infinity is too large. But it shows how easily it is to express a for loop once you express your formula in summation notation. Now, what you should do is fix it as described earlier, and only then do you incorporate your check with an epsilon value instead of an arbitrary value.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
I guess this should do. I am just a beginner. So you can just change the code to give a prof. look.Code:double sine(double x) { int i,n; int sign = -1; double sum = x,power=x,fact = 1.0; for (i = 1; i < infinity; ++i) { power = power * x *x; n = (2*i+1); fact = fact * n * (n-1); sum += sign * power/fact; sign = -sign; } return sum; }
Okay, I implemented yours, laserlight, and I tested it and the answer that I got with setting the loop count or n to 15 wasn't close to the value that I got from my calculated value.
You're only going to be able to go up to about 17! before your integer overflows.
Even something like n=7 should get your decent results for angles less than pi.
I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo