Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   how short can you make this (http://www.programmingforums.org/showthread.php?t=10565)

angry_asian Jun 28th, 2006 9:32 PM

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:

Jimbo Jun 28th, 2006 9:37 PM

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.

Yarvin Jun 28th, 2006 9:37 PM

uhm... none that I can see.....

Ooble Jun 28th, 2006 9:42 PM

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.

angry_asian Jun 28th, 2006 9:49 PM

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?

Kilo Jun 28th, 2006 10:05 PM

(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;

snipertomcat Jun 28th, 2006 10:12 PM

ouch...

Infinite Recursion Jun 28th, 2006 10:30 PM

you could optimize a bit within the inline keyword...

:

inline int area(unsigned short int x, unsigned short int y)
{  return (x * y);  }


Prm753 Jun 28th, 2006 10:46 PM

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.

angry_asian Jun 28th, 2006 10:54 PM

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


All times are GMT -5. The time now is 8:05 AM.

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