Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Test your skills (2005)
Index -> General Programming
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
[Gandalf]




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

c++:
#include <iostream>

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. Laughing
Sponsor
Sponsor
Sponsor
sponsor
wtd




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

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

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. Smile
[Gandalf]




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

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

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




PostPosted: 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




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

c++:
#include <iostream>

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




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

Martin wrote:
c++:
#include <iostream>

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 Wink
Martin




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

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




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




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

Martin wrote:
c++:
#include <iostream>

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. Smile

For instance, make this work with two strings.

c++:
#include <iostream>
#include <string>

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]




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

c++:
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string a = "4", b = "5";

   cout << a << b << endl;
   a.swap(b);
   cout << a << b << endl;
   return 0;
}


Wink

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




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

Alright wtd, let's hear the solution.
wtd




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

Just to make you all hate me...

c++:
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
   int a = 4, b = 5;

   cout << a << b << endl;
   swap(a, b);
   cout << a << b << endl;

   return 0;
}
Martin




PostPosted: 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




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

Here's a python question. Smile

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




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

Python:
class Student(object):
   def __init__(self, name, *grades):
      self.name = name
      self.grades = grades
      self.average_grade = sum(*grades)/len(*grades)
   def __str__(self):
      return "%s has an average of %.2f" % (self.name, self.average_grade)
     
foo = Student ( "Adrian", [40,34,40,50,69] )

print foo

This okay?

EDIT: w00t! 600 posts Very Happy
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 8 of 10  [ 147 Posts ]
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9, 10  Next
Jump to:   


Style:  
Search: