anyone a programmer or good at c++?

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
current build so far
#include <cstdlib>
#include <iostream>
#include <fstream>

float cos(float x, int n);
float sin(float x, int n);
float fact(int n);
float power(float x, int n);


using namespace std;


int main(int argc, char *argv[])
{
const float pi(22.0/7);
float e, f, g;
int n, x, count=0;
ifstream fin("ang.txt");
fin>>x;
cout<<"How many terms do you want to sum to?"<<endl;
cin>>n;
cout<<endl<<"Angle sin cos tan"<<endl;
cout<<"---------------------------------"<<endl;

while (!fin.eof())
{
count++;
e=sin(x,n)*180/pi;
f=cos(x,n)*180/pi;
g=sin(x,n)/cos(x,n)*180/pi;

cout<<e<<f<<g<<endl;
}

fin.close();
system("PAUSE");
return EXIT_SUCCESS;

}



float fact(int n)
{
if (n<2)
return 1;
else
return n*fact(n-1);
}


float power(float x, int n)
{
if (n==0)
return 1;
else
return x*power(x,n-1);
}


float sin(float x, int n)
{

if (n==0)
return 1;
else
return power(-1,n)*power(x,2*n+1)/fact(2*n+1);
}


float cos(float x, int n)
{
if (n==0)
return 1;
else
return power(-1,n)*power(x,2*n)/fact(2*n);
}
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
current build
#include <cstdlib>
#include <iostream>
#include <fstream>

float cos(float x, int n);
float sin(float x, int n);
float fact(int n);
float power(float x, int n);


using namespace std;


int main(int argc, char *argv[])
{
const float pi(22.0/7);
float e, f, g;
int n, x, count=0;
ifstream fin("ang.txt");
fin>>x;
cout<<"How many terms do you want to sum to?"<<endl;
cin>>n;
cout<<endl<<"Angle sin cos tan"<<endl;
cout<<"---------------------------------"<<endl;

while (!fin.eof())
{
count++;
e=sin(x,n)*180/pi;
f=cos(x,n)*180/pi;
g=sin(x,n)/cos(x,n)*180/pi;

cout<<e<<f<<g<<endl;
}

fin.close();
system("PAUSE");
return EXIT_SUCCESS;

}



float fact(int n)
{
if (n<2)
return 1;
else
return n*fact(n-1);
}


float power(float x, int n)
{
if (n==0)
return 1;
else
return x*power(x,n-1);
}


float sin(float x, int n)
{

if (n==0)
return 1;
else
return power(-1,n)*power(x,2*n+1)/fact(2*n+1);
}


float cos(float x, int n)
{
if (n==0)
return 1;
else
return power(-1,n)*power(x,2*n)/fact(2*n);
}
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
Hit me up on AIM/YIM (oipaloi). I'm not a code monkey and would like to get some advice. Or hit up this thread right here.

I'm using DEV C++ as my compiler.

I'm trying to calculate SIN/COS/TAN, etc without the use of CMATH. Using two recursive functions for the power and factorial. Gotta do some other things as well, but right now I'm just wondering why my values are off. Just wondering where I'm going wrong. Here's the code I've got so far. The first value in "ang.txt" is 90. When I get the number back (in radians), it's not one like it should be.

#include <cstdlib>
#include <iostream>
#include <fstream>

float cos(float x, int n);
float sin(float x, int n);
float fact(int n);
float power(float x, int n);


using namespace std;

int main(int argc, char *argv[])
{
const float pi(22.0/7);
float e, f, x;
int n, a;
ifstream fin("ang.txt");
fin>>a;
cout<<"How many terms do you want to sum to?"<<endl;
cin>>n;
e=sin(x,n)*180/pi;
f=cos(x,n)*180/pi;
cout<<endl<<"angle "<<a<<": sin = "<<e<<" cos = "<<f<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}



float fact(int n)
{
if (n<2)
return 1;
else

return n*fact(n-1);

}

float power(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
if (n==0)
return 1;
else
fin.close();
return x*power(x,n-1);

}

float sin(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
do
{
if (n==0)
return 1;
else
fin.close();
return (power(-1,n)*power(x,(2*n)+1))/fact((2*n)+1);
} while(!fin.eof());

}

float cos(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
if (n==0)
return 1;
else
fin.close();
return (power(-1,n)*power(x,2*n))/fact(2*n);

}
 

