PDA

View Full Version : I hate having to do this (help with C++ homework)


Letterbomber
02-24-2009, 09:11 PM
normally i would never come for help here, but ive worked for 8-9 hours on this and cant figure it out. google's no help either. what i have to do is this:



Write a class for Polynomial.

You class will contain the following functions:

A constructor
A destructor - you must free any allocated data.
A function to evaluate a polynomial: double Eval(double x)
An overload to the stream insertion operator to print a polynomial
An overload to the stream extraction operator to read a polynomial

A main program which will read in a polynomial of degree 10, print the
resulting polynomial, evaluate it and print the answer.

print polynomials in a format like this:

coef*x^exp + coef*x^exp ...

where coef is the coefficient of the ith term and exp is it's exponent.
if the coef is zero, don't print the term.

example:

10*x^5 + 5*x^4 - 7*x^2 + 5

note: if the coef is negative, print a minus, not a plus.

input format:

10,5 5,4 -7,2 5,0

where the first number is the coefficient and the second is the power of x to
which it applies.


so basically i have to print the polynomial in that format, and then print the result for a degree of x.

what i have so far:

my .h file:
class Polynomial;

class Term{
friend class Polynomial;
private:
float coef;
int exp;
};

class Polynomial{

public:
Polynomial();

Polynomial Add(Polynomial poly);

Polynomial Mult(Polynomial poly);

float Eval(float f);

double eval(double x);

private:
Term *termArray;
int capacity;
int terms;
}

my .cpp file:
#include iostream
#include math.h

#include "Polynomial1.h"

Polynomial(){
termArray= new Term[4];
capacity= 4;
terms= 0;
}

poly x;

cin>>x;
istream &operator>>(istream &in, poly x)
int a;
double b, c;
for(int i=0, i*less than sign*a, i++){
cin>>b>>c;
x.NewTerm(b,c);
}
return in;
}
void copy(Term *a, Term *b, Term *c){
while (a*less than sign*b) *(c++)= *(a++);
}

double Poly::eval(double x){
double ans= 0;
for(int i=0; i*less than sign*terms; i++){
ans= ans+termArray[i].coef*pow(x, termArray[i].exp)
}
return ans;
}


i think i got an idea on the class and how to print the result, but i'm completely lost when it comes to getting it print out in that format. any programmers out there that can help? i hate to seem lazy but i'm completely stuck and i dont know what else to do.

edit: almost forgot my errors:


Polynomial1.cpp:6: error: new types may not be defined in a return type

Polynomial1.cpp:6: note: (perhaps a semicolon is missing after the definition of ‘Polynomial’)

Polynomial1.cpp: In function ‘Polynomial Polynomial()’:

Polynomial1.cpp:7: error: ‘termArray’ was not declared in this scope

Polynomial1.cpp:8: error: ‘capacity’ was not declared in this scope

Polynomial1.cpp:9: error: ‘terms’ was not declared in this scope

Polynomial1.cpp: At global scope:

Polynomial1.cpp:12: error: ‘poly’ does not name a type

Polynomial1.cpp:14: error: expected constructor, destructor, or type conversion before ‘>>’ token

Polynomial1.cpp:15: error: expected constructor, destructor, or type conversion before ‘&’ token

Polynomial1.cpp:18: error: expected unqualified-id before ‘for’

Polynomial1.cpp:22: error: expected unqualified-id before ‘return’

Polynomial1.cpp:23: error: expected declaration before ‘}’ token

btw, i am a n00b when it comes to this stuff.

ryan
02-24-2009, 09:27 PM
i can't really help with the actual coding as i haven't done C in a few years and need to brush up, but how is your work process.

you said you're new to this. how do you go about coding? are you trying to just write it as you go? if so i recommend you flow chart it out. once you have a working coherent flowchart you can convert each flowchart bubble into its respective pseudo code and then from there do the actual coding.

it may sound like a lot of work but its not and it makes everything super easy. once you can visualize it, you can make it.

Letterbomber
02-24-2009, 09:30 PM
ok yeah i dont usually do that. i will attempt that and see how it goes. but that doesn't mean i won't take any help i can get from the rest of you!!

h-man
02-24-2009, 09:31 PM
once you can visualize it, you can make it.

if you asked about this a year ago i was pretty darn good at c++, but i haven't done any coding in a year.

what ryan is saying is on point though. flowchart. if you just don't know how to code, then you need to hit the books. usually you can find a good example in textbooks with regards to how you should organize your program to prototype off of.

some errors i'm noticing: functions need to be declared public/private. and don't you just need to use conio.h to print?

Adam
02-24-2009, 09:39 PM
God they are making this so much harder than it needs to be. Here's how I would do it.

1. Take the input and do a string split on the whitespace character, that should leave you with a string array that looks like this

arr[0] = 10,5
arr[1] = 5,4
arr[2] = -7,2
...
arr[n] = coef, exp

2. iterate through the array and create an STL map...the first value being the coef, and the second value being an exp, so your map looks like:

10->5
5->4
-7->2
...

3. iterate through your map, and simply append a string like so:

foreach iterator object {
string + (it*).key() + "^x^" + (it*).value() + "+"
}

4. then add in edge cases...so if its the last value of the iterator, you dont include the "+" at the end.

5. print the string to sysout

