Maximum PC

 It is currently Mon Mar 17, 2014 10:19 am

 All times are UTC - 8 hours

 Page 3 of 3 [ 46 posts ] Go to page Previous  1, 2, 3
 Print view Previous topic | Next topic
Author Message
 Post subject: Posted: Wed Jul 30, 2008 5:02 pm
 Million Club - 5 Plus*

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
hbgrg wrote:
Here it is:

Code:
{
stack hex;
int remainder;
while (number > 0)
{
remainder = number % 16;
number = number/16;
if (remainder > 9)
hex.push('A'  + remainder - 10);
else
hex.push(remainder);
}
hex.pop();
};

Ah, DOH! Why did I miss this one earlier?

casting an int to a char starts it at ASCII \0, or NULL. Adding '0' to the else remainder works
Code:
else {
hex.push('0' + remainder);
}

BTW, isEmpty is broke with an assignment in the if statement (single equals should be double)

Top

 Post subject: Posted: Wed Jul 30, 2008 5:08 pm
 Team Member Top 500

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
hahah YAYYY i actually fixed everything (including the isEmpty) before I saw this post :D:D:D I'm finally doneee

Thanks EVERYONE who helped!!

Top

 Post subject: Posted: Wed Jul 30, 2008 6:01 pm
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Great work!

Now, if you really want to learn .. step back and tell us what you'd do differently if you had to start the project again from scratch.

Top

 Post subject: Posted: Wed Jul 30, 2008 9:28 pm
 Team Member Top 500

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
ummm I'd prolly connect all three functions in a way because they are very similar (use modulus and divide operators)... And I'd use the stack class for octal too... that sound good?

Top

 Post subject: Posted: Thu Jul 31, 2008 6:50 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Yep.

In each of the conversion functions, you did the same thing. You just did it with different bases. So, instead of having binary(int), hex(int) and octal(int), you could have a function called convert that takes a starting base, an end base and a number and converts accordingly.

Code:
int convert(int baseStart, int baseEnd, int number)

Of course, if you want to make this possible with return base greater than 10, you would need a return type that includes characters. A character string or an array, for instance.

Also, you could definitely use the stack for each of the functions in your original code or in the 'new' code.

This makes your code reusable because it can convert from any base to any other. It also makes it easier to read, easier to maintain and it looks nicer. It is not necessarily more efficient, though. I won't get into efficiency here, but I will point out that efficiency has nothing to do with the number of lines of code you have in your source .. a common misconception among programmers until they get bitchslapped by the higher maths and learn algorithmic proofs of efficiency and correctness. That is for another day.

Damn it, I should be working here. Hope we've helped. Feel free to post your next assignment so we can help you think it through. I think we did a good job of leading you without handing you the answers or losing you along the way. Yes?

Top

 Post subject: Posted: Thu Jul 31, 2008 9:10 pm
 Team Member Top 500

Joined: Sun Apr 02, 2006 7:32 pm
Posts: 1323
Yes thank you all!!! I learned a lot from you guys...

(Not so) sadly, my class is over so I dont think I'll be coming back with anymore assignments but I think I'm going to take flash sometime so I might comeback with questions about that

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 3 of 3 [ 46 posts ] Go to page Previous  1, 2, 3

 All times are UTC - 8 hours

Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets