Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Author Message
[Gandalf]

Posted: Thu Nov 24, 2005 1:18 am   Post subject: (No subject)

 c++: #include using namespace std; int main() {    int a = 4, b = 5;    cout << a << b << endl;    cout << b << a << endl;    return 0;    cout << a << b << endl; }

Creative solution, no? And in only one extra line.

wtd

Posted: Thu Nov 24, 2005 1:28 am   Post subject: (No subject)

Ah, but you've changed the order of two of the lines.

Yes, you could have two return statements in there, but I'd consider that bad form. The ideal solution will involve no such hacks.

In fact, the final solution shouldn't be "hackish" in the slightest.
[Gandalf]

Posted: Thu Nov 24, 2005 1:33 am   Post subject: (No subject)

Ah, but you didn't mention that it had to follow good form .

Well, for that I'll have to look into it a little bit more...
wtd

Posted: Thu Nov 24, 2005 1:35 am   Post subject: (No subject)

Shame there's no easy way to swap the values in a and b.
Martin

Posted: Thu Nov 24, 2005 2:21 am   Post subject: (No subject)

 c++: #include using namespace std; int main() {    int a = 4, b = 5;    cout << a << b << endl;    a = a + b - (b = a); // 4 + 5 - (4)    cout << a << b << endl;    return 0; }

In half of two lines. Boo yeah!
md

Posted: Thu Nov 24, 2005 2:25 am   Post subject: (No subject)

Martin wrote:
 c++: #include using namespace std; int main() {    int a = 4, b = 5;    cout << a << b << endl;    a = a + b - (b = a); // 4 + 5 - (4)    cout << a << b << endl;    return 0; }

In half of two lines. Boo yeah!

I think you need brackets around (a+b), otherwise the second brackets get evaluated first and then b == a == 4, so a + b = 8 - 4 = 4
Martin

Posted: Thu Nov 24, 2005 2:30 am   Post subject: (No subject)

Worked in Java. I don't have a C++ compiler at work.
md

Posted: Thu Nov 24, 2005 2:32 am   Post subject: (No subject)

Hmmm... could it be that the evaluation doesn't follow normal bedmas rules? In that case it would certainly work... most interesting... Oh well, I'll give it a shot tomorrow once I get around to checking out gcc on linux

wtd

Posted: Thu Nov 24, 2005 2:42 am   Post subject: (No subject)

Martin wrote:
 c++: #include using namespace std; int main() {    int a = 4, b = 5;    cout << a << b << endl;    a = a + b - (b = a); // 4 + 5 - (4)    cout << a << b << endl;    return 0; }

In half of two lines. Boo yeah!

Well, I did specify in response to Gandalf that the solution should not be "hackish", and I'd say this qualifies.

For instance, make this work with two strings.

 c++: #include #include using namespace std; int main() {    string a = "4", b = "5";    cout << a << b << endl;    a = a + b - (b = a); // 4 + 5 - (4)    cout << a << b << endl;    return 0; }
[Gandalf]

Posted: Thu Nov 24, 2005 3:18 am   Post subject: (No subject)

 c++: #include #include using namespace std; int main() {    string a = "4", b = "5";    cout << a << b << endl;    a.swap(b);    cout << a << b << endl;    return 0; }

Tony's program compiles and runs fine for me...
Martin

Posted: Thu Nov 24, 2005 3:24 am   Post subject: (No subject)

Alright wtd, let's hear the solution.
wtd

Posted: Thu Nov 24, 2005 4:24 am   Post subject: (No subject)

Just to make you all hate me...

 c++: #include #include using namespace std; int main() {    int a = 4, b = 5;    cout << a << b << endl;    swap(a, b);    cout << a << b << endl;    return 0; }
Martin

Posted: Thu Nov 24, 2005 7:01 am   Post subject: (No subject)

It's alright wtd. I've hated you ever since I decided to buy my iMac.
wtd

Posted: Thu Nov 24, 2005 5:29 pm   Post subject: (No subject)

Here's a python question.

 Python: class Student(object):    def __init__(self, name, *grades):       self.name = name       self.grades = grades    def __str__(self):       return "%s has an average of %.2f" % (self.name, self.average_grade)

Add a single line of code which makes the above class work correctly. It must not only work, but also accurately report the student's average grade.
Hikaru79

Posted: Thu Nov 24, 2005 7:07 pm   Post subject: (No subject)

This okay?

EDIT: w00t! 600 posts
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 8 of 10  [ 147 Posts ]
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: