steaxauce
1000+ Head-Fier
- Joined
- May 23, 2006
- Posts
- 1,150
- Likes
- 17
I'm currently taking intro to computer science at Baylor U, and I've created a source code with my very limited knowledge. The purpose of the program is to graph a function. This program will work only if your compiler's screen is 80 characters across.
My problem is creating an interface by which the user can input the function to be graphed. I can think of at least one way--having the user input the function into an array of characters and then using the value and location of those characters to determine what operations need to be performed on the variable x to apply the specified function to x--but that would be very difficult and I'd rather not do it if there's another way.
Also, feel free to clean up my code; that is, if you can even tell what it does! I didn't comment when I wrote it, but feel free to ask me any questions about how it works. It's not very pretty--I actually had to coerce data types at one point (although that could probably be fixed with a little work)--but it does what it's intended to do. Please, let me know if there's a better way of doing something that I haven't yet learned about.
Right now, this program graphs the function y=x*sin(x), but it will graph any function you replace it with. Copy and paste the code to try it out.
I used Microsoft Visual C++.
I appreciate your help!
Code:
My problem is creating an interface by which the user can input the function to be graphed. I can think of at least one way--having the user input the function into an array of characters and then using the value and location of those characters to determine what operations need to be performed on the variable x to apply the specified function to x--but that would be very difficult and I'd rather not do it if there's another way.
Also, feel free to clean up my code; that is, if you can even tell what it does! I didn't comment when I wrote it, but feel free to ask me any questions about how it works. It's not very pretty--I actually had to coerce data types at one point (although that could probably be fixed with a little work)--but it does what it's intended to do. Please, let me know if there's a better way of doing something that I haven't yet learned about.
Right now, this program graphs the function y=x*sin(x), but it will graph any function you replace it with. Copy and paste the code to try it out.
I used Microsoft Visual C++.
I appreciate your help!
Code:
Code:
[left]// Graphs a function #include<iostream> #include<cmath> using namespace std; int main() { int count=1; double x; double y; while( count <= 6480.0 ) { static_cast<double>(count); x= (fmod(count, 80.0) - 40.0)/4.0; //function to be graphed below y= x*sin(x); //function to be graphed above if(-1.0*(count-fmod(count, 80.0))/320.0 + 10.0 - (y) <= 0.125 && -1.0*(count-fmod(count, 80.0))/320.0 + 10.0 - (y) > -.125) { cout << "*"; } else if( fmod(count, 40) == 0 ) { if( fmod(count, 80) == 0 ) cout << " "; else if(((abs(3240-count))%320) == 0) { cout << abs((3240-count)/320)%10; } else { cout << "|"; }; } else if( abs( 3240 - count ) <= 40 ) { if ( (abs(3240-count)%4) == 0 ) { cout << (abs(3240-count))/4; } else { cout << "-"; } } else { cout << " "; }; count++; } cout << "Cool, now can you graph the fuction I asked you to?" << endl; return 0; }[/left]