Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2006, 8:32 PM   #1
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
how short can you make this

what things can one shorten in this area program
#include <iostream.h>
int area(unsigned short int x, unsigned short int y)
{
	return (x * y);
}

int main()
{
unsigned short int x, y, z;
cout << "Enter width and height (with a space in between):";
cin >> x;
cin >> y;
z=area(x,y);
cout << "Area=" << z << "\n";
return 0;
}
i've already shortend it from what the book had :banana:
angry_asian is offline   Reply With Quote
Old Jun 28th, 2006, 8:37 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
newlines... yep, they've got to go... Seriously though, shorter code is not necessarily better code. Often it's the opposite. Obfuscated code is only good for confusing people (hence the name).

By the way, you're using a deprecated header. Use #include<iostream> and look up namespace std.
Jimbo is offline   Reply With Quote
Old Jun 28th, 2006, 8:37 PM   #3
Yarvin
Programmer
 
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0 Yarvin is on a distinguished road
uhm... none that I can see.....
Yarvin is offline   Reply With Quote
Old Jun 28th, 2006, 8:42 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
There are a couple of ways to shorten this without making it any less readable:
#include <iostream>

int main()
{
    unsigned int x, y;
    std::cout << "Enter width and height (with a space in between):";
    std::cin >> x >> y;
    std::cout << "Area = " << x * y << "\n";
    
    return 0;
}

Seriously though, I don't know why you'd bother. DaWei has a link to an excellent article on optimisation and how to determine whether it's necessary - if I wasn't so sleepy, I'd point you to it. I think it's in his signature. More important would be error-checking.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jun 28th, 2006, 8:49 PM   #5
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
how do i look up namespace std...?
instead of
#include<iostream.h>
is it
#include<iostream>
using namespace std;
just like that or what exactly (with right punctuation and spelling) is it?
angry_asian is offline   Reply With Quote
Old Jun 28th, 2006, 9:05 PM   #6
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
(without offense): [b]how do i look up namespace std...?[b] Aren't you supposed to be some super asain? genius? I mean I have asked some pretty (IMO) stupid questions but you just blew me out of the water!

using the following is stating that the entire program is using the standard namespace.
using namespace std;

without using the above use the following to state the namespace in a single instance
std::cout << "(IMO) this one is better" << std::endl;
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Jun 28th, 2006, 9:12 PM   #7
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
ouch...
__________________
if (u=an_asshole) then GOTO (hell)
snipertomcat is offline   Reply With Quote
Old Jun 28th, 2006, 9:30 PM   #8
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
you could optimize a bit within the inline keyword...

inline int area(unsigned short int x, unsigned short int y)
{  return (x * y);  }
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jun 28th, 2006, 9:46 PM   #9
Prm753
Professional Programmer
 
Prm753's Avatar
 
Join Date: Oct 2005
Location: United States
Posts: 447
Rep Power: 4 Prm753 is on a distinguished road
Send a message via AIM to Prm753 Send a message via MSN to Prm753
Kilo, why did you make a reference to him being Asian? That has nothing to do with the OP's question.

I think Ooble about shortened it up with it still being readable. Of course, you could still do this:
#include <iostream>
int main() { int x, y; std::cout << "Enter x and y values (with space): "; std::cin >> x >> y; std::cout << "Area: " << x * y << std::endl; return 0; }

That would be an awful way to put it, though.
__________________
The world's first athletic computer geek!
The home of PrProgramsStudios
How not to post a question: <-- Please don't reply
Prm753 is offline   Reply With Quote
Old Jun 28th, 2006, 9:54 PM   #10
angry_asian
Hobbyist Programmer
 
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3 angry_asian is on a distinguished road
Quote:
Originally Posted by Kilo
(without offense): [b]how do i look up namespace std...?[b] Aren't you supposed to be some super asain? genius? I mean I have asked some pretty (IMO) stupid questions but you just blew me out of the water!

using the following is stating that the entire program is using the standard namespace.
using namespace std;

without using the above use the following to state the namespace in a single instance
std::cout << "(IMO) this one is better" << std::endl;
well i did think that if u read my second post fully i said "i think it is "using namespace std;"" so i was just making sure
but still, thanks especially for the second way fo defining without doing it at the beginningm thats what ooble did and it confused me but now i get it
ps- i am a genius, just not at programming
angry_asian is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:04 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC