Pages: [1]
|
|
|
|
Author
|
Topic: C++ Hwk (Read 70 times)
|
|
Guardian_Tenshi
Global Moderator
Karma: +53/-26
Offline
Gender:
Posts: 1114
|
|
C++ Hwk
« on: March 25, 2004, 07:43:01 PM »
|
|
Hey guys, I was finishing up my C++ hwk...the assignment was to take our previously done operator overloading assignment, and add some dynamic memory allocation to it. It wasn't bad at all, but the last operator he had us do was operator==. I wrote it, and it worked my way, but I'm wondering if there was a better way to do it, as I had to "break" from a for loop, which isn't normally good...
The pgm is class is called "VectorN" where 'N' is the dynamically allocated part, the previous assignment was "Vector3D" so, essentially, N is the dimension, get it??
Ok...so basically, each element in the vector is an element in a dynamically allocated array. so ar = x, ar[1] = y, ar[2] = z, etc...so VectorN has a size and pointer to the array.
==compares the size, then the elements in left == right, and so on until you reach the end of the array. if they are of different size, they are not equal by default... enough rabling here is my code...
//Overloading operator== bool operator==(const VectorN &left, const VectorN &right) { bool flag = true; if ( left.size() != right.size() ) flag = false; else { for (int i=0; i<left.size(); i++) { if (left.ptr[i] == right.ptr[i]) { flag = true;} else { flag = false;}
if (flag == false) break; }//closes for loop. }//closes else return (flag); }//close function
|
|
is there a better way to write that, which gets around the break??
Tenshi
|
|
Logged
|
|
|
|
Fotty
Admin Team CSR Connoisseur
Karma: +35/-10
Offline
Gender:
Posts: 840
|
|
Re:C++ Hwk
« Reply #1 on: March 25, 2004, 08:50:11 PM »
|
|
there is nothing wrong with breaking from a for loop, thats what break is for... I did take out your check though by putting the break out of the loop in the else statement of the compairison...this saves the processor 1 whole boolean check
//Overloading operator== bool operator==(const VectorN &left, const VectorN &right) { bool flag = true; if ( left.size() != right.size() ) flag = false; else { for (int i=0; i<left.size(); i++) { if (left.ptr[i] == right.ptr[i]) { flag = true;} else { flag = false; ••• break; ••• }//closes else }//closes for loop. }//closes else return (flag); }//close function |
|
|
« Last Edit: March 25, 2004, 08:50:37 PM by Fotty » |
Logged
|
|
|
|
Terraji
Admin Team CSR Connoisseur
Karma: +35/-15
Offline
Gender:
Posts: 789
|
|
Re:C++ Hwk
« Reply #2 on: March 25, 2004, 09:20:45 PM »
|
|
In my opinion, I see nothing wrong with having a break there, but I am sure I have a style guide lying around somewhere that says otherwise. I usually don't pay much attention to little niggly things like that (they usually don't care anyway, especially in comp sci). As long as it has good modular design and it is readable it is good enough for me.
One thing that I would do if you don't want to use a break is put a 'return false;' the second you know that they aren't equal. I find that it safeguards from stepping on unwanted instructions on the way down that you might add later.
|
|
Logged
|
|
|
|
Terraji
Admin Team CSR Connoisseur
Karma: +35/-15
Offline
Gender:
Posts: 789
|
|
Re:C++ Hwk
« Reply #3 on: March 25, 2004, 09:34:52 PM »
|
|
As an alternative, you could go all Computer Science on it's ass and do a recursive solution for extra slickness.
|
|
Logged
|
|
|
|
Porter
[Wumpa]
Board Admin
Karma: +176/--88
Offline
Gender:
Posts: 3910
|
|
Re:C++ Hwk
« Reply #4 on: March 25, 2004, 09:45:56 PM »
|
|
You don't need the break at all, you can use the conditional check of the for loop itself:
//Overloading operator== bool operator==(const VectorN &left, const VectorN &right) { bool flag = true; if ( left.size() != right.size() ) flag = false; else { for (int i=0; i<left.size() && flag; i++) { if (left.ptr[i] == right.ptr[i]) { flag = true;} else { flag = false;} }//closes for loop. }//closes else return (flag); }//close function |
|
|
|
Logged
|
[Wumpa] Porter --Silent, professional, lethal... sometimes.
|
|
|
Guardian_Tenshi
Global Moderator
Karma: +53/-26
Offline
Gender:
Posts: 1114
|
|
Re:C++ Hwk
« Reply #5 on: March 25, 2004, 10:15:10 PM »
|
|
You don't need the break at all, you can use the conditional check of the for loop itself:
//Overloading operator== bool operator==(const VectorN &left, const VectorN &right) { bool flag = true; if ( left.size() != right.size() ) flag = false; else { for (int i=0; i<left.size() && flag; i++) { if (left.ptr[i] == right.ptr[i]) { flag = true;} else { flag = false;} }//closes for loop. }//closes else return (flag); }//close function |
|
|
|
ok, that's what i thought too porter, but then what if the second element isn't equal, but the third is?? e.g. <1,2,3> and <1,4,3>...it'll get to the 3 again, and reset the flag to true won't it??
Tenshi
|
|
Logged
|
|
|
|
Porter
[Wumpa]
Board Admin
Karma: +176/--88
Offline
Gender:
Posts: 3910
|
|
Re:C++ Hwk
« Reply #6 on: March 26, 2004, 07:52:31 AM »
|
|
No, because on the second pass through the loop, flag will get set equal to false. When the condition "i<left.size() && flag" is checked again before the start of the third pass, i less than left.size() is true, but flag is false, and so the for() statement stops right there.
|
|
Logged
|
[Wumpa] Porter --Silent, professional, lethal... sometimes.
|
|
|
Terraji
Admin Team CSR Connoisseur
Karma: +35/-15
Offline
Gender:
Posts: 789
|
|
Re:C++ Hwk
« Reply #7 on: March 26, 2004, 09:21:30 AM »
|
|
Ok, Porter wins. But purely for geek points, I did it recursively
no guarantees that it works, since I didn't test it.
bool operator==(const VectorN &left, const VectorN &right) { return recursive_compare(left,right,-1); }
bool recursive_compare(const VectorN &left, const VectorN &right, int i) { if (i <= -1) { if (left.size() == right.size()) { return recursive_compare(left,right,0); } else { return false; } }
if( (i == left.size()) ) { return true; } else if(left.ptr[i] == right.ptr[i]) { return recursive_compare(left,right,i+1); } else { return false; }
}
|
|
|
« Last Edit: March 26, 2004, 09:23:01 AM by Terraji » |
Logged
|
|
|
|
Guardian_Tenshi
Global Moderator
Karma: +53/-26
Offline
Gender:
Posts: 1114
|
|
Re:C++ Hwk
« Reply #8 on: March 27, 2004, 03:35:01 PM »
|
|
No, because on the second pass through the loop, flag will get set equal to false. When the condition "i<left.size() && flag" is checked again before the start of the third pass, i less than left.size() is true, but flag is false, and so the for() statement stops right there.
|
|
thank you porter, i missed that you added that into the conditional part of the for-loop. I knew there was a better way, but just couldn't think of it.
As for terraji, I really haven't had much exp in recursion, and frankly, I try to avoid it whenever possible, it in general seems to make more processor and function calls then you ever want, and makes things less logical... Ok, well sometimes the logical part is better with recursion, but usually not. I dunno, I've just always like logical code better then efficient code i guess.
Tenshi
|
|
Logged
|
|
|
|
Porter
[Wumpa]
Board Admin
Karma: +176/--88
Offline
Gender:
Posts: 3910
|
|
Re:C++ Hwk
« Reply #9 on: March 27, 2004, 04:15:26 PM »
|
|
I dunno, I've just always like logical code better then efficient code i guess. |
|
Wait till you have to start dealing with multiple NP-Complete problems: you'll like that efficient code a lot better. Recursion comes pretty naturally to me-- it makes more sense and can clean your code up a ton. It's not necessarily any slower than iteration, but it might be more memory intensive given the implementation.
The interpreter I wrote last term used a recursive tree-walk function to "run" an abstract syntax tree-- that was TONS easier to understand and code than an iterative solution would have been.
|
|
Logged
|
[Wumpa] Porter --Silent, professional, lethal... sometimes.
|
|
|
Terraji
Admin Team CSR Connoisseur
Karma: +35/-15
Offline
Gender:
Posts: 789
|
|
Re:C++ Hwk
« Reply #10 on: March 27, 2004, 06:59:14 PM »
|
|
I had to code up a recursive function in assembler once, it makes alot more sense once you actually have to deal with stack frames linearlly sequenced in memory. When you look at it that way, it is actually a lot more natural way for computers to do things.
Recursion becomes pretty darn easy once you wrap your head around it. Like Porter said, tree iterations are a breeze, as well as recursive sorting algorithms.
|
|
Logged
|
|
|
|
Porter
[Wumpa]
Board Admin
Karma: +176/--88
Offline
Gender:
Posts: 3910
|
|
Re:C++ Hwk
« Reply #11 on: March 28, 2004, 12:44:00 PM »
|
|
**cough**quicksort**coughcough**
|
|
Logged
|
[Wumpa] Porter --Silent, professional, lethal... sometimes.
|
|
|
Pages: [1]
|
|
|
|
|
|
CSReloaded Forums | Powered by YaBB SE
© 2001-2003, YaBB SE Dev Team. All Rights Reserved. |
|
|