Kai

That Limey Bastard
Staff member
Okay - instead of calling data from an external text file - could you not adapt the program to rely on data input at the console? That would make things easier than having to constantly change the datafile, and would make debugging a lot easier. For Math C++ programs, its always best to do this. I have a few sections of C++ code that might be able to help if you have to go with the file call route..
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
Nope. It has to be from an input file.

The main problem I'm having is that the numbers are off.

I'm pretty sure it's not a problem with the fact or power functions, as they are cut n paste from when I used them to calculate e without CMATH.

FYI these are the SIN and COS
sm_photo_missing.jpg

sm_photo_missing.jpg
 

Attachments

  • sin.png
    sin.png
    1.8 KB · Views: 4
  • cos.png
    cos.png
    1.7 KB · Views: 1

Kai

That Limey Bastard
Staff member
Ok - hang a sec, lemme upload a program i had to do aaaaages ago that deals with input from a text file. The math looks correct, i think its just the method thats being used to call the text file. First thing you should try, is to add this as the first function to be executed in the file retrieval subsection:

int buffer[256];

That sets the buffer to accept integer only input from the file and to ignore all other ascii characters. It also sets the input buffer size to 256 bytes.

The whole section of my particular program looked like this, when trying to read in ascii data - as in, regurgitate a text file when prompted:

void input()

{
char buffer[256];

ifstream datafile ("input.txt");

if (! datafile.is_open())

{
cout << "Error opening file"; exit (1); // If the file cannot be found, ie, it
// is not present, an error message will
// be displayed, and exit the program.
}

while (! datafile.eof() )

{
datafile.getline (buffer,10000); // specifies the size of the display buffer in number of bytes

cout << buffer << endl;
}

system("PAUSE");
system("CLS");
}


If you dont set the correct input type for whatever file you're trying to get it to read, it'll screw up - you need to specify exactly what type of data it's going to be reading in - ie, INT for integers, FLOAT for FP numbers or CHAR for all other ascii characters with a non-numerical value.

HTH :)
 

lagged

1991 1JZ
Mar 30, 2005
2,616
0
0
39
new rochelle
alright id suggest a complete rewrite of that program.

the design of your functions are needlessly complicated.

your functions should take the parameters and return the value and nothing more.

all data used from your input stream should be taken care of in main()

also, you are needlessly specifying parameters for main() that you never pass.

if i were you i would start from scratch.

pretend you want to use each of your math functions over and over again, you want them to be independent of the task that main() is to complete.

each function should only do one thing, pass your parameters to it, and return your value. nothing more. they should not be reading from your filestream.
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
As is, it retrieves correctly and I can assign input from the file values without a problem. I know this because in main, I pulled the value from the input file, assigned it to a then cout-ed it and it was the correct value.

Are my equation in the SIN and COS functions put in there correctly, as per the images I posted? That's the part that I'm realy not sure on and think thats where the error is. but I dunno.
 

lagged

