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
Andy




PostPosted: Wed Jan 12, 2005 10:36 pm   Post subject: (No subject)

since when did u conform to the norm wtd?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Jan 12, 2005 10:40 pm   Post subject: (No subject)

Andy wrote:
since when did u conform to the norm wtd?


Since I started wanting my code to work right. Wink

Let me just reduce it to this: any time you spend learning nonstandard C++ or java, or whatever is time you've lost and you'll never get back. It's time you've not only not spent learning something useful, but it's work you have to undo later on, meaning even less time to learn useful stuff.
wtd




PostPosted: Tue Jan 25, 2005 11:57 pm   Post subject: (No subject)

The answer to this one is way overdue.

wtd wrote:
For 30 bits, just to shake things up.

I have the following class in Ruby:

code:
class Foo
   def initialize(bar, baz)
      @bar, @baz = bar, baz
   end
end


Now, @bar and @baz are private, as all instance variables are in Ruby, but I can write methods which let me access those variables.

With only two additional lines of code add the ability to read and write to @bar, and read @baz.

If you use more than two lines, there will be no bits. Smile


code:
class Foo
   attr_accessor :bar
   attr_reader :baz
   def initialize(bar, baz)
      @bar, @baz = bar, baz
   end
end


wtd wrote:
Next question!

I have a Haskell function for multiplying an integer by two. Its type is:

code:
double :: Int -> Int


Without changing the name "double", write the shortest possible function definition. This is a 20 bit question.


We start with the obvious:

code:
double n = n * 2


But we realize that n is redundant, so we use a partial application of the * operator, which is really just a function itself.

code:
double=(*2)


And that, folks, is as short as it gets.
wtd




PostPosted: Wed Apr 13, 2005 1:21 pm   Post subject: (No subject)

The following will not compile. Make the minimal necessary change such that it does compile, and PM me with your answer.

c++:
class A
{
   private:
      const int& b;
   public:
      A(const int& init_b)
      {
         b = init_b;
      }
};

int main()
{
   int foo(42);
   A bar(foo);

   return 0;
}
wtd




PostPosted: Wed Apr 13, 2005 2:16 pm   Post subject: (No subject)

Sorry, rizzix, that's incorrect. Smile
rizzix




PostPosted: Wed Apr 13, 2005 2:19 pm   Post subject: (No subject)

yea i deleted the post.. did't read it correctly Razz try adding an &
wtd




PostPosted: Wed Apr 13, 2005 2:24 pm   Post subject: (No subject)

Nope. Nowhere do you add an & in the solution. Smile
rizzix




PostPosted: Wed Apr 13, 2005 2:26 pm   Post subject: (No subject)

then it has to be the const.. but.. well i'm not fimiliar with the &.. anyhow.. then isint int & b = &k; the way to go.. and not int &b = k;
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Wed Apr 13, 2005 2:32 pm   Post subject: (No subject)

Here's a hint: what if you wanted to do this?
c++:

#include <iostream>
class A
{
   private:
      const int& b;
   public:
      A(const int& init_b)
      {
         std::cout << b << std::endl;
         b = init_b;
      }
};

int main()
{
   int foo(42);
   A bar(foo);

   return 0;
}
rizzix




PostPosted: Wed Apr 13, 2005 2:34 pm   Post subject: (No subject)

huh? howz that a hint.. i got it already.. gee.. the alias is defined const. pfft. i shouldn't.. point is.. the referencing is wrong.. now isin't it?

as for my second question.. constructors cannot initialise const values?
Martin




PostPosted: Wed Apr 13, 2005 2:38 pm   Post subject: (No subject)

the const is fine the way it is.
wtd




PostPosted: Wed Apr 13, 2005 2:40 pm   Post subject: (No subject)

In C++, in addition to regular old C style pointers there're also references. A reference is declareed like so:

code:
int& foo


Whereas a regular int would be declared as:

code:
int foo;


And a pointer to an int as:

code:
int * foo;


A reference to a constant int would be like so:

code:
const int& foo;


If we have an int variable, like so:

code:
int foo(42);


We can assign it to an int reference like so:

code:
int& bar(foo);


The & used as a prefix operator returns the address in memory of the variable. This is useful when dealing with pointers, but not so much references.

For instance, we might have:

code:
int foo(42);
int * bar(&foo);
rizzix




PostPosted: Wed Apr 13, 2005 2:41 pm   Post subject: (No subject)

martin:i dont think so. i compiled it.. and apparently contstuctors cannot initialise const values.
Martin




PostPosted: Wed Apr 13, 2005 2:43 pm   Post subject: (No subject)

Yes they can. I already got this one.
wtd




PostPosted: Wed Apr 13, 2005 2:43 pm   Post subject: (No subject)

rizzix wrote:
martin:i dont think so. i compiled it.. and apparently contstuctors cannot initialise const values.


Oh, I assure you they can. Are you sure you understand C++ constructors as well as you think? What you think is initialization might not be initialization at all. Wink
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

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


Style:  
Search: