![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
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;
} |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
uhm... none that I can see.....
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
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? |
|
|
|
|
|
#6 |
|
Expert Programmer
|
(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 |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
ouch...
__________________
if (u=an_asshole) then GOTO (hell) |
|
|
|
|
|
#8 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#9 |
|
Professional Programmer
|
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 |
|
|
|
|
|
#10 | |
|
Hobbyist Programmer
Join Date: Jun 2006
Location: at my computer desk
Posts: 138
Rep Power: 3
![]() |
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|