Place the STL map in your constructor. your destructor will most likely be empty because you arne't calling the opreator new, so don't really worry about that. And put all of the above in your eval method, and then in main, simply call "eval(string)" where string is your raw input from sysin.

Kobe Bryant
02-24-2009, 09:42 PM
BLEEP BLOOP BLEEP BLOOP 101010010001001001010010101000101001010100 BLIP BLOP 1010100010100010010 "You've got mail!"

Letterbomber
02-24-2009, 09:46 PM
God they are making this so much harder than it needs to be. Here's how I would do it.

1. Take the input and do a string split on the whitespace character, that should leave you with a string array that looks like this

arr[0] = 10,5
arr[1] = 5,4
arr[2] = -7,2
...
arr[n] = coef, exp

2. iterate through the array and create an STL map...the first value being the coef, and the second value being an exp, so your map looks like:

10->5
5->4
-7->2
...

3. iterate through your map, and simply append a string like so:

foreach iterator object {
string + (it*).key() + "^x^" + (it*).value() + "+"
}

4. then add in edge cases...so if its the last value of the iterator, you dont include the "+" at the end.

5. print the string to sysout

Place the STL map in your constructor. your destructor will most likely be empty because you arne't calling the opreator new, so don't really worry about that. And put all of the above in your eval method, and then in main, simply call "eval(string)" where string is your raw input from sysin.

i would do that (seems like an easier concept, but im a n00b so i hadn't thought of that), but the problem is he wants it to be done in that exact manner. shitty i know. i appreciate the help though.

Mr. Machine
02-24-2009, 09:49 PM
i would do that (seems like an easier concept, but im a n00b so i hadn't thought of that), but the problem is he wants it to be done in that exact manner. shitty i know. i appreciate the help though.

I wanted to come in and try to help, but I didn't know C++ was so different from C. :( I just got done with my MATLAB II class today, hooray!

Nickzed
02-24-2009, 09:51 PM
fuck, im coming to you guys for my java homework when i need help.

ryan
02-24-2009, 09:53 PM
oh god matlab is awful

we only have to use matlab in three classes just for the labs. shitty thing is we're not getting tested on it so why bother learning it. luckily he hasn't changed the lab in 3 years so i've been using my friend's who did it last year.


fuck, im coming to you guys for my java homework when i need help.

i find java much easier than C

just hope you never have to do assembly, god that language was a pain

Letterbomber
02-24-2009, 09:59 PM
^ i did assembly last semester. holy shit that is god awful. makes C/C++ look easy.

Mr. Machine
02-24-2009, 10:05 PM
oh god matlab is awful

we only have to use matlab in three classes just for the labs. shitty thing is we're not getting tested on it so why bother learning it. luckily he hasn't changed the lab in 3 years so i've been using my friend's who did it last year.


You really think so? I love MATLAB, especially compared to C.

^ i did assembly last semester. holy shit that is god awful. makes C/C++ look easy.

Word, son. Assembly is fucking ridiculous. I remember the last lab in my assembly class was to produce a cardiac output signal...brutal.

Nickzed
02-24-2009, 10:31 PM
oh god matlab is awful

we only have to use matlab in three classes just for the labs. shitty thing is we're not getting tested on it so why bother learning it. luckily he hasn't changed the lab in 3 years so i've been using my friend's who did it last year.




i find java much easier than C

just hope you never have to do assembly, god that language was a pain

oh yeah java is certainly easier than C, since C is basically strictly procedural and java is OO and well, it has a lot of shortcuts that you have to manually do in C.

Adam
02-24-2009, 10:58 PM
FYI: My method adheres to all of your constraints. Re-read it and you'll see that you can do it that way.

Kyle
02-24-2009, 11:02 PM
This is why I switched from a CS major to EE. FYL, dude.

theplot
02-24-2009, 11:50 PM
lol, my roommate is a EE. he studies 12 hours a day. i feel bad for him and im a chem e. FYL, dude.

although, i was in the computer lab today and this huge, fat, azn CS guy was pounding on the desk every 10-20 minutes because his code wasn't working. i left because i was scared someone would say something to him and he'd shoot up the room or something.

Mr. Machine
02-25-2009, 06:59 AM
Fuck EE. I hated that shit so much that I switch to biomedical engineering.

Kyle
02-25-2009, 07:11 AM
I'm not saying it's not difficult. I just fucking hate coding.

Mr. Machine
02-25-2009, 07:28 AM
I just fucking hate coding.

QFT.

Smot
02-25-2009, 11:42 AM
^ i did assembly last semester. holy shit that is god awful. makes C/C++ look easy.

When I was a CS Major I took assembler over the summer. WORST MISTAKE OF MY COLLEGE LIFE. Holy fuck, after I finished the class with a C I had to take Assembler II. That's where I drew the line and said CS NO MOAR!!

Seriously I don't have the right mind frame for programming. Spending hours and hours and hours for one assignment was painful and ridiculous. I have all the respect for people that finish that god awful major and like it.

B.S B.A. is where I am at now and love it.

ryan
02-25-2009, 11:55 AM
i didn't realize tehre were so many people here with comp sci/elec eng/comp eng majors and backgrounds. we're a lot nerdier than i thought

sosADAMsos
02-25-2009, 12:08 PM
Man, this stuff makes no sense to me. I hope I can keep it that way ;)

I'm very impressed with you guys though.