Harut Posted April 26, 2002 Report Share Posted April 26, 2002 any more problems? Quote Link to comment Share on other sites More sharing options...
Sip Posted April 27, 2002 Author Report Share Posted April 27, 2002 Cool! Very nice. More problems will follow for sure Quote Link to comment Share on other sites More sharing options...
Sip Posted July 28, 2002 Author Report Share Posted July 28, 2002 quote:Originally posted by Harut:any more problems? Ok try this one ... my coworker just spent 1 hour trying to debug this thing!!! Â code:int c=99;int n=100;for (int i=0; i<2; i++) { c = (c++) % n;}cout << c << endl;What is the output of this thing? Of course you can "cheat" and actually run it ... but see if you can figure it out! I can tell you I was WRONG! ... we tried it with 3 compilers and they all got the same thing. Not what I was expecting at all. Quote Link to comment Share on other sites More sharing options...
Harut Posted July 28, 2002 Report Share Posted July 28, 2002 ha ha ha, it was a good one.  the answer is 1.  here is what is happening in c = (c++) % 100 expresion.0. c=991. c%1002. c=993. c++4. c=100 and one more time0. c=1001. c%1002. c=03. c++4. c=1 i know where you got confused.99%110 gives 99 not 0. and you tried to confuse me with and () around c++? even with the () it doesn't the effect of ++c. any more problems? Quote Link to comment Share on other sites More sharing options...
Sip Posted July 28, 2002 Author Report Share Posted July 28, 2002 The place where I made my mistake was: c=99 c++ -> c=100 (but returns 99) so: c=99%100 which makes c=99. But the way these compilers seem to work, is they do: c=99%100 thenc=c+1 (for the c++)  For one thing, I would NEVER write any program that looks like c=c++ !!! But I have not seen any book or any documentation which actually explains what would happen if you have something like that! Quote Link to comment Share on other sites More sharing options...
elovna Posted July 30, 2002 Report Share Posted July 30, 2002 1 goes left, 2 goes right, 3 left, 4 right, etc. Â I am not old enough to know everything, derr! Quote Link to comment Share on other sites More sharing options...
Sip Posted July 30, 2002 Author Report Share Posted July 30, 2002 quote:Originally posted by Aghtchik:[QB]1 goes left, 2 goes right, 3 left, 4 right, etc. Harut jan, you ha ... ooops, Sorry, it was a force of habit!!!! Ok so as far as I understand, now you have all odd numbers on the "left" and all even ones on the "right". What now?  ---Update: Actually, after thinking about it for a while, I think that problem is much harder than I thought at first. So here is the updated version of the problem! But feel free to answer either one  You have 1 million objects with integer names (between 0 and 1 million) and you want to put them in order. What is the fastest way? Quote Link to comment Share on other sites More sharing options...
Sip Posted July 30, 2002 Author Report Share Posted July 30, 2002 Next problem? Let's say you have 1000 integers between 0 and one million and you want to put them in order. What is the fastest way to do it? Quote Link to comment Share on other sites More sharing options...
Azat Posted July 31, 2002 Report Share Posted July 31, 2002 depends what language you are working in. If in vb, you creaet a collection and run Sort on it. I know I am a WiseASS. But this was the computers section. Quote Link to comment Share on other sites More sharing options...
Sip Posted July 31, 2002 Author Report Share Posted July 31, 2002 quote:Originally posted by Azat:depends what language you are working in. If in vb, you creaet a collection and run Sort on it. I know I am a WiseASS. But this was the computers section.Good point ... you got me! I meant fastest "run-time" ... not fastest programming/debugging time.  Otherwise, I can also import them in Excel and choose "SORT" from the data menu Quote Link to comment Share on other sites More sharing options...
Sip Posted August 17, 2002 Author Report Share Posted August 17, 2002 quote:Originally posted by Sip:Next problem? Let's say you have 1000 integers between 0 and one million and you want to put them in order. What is the fastest way to do it?SOLUTION: You make one million + 1 slots numbered from 0 to 1000000. You mark them all emtpy. Then for each number, you put it in it's corresponding slot by marking the slot FULL. So basically with one pass through the 1000 integers, you have them in sorted order! You may need one more pass to "print them out" but that's about it. Fastest way to sort 1000 integers (or integer-numbered objects) Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 here is one for you guys. actually it's not a callenge, just a regular, easy task. i recently took a test-like thing for an internship possision. and one of the question was this. i just want to see how you guys would do it and compare it with mine. i just want to see if there are any hopes, should i wait for a call or not. anyways, here it is. quote:1) Using any programming language you are familiar with, write a programwhich does the following: A) parse the following string based on the spaces between words, and outputeach word on its own line to the console."The lazy brown fox jumped over the cat" The following string represents a method call on an object instance named'myObject'. Parse the string and output the name of the object, the name ofthe method, and each method parameter, with each value on its own line.Strip out any unnecessary characters, such as '.', '(', ')', and literalquotes."myObject.myMethod(1, 72, "test1", 82.5);"i'd prefer you use C++. that's what i used. Quote Link to comment Share on other sites More sharing options...
Sip Posted December 6, 2002 Author Report Share Posted December 6, 2002 A)  code:char c;while(!cin.eof()) cout << (((c=cin.get())==' ') ? 'n' : c); code:int i=0;char c;cout << "object: "; while(!cin.eof() && (c=cin.get())!='.') cout << c; cout << endl;cout << "method: "; while(!cin.eof() && (c=cin.get())!='(') cout << c; cout << endl;while (!cin.eof() && cin.peek()!=')') { cout << "paramater " << ++i << ": "; while (!cin.eof() && cin.peek()!=')' && (c=cin.get())!=',') if (c!='"' && c!=' ') cout << c; cout << endl;}In both cases I assumed there is no syntax checking required. In both cases I assumed EOF (end of file) terminates the input. Both those work exactly as specified by using the input stream redirection (i.e. compile the program and run it using a redirected input text file from the console). I assumed the " " around the input are only there in the problem statement to specify things as input. Quote Link to comment Share on other sites More sharing options...
Azat Posted December 6, 2002 Report Share Posted December 6, 2002 quote:Originally posted by Harut:please delete all the unregisted stuffdone. how did you manage to pose with the unregistered account? Quote Link to comment Share on other sites More sharing options...
Sip Posted December 6, 2002 Author Report Share Posted December 6, 2002 Nice MESS you created here HARUT  Part B doesn't look like it would work. Seems to me it would just print some characters, one character one a line, and won't really "parse" things the way it's supposed to. Did you try to run it? Quote Link to comment Share on other sites More sharing options...
Sip Posted December 6, 2002 Author Report Share Posted December 6, 2002 Oooooh ... and another thing... take a close look at where you say "sizeof(methodCall)" Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 quote:Originally posted by Sip:Nice MESS you created here HARUT smilies/lol.gif  Part B doesn't look like it would work. Seems to me it would just print some characters, one character one a line, and won't really "parse" things the way it's supposed to. Did you try to run it?it does work. i ran it and it gives me this output.code:Thelazybrownfoxjumpedoverthecat myObjectmyMethod172test182.5can you please write a little what "parse" really means? Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 quote:Originally posted by Sip:Oooooh ... and another thing... take a close look at where you say "sizeof(methodCall)" can't figure it out. what do you mean? Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 oh, just to note that i missed the function declaration for the second one. so there is a first line to the third part. code:void PartB ()and one more thing. like i mentioned already, i "or" should be logical or.logical or is the cause of the mess in the forum. Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 i'll try one last time... compact version code:#include using namespace std;#include void PartA ();void PartB ();int main (){ PartA (); PartB (); getch (); return 0;}continued... Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 ...code:void PartA (){ char sentence[] = "The lazy brown fox jumped over the cat"; for ( int i = 0; i < sizeof ( sentence ); i++ ) cout << ( sentence [ i ] == ' ' ? 'n' : sentence [ i ] ); cout << endl;}continued... Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 i see what's the problem. i changed the logical or to "or". ...code:{ char methodCall [] = "myObject.myMethod(1, 72, "test1", 82.5);"; bool newLine = true; cout << endl; for ( int i = 0; i < sizeof ( methodCall ); i++ ) { if (( methodCall [ i ] >= 48 && methodCall [ i ] <= 57 ) or ( methodCall [ i ] >= 65 && methodCall [ i ] <= 90 ) or ( methodCall [ i ] >= 97 && methodCall [ i ] <= 122 ) or ( methodCall [ i ] == '.' && methodCall [ i + 1 ] >= 48 && methodCall [ i + 1 ] <= 57 ) ) { cout << methodCall [ i ]; newLine = true; } else if ( newLine ) { cout << 'n'; newLine = false; } }}the end Quote Link to comment Share on other sites More sharing options...
Harut Posted December 6, 2002 Report Share Posted December 6, 2002 please delete all the unregisted stuff Quote Link to comment Share on other sites More sharing options...
Sip Posted December 7, 2002 Author Report Share Posted December 7, 2002 quote:Originally posted by Harut: quote:Originally posted by Sip:Oooooh ... and another thing... take a close look at where you say "sizeof(methodCall)" can't figure it out. what do you mean?My bad on BOTH things. The sizeof the way you are using is correct. However, one often uses the strlen function to check for the length of a string as sizeof() doesn't work for dynamic arrays. Also, you are right ... your code works I didn't compile it correctly in my head Quote Link to comment Share on other sites More sharing options...
Harut Posted December 7, 2002 Report Share Posted December 7, 2002 quote:Originally posted by Sip:I didn't compile it correctly in my headdid you get all the latest upgrades? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.