Anyone speak C++?

Nov 6, 2006 at 3:40 AM Thread Starter Post #1 of 8

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:

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]

 
Nov 6, 2006 at 3:55 AM Post #2 of 8
That's a nice way of graphing the function, with symbols
biggrin.gif
. What you want to do is to write a function parser, which is a non-trivial exercise (as you noted). You will need some knowledge of stacks and other constructions to best get the job done, as what you'll basically be doing is converting strings of text into something your program can understand. As for the actual plotting, look into a library called SDL, which allows you to easily plot points in a window, and it's really, really easy.

Writing a proper function plotter is a great long-term project, and you will learn many things, both in mathematics and in computer science.
 
Nov 6, 2006 at 4:27 AM Post #3 of 8
Just a tip. You can use the pound sign on the tool bar of the reply window to cut and paste code so that it includes your indents.

Code:

Code:
[left]I just typed this and put code brackets around it[/left]

 
Nov 6, 2006 at 4:32 AM Post #4 of 8
The task you have chosen for an intro to computer science class is extremely large. As mentioned by vagarach you would need to write a function parser capable of discerning every different type of operation you would support. Then use that information to decide what type of math to do in order to output a visible graph. You would also need to have information deciding what limits to have for the graph so that you can see the "useful" part of the graph since by definition a function is infinite.

The idea of parsers is usually taught in an upper level compilers class. A function parser of your needs probably won't be as deep as one you would write for a compiler parser but the ideas are the same. It's a fairly complex idea and writing a parser capable of reading numerous operations and mapping them to actual functions that can computer the appropriate on screen display.

My recommendation is to rethink the idea of having them input a function directly and look more into giving them a menu choice of operations they are capable of graphing. Like, sin(x), cos(x), x^2, x^3, ln(s), etc... That would definitely be more within the scope of an introductory class, and would still require input, output, and math required to correctly display the information.

I wish you luck in your schooling. Software Engineering/Development is a good gig. Rated the best job in america quite recently by CNN Money.
 
Nov 6, 2006 at 5:53 PM Post #5 of 8
Thanks, guys! So, I guess that confirms my fears. It is going to be a pain in the *** to create this interface. I'll post the updated code after I finish. There's also a lot of cleaning up I need to do. I can probably streamline those if, else if, and else statements a little (also taking care of that rogue asterisk on the far right of the output), and take care of that coersion, so it will work with all compilers, if all compilers have screens that are 80 characters across. I can also make that while statement a for statement. I didn't know how to use a for statement when I made this program a few weeks ago. Actually, I didn't know until yesterday
tongue.gif
.

I don't know if I want to make this a long term project, using a bunch of C++ features that I don't yet know about, but it could be fun. We'll see. I'm actually not a CSI major. I'm studying Math and Religion, and this might be my last compsci class, although I like it a lot, so I might take a few more.
 
Nov 6, 2006 at 6:27 PM Post #6 of 8
I started college to get a Math degree as well... ended up with a BS in Math, a BS in Computer Science, with a minor in Philosophy because the CS stuff came very naturally to me and I enjoyed a bunch of Philosophy classes.

Now I work as Software Engineer and enjoy it quite a bit. Very relaxed work environment... headphones are almost a necessity.
 
Nov 6, 2006 at 8:32 PM Post #7 of 8
You've got a variety of problems to solve here:

(1) Creating a data structure/class library/whatever to store equations in
(2) Finding a good way to traverse the structure while still following order of operations
(3) Parsing the actual input [and handling improper input]
(4) designing and writing a solid interface to the whole thing

You're talking an incredible effort here. If you stick with it, you'll probably be working on this for quite some time.

Now's as good a time as ever to realize that if you start a project by just cranking out code, you will have major headaches later on. Good planning in advance is the way to go.
 
Nov 6, 2006 at 9:44 PM Post #8 of 8
It must also be noted that there is quite a bit of theory behind what you want to do, both in computer science and mathematics (numerical methods come to mind...taylor series' and all that jazz
biggrin.gif
). Do you know, for example, how to construct a binary or n-ary tree that represents the string (2*(5+2))-((8/2)+2-4)? You'll be needing a solid CS textbook to work from. The one I used is the Walls and Mirrors one by Carrano & Pritchard, an introductory book aimed at those totally at ease with programming, and ready to learn the theory of data structures, recursion, etc.

Complete this, and you should be at 2nd/3rd year CS major level? Not to be discouraging, but non-trivial is the operative term here.

edit: Just drop the part where you let people enter the function. This will avoid 90% of your problems (and make it a realistic project). Like someone suggested above, let the user pick from a menu, and later extend it by allowing them to graph, say, sin(x+alpha), where alpha is number of their choice, or similar.
 

Users who are viewing this thread

Back
Top