• Advertisement

Archived

This topic is now archived and is closed to further replies.

Brainbuster reposted correctly (GOD I hate HTML)

This topic is 6557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, here''s the brainbuster again, correctly typed, and displayed (I think) And it wasn''t meant to be a test of syntax errors, that was purely due to the HTML. It was meant to be a test of pointer knowledge etc..., and this code will compile, (if I''ve got it to display correctly). Let''s see if it worked this time. If not, then I''ll stop posting garbage.
int main(int argc, char *argv[]){
    int x = 0;
    char a[26] = {''a'', ''b'', ''c'', ''d'', ''e'', ''f'', ''g'',
                ''h'', ''i'', ''j'', ''k'', ''l'', ''m'', ''n'',
                ''o'', ''p'', ''q'', ''r'', ''s'', ''t'', ''u'',
                ''v'', ''w'', ''x'', ''y'', ''z''};
    for(int i=0; i < 8; i++){
        x += i;
        cout << x [ a ] << endl;
    }
    return 0;
}
 

Share this post


Link to post
Share on other sites
Advertisement
Ok, that is correctly typed in, reply away my litttle chick-a-dees, oddly enough, I just found an example of this method in the ANSI C 3rd edition. I''ll check this thread a little later and reply with the answer if nobody gets it.


Jon Stelly

Share this post


Link to post
Share on other sites
Ah, now that''s a much more interesting question. My stab at the answer (I''ll explain later if I''m told I''m correct):

a
b
d
g
k
p
v
*junk* (out of array, unknown output char)

Share this post


Link to post
Share on other sites
Hej,

you had me for a second. This is a cool one. With ( and ) i actually mean the square brackets.

But x(a) is basically the same as a(x). This is because it doesn't matter whether you switch base and index cause all that happens is that they are added together to obtain the final adress.

This could become a problem when the size of the elements are bigger then a byte but I haven't thought about that one yet.

I was a little bit surprised that my compiler allowed indexing of non pointer types however.

The output of the program by the way should not be hard to find out by the way. It's the same when you do a(x). Also note that you are reading out of the array boundaries. THe answer as in the message above this one is correct.

Correct me if i'm wrong!

Jaap Suter

Edited by - s9801758 on 3/8/00 4:50:24 PM

Share this post


Link to post
Share on other sites
Shite square brackets! .

Sorry for the double post.

Edited by - s9801758 on 3/8/00 4:51:47 PM

Share this post


Link to post
Share on other sites
in the for loop, shouldn''t you do something more like this?:

x++;
cout< }//end for loop


rather than this

x += i;
cout<}//end for loop


or you could just screw x and do this:

for(int i;i=0;i++)
{
cout<<}//end for loop


You see, if you just went through the for loop using the x+= i, then this is what would happen:


i value x value
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55


see? You''re adding the current value of i to the current cumulating value of x .And then you printed to the screen x[a]. Why? x is not an array of integers. I think that you wanted to do a[x] because a is an array of chars. I think that you just accidentally switched them. Oh, and by the way, If the table that I put up there is screwed up, then I''ll try to put it up again. '' Target=_Blank>Link

"Remember, I'm the monkey, and you're the cheese grater. So no fooling around."
-Grand Theft Auto, London

Share this post


Link to post
Share on other sites
Great job fellas, both of you are right, and the array bounds issue was one of the things that he wanted me to catch.

I think I might have to find some way to use this in this years obfuscated C competition. I got it after reading through it a second time, but at least the person interviewing got a kick out of it when I started grinning ear to ear when I saw the answer.

Share this post


Link to post
Share on other sites
PsYcHoPrOg No


x+=i <=> x=x+i;
x++ <=> x=x+1;

x+=i simulation;

i 0 1 2 3 4 5 6 7
x 0 1 3 6 10 15 21 28

x++ simulation;

i 0 1 2 3 4 5 6 7
x 0 1 2 3 4 5 6 7


Hope this clears the issue ;p



Share this post


Link to post
Share on other sites
erm, (Mithrandir Raises his hand) excuse me teacher, but whats the point of this? If anyone ever actually uses code like this they should be shot on sight

sorry, but I''m comming from an engineering perspective here, and even knowing this stuff seems pretty worthless to me.

===============================================
I saw a man pursuing the horizon;
Round and round they sped. I was disturbed at this; I accosted the man.
"It is futile," I said, You can never -- "

"You lie," he cried, And ran on.

Share this post


Link to post
Share on other sites
Mithrandir: You are right, no one should write code like this. But the point is that you need to understand how it works when you come across it while debugging other peoples code. Believe me, you will see a lot of stupid code once you start working with other engineers.

Share this post


Link to post
Share on other sites
Mith, being an engineering professional myself, I have to say that it''s pretty difficult to come up with good interview questions. As it happens, this is a great interview question (IMHO), and I''ll probably steal it in the future if we ever start hiring again. Maybe you missed the original thread that stated this was a question asked in an interview.

Share this post


Link to post
Share on other sites
Yeah Mith, I agree, I''d request that anyone who used something like this be shot also, but it was an interview question. And I''d have to say it''s a pretty good one for an interview, shows you how quickly someone can analyze what is going on and tests their knowledge of pointers and arrays etc.

Share this post


Link to post
Share on other sites

  • Advertisement