Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: ztrumpet on May 17, 2010, 04:08:13 pm

Title: My schoolwork got rejected...
Post by: ztrumpet on May 17, 2010, 04:08:13 pm
My schoolwork got rejected because it was too complex for my teacher to grade.  I think my Axe and Asm knowledge rubbed off, making my C++ a lot more optimized.  My teacher took one look at it, shook his head, and told me that he wouldn't grade anything that complex.  For those who know C++, you make the call.

My awesome version:
Code: [Select]
#include<fstream.h>
#include<iostream.h>

void main()
{
char string[25], header[50] = "Name     \0Address  \0City     \0State    \0ZIP Code ";
ifstream file_ptr;

int offset = 0;
file_ptr.open("ADDRFILE.DAT",ios::in);
if (file_ptr)
{
do
{
file_ptr.get(string, 25);
file_ptr.ignore(80, '\n');
if (!file_ptr.eof())
cout << header + 10 * offset << ':' << string << '\n';
offset = (offset + 1) % 5;
}
while (!file_ptr.eof());


file_ptr.close();
}
else
cout << "An Error occured!\n";
}

The version I had to turn in:
Code: [Select]
#include<fstream.h>
#include<iostream.h>

void main()
{
char name[25], address[25], city[25], state[25], zip[25];
ifstream file_ptr;

file_ptr.open("ADDRFILE.DAT",ios::in);
if (file_ptr)
{
do
{
file_ptr.get(name, 25);
file_ptr.ignore(80, '\n');
file_ptr.get(address, 25);
file_ptr.ignore(80, '\n');
file_ptr.get(city, 25);
file_ptr.ignore(80, '\n');
file_ptr.get(state, 25);
file_ptr.ignore(80, '\n');
file_ptr.get(zip, 25);
file_ptr.ignore(80, '\n');
if (!file_ptr.eof())
{
cout << "Name: " << name << '\n';
cout << "Address: " << address << '\n';
cout << "City: " << city << '\n';
cout << "State: " << state << '\n';
cout << "ZIP Code: " << zip << '\n';
}
}
while (!file_ptr.eof());


file_ptr.close();
}
else
cout << "An Error occured!\n";
}

Is there a version that's even more optimized?  Feel free to rewrite it if you want... ;D
Title: Re: My schoolwork got rejected...
Post by: Galandros on May 17, 2010, 04:52:38 pm
Interesting. Assembly power, ftw.

