

InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
1. |
Solve : A note for all programmers? |
Answer» Everyone who is on a large-scale project can benefit from this simple, yet amazing advice. It's the best way to make sure your code works. It can be summed up as follows: |
|
2. |
Solve : Intermediate C/C++ - #define? |
Answer» The #define Keyword For those of you who are coding in C or C++ and have to cut-paste a lot of code, this may be a big help. Normally, you shouldn't cut and paste code - it leads to horrible misunderstandings. However, in the event that an action (a block of code) needs to be done a lot, #define is excellent for helping with that. How it works #define is a pre-compiler instruction that does a simple text replacement. That's it. For example if you had this: Code: [Select]#define BIG 512 int arrayObjects[BIG]; By the time the actual compiler began working with your code, it would see this: Code: [Select]int arrayObjects[512]; The pre-compiler instruction, like comments, are stripped at this point, to save space. This is particularly handy if you need a block of code to be executed a lot. An example is a command prompt game I wrote the other week. I needed delays in my code so users would READ the text line-by-line, as opposed to the bottom of a paragraph, which was my first inclination. I used this (#include ) to do that: Code: [Select]time_t X, y; time(&x); do { time(&y); } while((y - x) < 1.5); //1.5 second delay I needed this code to repeat after each line of text. I couldn't just do this: Code: [Select] time_t x, y; cout << "A line of text here.\n"; time(&x); do { time(&y); } while((y - x) < 1.5); cout << "More text\n"; time(&x); do { time(&y); } while((y - x) < 1.5); Doing this ad infinitum is painstaking, annoying, and hard to read or maintain. There are two ways to do this. One is to put the declarations time_t x,y as a global variable and place the while loop in a function, then I can simply call Code: [Select]cout << "Line of text\n"; smallDelay(); Assuming smallDelay() has the delay code, this works. But this is even neater, in that you can write as few as three letters each time (less is POSSIBLE, but the point of this is clarity, so...). Here's an example: Code: [Select]#define PAU time(&x); \ do \ { \ time(&y); \ } ] while((y - x) < 1.5) cout << "Line of text...\n"; PAU; cout << "Second line of text...\n"; PAU; And so on. Possible problems with #define #define is useful, but it can lead to trouble. #define does not do any type casting, which means it does not make sure you are assigning proper values to an int, string, or other variable. You could do this: Code: [Select]#define BIG 512 using std::string; int theInt = BIG; string THESTRING = BIG; //Oops, meant to have 'char theString = "BIG";'! The compiler would see string theString = 512; An int cannot be assigned to a char[]. In fact, you will probably see an error message almost exactly like the previous sentence. Having quotes around "BIG" makes the intended assignment: The character array 'B', 'I', 'G', '\0'. (Strings end in null 0) |
|
3. |
Solve : C++ strings in upper/lower case? |
Answer» Question I've got a program I'm writing in C++ and I need user input to be all lower or uppercase. How can I get this without spitting error messages every time someone capitalizes/doesn't capitalize a single letter? Answer This probably isn't a common question, but it's one that led me on a long chase with several answers that would mess up depending on the compiler. This is stupid. There is a method, quick and easy, that works in C++, and it should be kept within easy access, in my opinion, because anyone who is learning C++ will play with the GETLINE(cin) stuff and will want to use conditional statements with that input. It's all part of learning. So, an example: Code: [Select]//To lowercase string X = "DilBERt"; //So we get a mix. for(int i = 0; i < 100; i++) //This makes sure that we get the whole word. { if(x[i] + 32 < 122) //122 is the VALUE for "z". { x[i] += 32; //32 is the difference between an uppercase and its lower counterpart } } For conversion to uppercase, change the if statement to if(x - 32 > 65) // 65 is the value for "A". and x += 32 should be changed to x -= 32 |
|
4. |
Solve : Operator overload -- Factorials (C++)? |
Answer» This isn't the solution I wanted to this problem, but it's pretty darn close, and it's easy to use. In common usage I have seen ! being used as factorial. That's why Dilbert has defined the ! as an operator, Silly the ! is one of a slim number of Postfix operators, where the operator appears after the number/value it operates on. NOT fun to try to parse an expression and handle these... Definitely a bigger pain then your standard UNARY operator (such as minus) What is an expressionQuote from: diDy on March 15, 2010, 07:29:25 AM What is an expressionThis section of the forum is for answering questions that are frequently asked, just like yours. If you can't find your answer in this section of the forum, try this section: Computer Programming |
|