1991 1JZ
Mar 30, 2005
2,616
0
0
39
new rochelle
example:
Code:
float sin(float x, int n)
{
ifstream fin("ang.txt");
fin>>x;
do
{
if (n==0)
return 1;
else
fin.close();
return (power(-1,n)*power(x,(2*n)+1))/fact((2*n)+1);
} while(!fin.eof());

in main() you pass x to sin(float x, int n)

HOWEVER

in sin(...) you assign a value to x from your datastream.

so it looks like youre not using the value you pass to your sin function.
 

lagged

1991 1JZ
Mar 30, 2005
2,616
0
0
39
new rochelle
sorry, ok i see more weird things here.

x is declared in main() AND passed to your sin(...) function, yet in your sin(..) function as stated earlier, you are assigning a different value to x.

you should redesign the entire thing and post back, itll make it easier for me to help you.


ill even do one for you, stay tuned and ill post up a nice sin() function for you
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
okay, I rewrote as:

MAIN
int main(int argc, char *argv[])
{
const double pi(22.0/7);
int e, f, x, n;
ifstream fin("ang.txt");
fin>>x;
cout<<"How many terms do you want to sum to?"<<endl;
cin>>n;
e=sin(x,n)*180/pi;
f=cos(x,n)*180/pi;
cout<<endl<<"angle "<<x<<": sin = "<<e<<" cos = "<<f<<endl;
system("PAUSE");
return EXIT_SUCCESS;
fin.close();
}

SIN
double sin(double x, int n)
{
if (n==0)
return 1;
else
return (power(-1,n)*power(x,(2*n)+1))/fact((2*n)+1);
}


However, it's returning some values like -68912389893 crazy shit when it should just be 1. haha. As per the definition of sin (images I posted), am I putting it in there correctly?

BTW, thanks alot for helping me out guys.
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
It should have two, shouldnt it? Because I have to do it recursively without CMATH. It'll have n (how many times its going to be summed, as per the definition) and x (the angle).

???
 

lagged

1991 1JZ
Mar 30, 2005
2,616
0
0
39
new rochelle
alright a better way to go about this is to simply use

opposite/hypotenuse

for your value of sin.

these artificial restrictions make this problem so impractical that it becomes difficult to find the solution to relatively easy problem.

i understand your professor is trying to challenge you, but he could be more creative in his approach!

im only a 3rd year CS student, while i enjoy programming thoroughly, math is not my forte so i may be missing something here.

i have an idea,

create a header file with the prototype for your math functions and in the implementation #include <cmath> and only give your teacher the obj file and not the source!
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
There are no values for opp/hypo, haha. I wish, then i wouldnt have to do any of the recursive funstions. It only reads in angles.

I know it's such a pain in the ass. If I could only damn use CMATH, fucking "sin 90" done.

While I've got you here. How would you have it read in all the values and print them all out. do while (! eof style) in the function? How would you call it into main then?
 

outofstep

Senior Member
Mar 31, 2005
364
0
0
fwb
Works gorgeously now. Notice where the problem was, sin/cos weren't recursive. Duh... Can't do a limit without adding itself back onto itself. That was the big issue, just adding the function back onto itself. I cleaned up all the rest. There you go, a program that can calculate SIN/COS/TAN without the use of CMATH.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

//initializing all of the functions
double cos(double x, int n);
double sin(double x, int n);
double fact(int n);
double power(double x, int n);


using namespace std;


int main(int argc, char *argv[])
{
//initializing the variables
const double pi(22.0/7);
double e, f, g, n, x, count=0;

//opening the input file and getting how many times it should be summed together
ifstream fin("ang.txt");
cout<<"How many terms do you want to sum to?"<<endl;
cin>>n;
cout<<endl<<"Angle sin cos tan"<<endl;
cout<<"---------------------------------"<<endl;

// while loop that calls the functions, converts to radians, and prints out the results
while (!fin.eof())
{
count++;
fin>>x;
e=sin(x*pi/180.0,n);
f=cos(x*pi/180.0,n);
g=e/f;

cout<<fixed<<setprecision(3)<<x<<" "<<e<<" "<<f<<" "<<g<<endl;
}

fin.close();
system("PAUSE");
return EXIT_SUCCESS;

}


//this recursive function calculates the factorial part
double fact(int n)
{
if (n<2)
return 1;
else
return n*fact(n-1);
}

//this function calculates the power part
double power(double x, int n)
{
if (n==0)
return 1;
else
return x*power(x,n-1);
}

//this function calculates sin
double sin(double x, int n)
{

if (n==0)
return x;
else
return power(-1,n)*power(x,2*n+1)/fact(2*n+1)+sin(x,n-1);
}

//this function calculates cos
double cos(double x, int n)
{
if (n==0)
return 1;
else
return power(-1,n)*power(x,2*n)/fact(2*n)+cos(x,n-1);
}
 

lagged

1991 1JZ
Mar 30, 2005
2,616
0
0
39
new rochelle
looks much better.

you dont need int main(int argc, char *argv[])

only int main()

youre not using any parameters passed from the command line.
 

lagged

1991 1JZ
Mar 30, 2005
2,616
0
0
39
new rochelle
xscurry said:
Now try doing it in assembly without the nice libraries and functions of C++. ;)

while it would be a great exercise, there is no need what so ever.

from a learning point of view, yes its a great way to learn. but i feel that many people get confused and think higher level languages like c++ are not good for some reason.

if its already been done for you, use it. like the cmath libraries.

in my limited experience i have come across but one exception to that rule,

VECTORS!!

not only are they slow, but for more useful functionality you still have to write your own functions to get them to do anything worthwhile.

might aswell make a dynamic array class with linked lists.

hell, even a class with a simple array of pointers might be better than vectors.