The first code looks much better (smaller, less repetitive, flexible to changes) to me even if I don't know what those C++ functions are doing exactly.
Title: Re: My schoolwork got rejected...
Post by: Silver Shadow on May 17, 2010, 05:21:37 pm
Sometimes teachers are really stupid...
Title: Re: My schoolwork got rejected...
Post by: meishe91 on May 17, 2010, 05:44:24 pm
Wow, that is really annoying when teachers do that :( Sorry you had to downgrade your project.
Title: Re: My schoolwork got rejected...
Post by: ztrumpet on May 17, 2010, 05:45:41 pm
Sorry you had to downgrade your project.
Nah, it's fine, as it only took me 10 more minutes. ;D
Title: Re: My schoolwork got rejected...
Post by: calcdude84se on May 17, 2010, 05:53:01 pm
Sigh... If the teacher can't understand something you can, he ought to learn how your code works...
Title: Re: My schoolwork got rejected...
Post by: meishe91 on May 17, 2010, 05:55:22 pm
Sigh... If the teacher can't understand something you can, he ought to learn how your code works...

I agree with that. Or that you should teach the class for the day and get the money he would've made :P

Haha well its still harder to think in unoptimized thoughts once you are good at not doing it :P
Title: Re: My schoolwork got rejected...
Post by: Eeems on May 17, 2010, 06:19:21 pm
he should have given you full marks if it surpassed his level...after all that is then past the level of the course isn't it?
Title: Re: My schoolwork got rejected...
Post by: Builderboy on May 17, 2010, 07:00:51 pm
Lol, that is both a win on your part and a fail on his part :P he should have given you full marks, or maybe *tried* to decipher it! ;D
Title: Re: My schoolwork got rejected...
Post by: simplethinker on May 17, 2010, 09:09:36 pm
lol epic fail on your teacher's part :D

The disturbing thing is... what part was too complex for him?  The only real differences are the two lines
Code: [Select]
cout << header + 10 * offset << ':' << string << '\n';
offset = (offset + 1) % 5;
The first is fairly straight forward (display "{something}: {what you retrieved}"), and the second is just making "offset" go 0-1-2-3-4-0-1-2-3-4...

[edit] Now that I think about it, for my mathematical software survey ("SoftSurv" for short :)) final project my code had a lot of tricks like that.  I wonder if that's why I got an A in the class (my code was completel crap lol)
Title: Re: My schoolwork got rejected...
Post by: Quigibo on May 17, 2010, 09:27:32 pm
That code isn't hard to follow at all, and its not very complex.  But I have a feeling what your teacher is complaining about is that you made it very "C style" instead of "C++ style".  They want to train you to stick with the C++ commands and not do those efficient workarounds becasue eventually you're going to be dealing with abstract objects and your code will no longer be portable when instead of strings you have classes and instead of characters you have child objects.
Title: Re: My schoolwork got rejected...
Post by: ztrumpet on May 17, 2010, 10:06:43 pm
That code isn't hard to follow at all, and its not very complex.  But I have a feeling what your teacher is complaining about is that you made it very "C style" instead of "C++ style".  They want to train you to stick with the C++ commands and not do those efficient workarounds becasue eventually you're going to be dealing with abstract objects and your code will no longer be portable when instead of strings you have classes and instead of characters you have child objects.
Okay, that's probably why he did it.  He likes to have us program certain exercises in a certain way because it'll help us later on.  Thanks for the info! :D
Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 17, 2010, 10:49:17 pm
Wow that was retarded from the teacher, but again maybe Quibigo has a point. Still, I would hate if I worked hard on a school project and it got rejected because it surpasses the teacher requirements way too much.

In my case, the worst I had happen is that the teacher and other students couldn't help me fixing bugs in my last project before graduation (The Reign of Legends 0). It was too advanced for him so I was pretty much on my own x.x
Title: Re: My schoolwork got rejected...
Post by: Geekboy1011 on May 17, 2010, 11:16:16 pm
nice ztrumpet thats a;ways interesting to see a student do better than a teacher
and my teach is the same way i can do ti this way she wants it that way oh well xD
Title: Re: My schoolwork got rejected...
Post by: TIfanx1999 on May 18, 2010, 08:28:03 am
I don't have much experience with C++, but Quigbo does raise a very good point. Nice insight there! You should also remember that many teachers just know how to grade things based on how the course is laid out, IE they only have knowledge of/ teach what is outlined in the course, and may not have (much) experience on the subject outside of that, especially on the high-school level. It's unfortunate, but that is the way our public education system is in the U.S.
Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 18, 2010, 12:01:41 pm
It reminds me my last school project how I did not include a highscore system. I was making a adventure type game and they wanted us to put a highscore system in our game. WTF? I just decided to ditch it. It costed me 20 pts out of 100. However I didn't care because I was still the best person in the class in average for the entire year and it did not affect my overall school classes average enough. I got 80% on the game, btw
Title: Re: My schoolwork got rejected...
Post by: mapar007 on May 18, 2010, 01:42:06 pm
OMFG if he doesn't understand it, he should either give you an A, or voluntarily resign. (you see, this is one of the reasons why I never wanted to take comp classes at school)
Title: Re: My schoolwork got rejected...
Post by: ztrumpet on May 18, 2010, 04:29:31 pm
I think I should point out before my teacher gets bashed too bad that he was the teacher that taught me TI-Basic... ;D
Title: Re: My schoolwork got rejected...
Post by: Tribal on May 18, 2010, 04:35:00 pm
Oh wow, I can't believe a teacher actually rejected a work that WORKS and is more OPTIMIZED, if it was to complex for him he should have just given you an A+. Next time he asks you to rewrite the program ask him if he'd feel better if you teached the class :P
Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 18, 2010, 04:37:31 pm
Lol XD
Title: Re: My schoolwork got rejected...
Post by: meishe91 on May 18, 2010, 09:02:25 pm
If he does it again just turn in one of your calc games to him instead since he apparently knows TI-BASIC :P
Title: Re: My schoolwork got rejected...
Post by: ztrumpet on May 18, 2010, 09:29:09 pm
If he does it again just turn in one of your calc games to him instead since he apparently knows TI-BASIC :P
I showed him Exodus a couple of weeks ago.  I got the same response. ;D
Text sprites FTW! ;D
Title: Re: My schoolwork got rejected...
Post by: meishe91 on May 18, 2010, 09:29:45 pm
Haha very nice.
Title: Re: My schoolwork got rejected...
Post by: Halifax on May 18, 2010, 11:27:50 pm
(http://www.dahmus.org/blogimg/fry-see-what-you-did-there.jpg)
What Quigibo wrote is very true. You're talking to a self-taught programmer that went through the trials and tribulations of learning everything, that he already knew, in AP Computer Science. As you grow as a programmer you will come to realize the reasons why they teach the way they do.

At any rate, at face value those two pieces of code appear to do the same thing correct? But have you compiled it on every compiler new and old? This is an issue faced by lots of companies in the real world that are supporting legacy code that may need to be built with older compilers, etc. Believe it or not there is a compromise! You can keep your optimized version while simultaneously making your code easier to understand and more portable. Have the compiler do the pointer arithmetic for you:
Code: [Select]
#include <fstream>
#include <iostream>

void main()
{
char string[25];
char header[5][10] = {
{"Name"},
{"Address"},
{"City"},
{"State"},
{"ZIP Code"}
};

int offset = 0;
file_ptr.open("ADDRFILE.DAT", ios::in);
if (file_ptr)
{
do
{
file_ptr.get(string, 25);
file_ptr.ignore(80, '\n');
if (!file_ptr.eof())
cout << header[offset % 5] << ":" << string << "\n";
++ offset;
}
...etc
So I guess the main point here is, don't over-complicate things, and remember that portability is an issue. Other than that, I would like to say good job on taking the incentive to go above and beyond the accepted normalities; keep the drive going!
Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 18, 2010, 11:31:58 pm
ohai Halifax

I think portability is a good point to raise too. I do not know C but in the past I learned about programming some stuff and programmign teachers kept reminding us to make sure our code is portable (both by being useable in multiple apps without modifying too much and if possible cross-platform)
Title: Re: My schoolwork got rejected...
Post by: necro on May 19, 2010, 01:11:04 am
I'm going to agree with Halifax, In a programming job, readability, maintainability, and portability are likely to be more important than speed most of the time because other people will have to read and edit your code and very few apps (mainly those with sound or graphics) will max out a processor, so the trade off probably isn't worth it. Imagine a project of 6k lines of this and the migraine it would cause, even if its faster. Also, comments are always a good idea whenever clever code is employed.

What kind of class is it?
Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 19, 2010, 01:40:18 am
That said, I think if speed is really a big concern, code should be as optimized as possible, though. Otherwise, we end up with stuff like Halo 2 PC and other games that takes too much ressources for what they do.
Title: Re: My schoolwork got rejected...
Post by: Halifax on May 19, 2010, 11:13:32 am
Haha, hey DJ_Omnimaga. :D Yeah, it's been a while since I've gotten time to get back into calculators.
Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 19, 2010, 11:41:11 am
Sorry to hear D: sometimes life can get pretty hectic when it comes to free time x.x
Title: Re: My schoolwork got rejected...
Post by: ztrumpet on May 19, 2010, 04:59:54 pm
Imagine a project of 6k lines of this and the migraine it would cause, even if its faster. Also, comments are always a good idea whenever clever code is employed.
I can imagine; I just printed out the source to The Penguin 77's Tetris Marathon (64 pages) and Quigibo's Puyopuyo (49 pages) in an attempt to learn Asm. ;D  It's pretty cryptic, but it's fast. :D  (*ZTrumpet is just weird... ;D )
Despite the comment I just made, I completely agree with you, necro. :)  I should have probably commented and coded better earlier. :)

Code: [Select]
#include <fstream>
#include <iostream>

void main()
{
char string[25];
char header[5][10] = {
{"Name"},
{"Address"},
{"City"},
{"State"},
{"ZIP Code"}
};

int offset = 0;
file_ptr.open("ADDRFILE.DAT", ios::in);
if (file_ptr)
{
do
{
file_ptr.get(string, 25);
file_ptr.ignore(80, '\n');
if (!file_ptr.eof())
cout << header[offset % 5] << ":" << string << "\n";
++ offset;
}
...etc
So I guess the main point here is, don't over-complicate things, and remember that portability is an issue. Other than that, I would like to say good job on taking the incentive to go above and beyond the accepted normalities; keep the drive going!
Halifax, that's pretty great code!  Thanks for sharing it, and I think it better than my version.  Awesome job! ;D

What kind of class is it?
It's an introduction to C++ that I'm taking.  As you can tell, we are learning about data files right now. :)
Title: Re: My schoolwork got rejected...
Post by: Quigibo on May 19, 2010, 11:36:30 pm
Imagine a project of 6k lines of this and the migraine it would cause, even if its faster. Also, comments are always a good idea whenever clever code is employed.
I can imagine; I just printed out the source to The Penguin 77's Tetris Marathon (64 pages) and Quigibo's Puyopuyo (49 pages) in an attempt to learn Asm.

You printed 113 pages of asm code!  :o That's dedication!

Readability is really important, er... at least it is for normal sized projects, obviously not for simple assignments.  Often you finish one part of the code then start working on another only to find later you have to change that original section of code, only now you forgot what you did!  Its necessary to leave clues about what everything does becasue even though you wrote everything, its very very easy to forget.  Especially in assembly you have to keep track of more than just what it does, you also have to know if things need to be called or jumped to, what registers are needed for input and output, which registers get destroyed and which ones are preserved, the state of the stack before and after, etc.

Axe right now is 6770 lines of code for instance, which is over 100 pages if printed.  One way I keep it organized is by overusing macros.  You should see some sections of code are entirely macros to make it more readable.  I'll attach a screen shot to show you what I mean.  You wouldn't even be able to tell that its asm:

Title: Re: My schoolwork got rejected...
Post by: DJ Omnimaga on May 19, 2010, 11:40:36 pm
wow by version 1.0 it will be OVER 9000
Title: Re: My schoolwork got rejected...
Post by: Galandros on May 20, 2010, 09:00:06 am
Macros are really being used there.

I think every z80 assembly programmer should print this z80 instruction set by calcmaniac84:
http://www.ticalc.org/archives/files/fileinfo/427/42722.html

I printed in two pages and is without doubt valuable.
Title: Re: My schoolwork got rejected...
Post by: ztrumpet on May 20, 2010, 05:07:25 pm
Axe right now is 6770 lines of code for instance, which is over 100 pages if printed.  One way I keep it organized is by overusing macros.  You should see some sections of code are entirely macros to make it more readable.  I'll attach a screen shot to show you what I mean.  You wouldn't even be able to tell that its asm:
Wow, that's awesome!  It doesn't even look like asm. :D

Imagine a project of 6k lines of this and the migraine it would cause, even if its faster. Also, comments are always a good idea whenever clever code is employed.
I can imagine; I just printed out the source to The Penguin 77's Tetris Marathon (64 pages) and Quigibo's Puyopuyo (49 pages) in an attempt to learn Asm.
You printed 113 pages of asm code!  :o That's dedication!
Thanks. :D  I've also printed out Fishy by Iambian and Orbit by The Penguin 77.  Oh, and a little tutorial called Asm in 28 days. ;D  Asm in 28 has its own dedicated 3 inch binder. ;D
Title: Re: My schoolwork got rejected...
Post by: Geekboy1011 on May 21, 2010, 12:46:24 am
nice ztrumpet

i think i printed out ti's official app making guide or somthing like that
with 4 pages per sheet of paper it was still like 200 pages long X.x