Friday, July 31, 2009

A few C++ questions?

Hey guys, just got a few c++ questions





1. What is an unsigned int?


2. When writing a boolean expression are numbers (other than 0) and "true" interchangable? (ie. bool a = true; is the same as bool a = 23;)


3. Can a float be an integer? (ie. float a = 6;)


4. What are substrings and how do you use them?


5. What order do you do commands like %26amp;%26amp;, || and ! in?


6. In relation to the commands above, what combos produce what? (f || t %26amp;%26amp; t || t yield what?)


7. How do you create random numbers (I remember my GSI telling us about special libraries that have to be called, what are they?)








Thanks

A few C++ questions?
1) The difference between signed and unsigned is how the computer interprets a particular bit pattern. For example, take an 8 bit byte.





If you tell the compiler this is an unsigned byte, the values range from 0 to 255. A signed 8 bit integer ranges from -128 to +127. unsigned integers are %26gt;= 0, ranging from 0 to (2^n) - 1.





2) yes. In any statement that takes a boolean expression (if, while, for. ?, until) any non-zero numeric value is treated as true and 0 is treated as false.





3) Yes, floats can represent some integers. Because of the float representation, there are some integers float cannot directly represent (though small integers are fine).





4) This is a very open question. Technically a substring is any inner portion of a source string. If you have a char pointer that points to a source string, you can make another pointer that points to somewhere inside the source string. What you do with it is up to you. You can parse for words, search for a token. Who knows what?





5) || has the lowest precedence, then %26amp;%26amp;, and ! is the highest of the 3





6) You didn't list any commands, so your example:


f || t %26amp;%26amp; t || t


%26amp;%26amp; is highest. t %26amp;%26amp; t -%26gt; t


f || t || t -%26gt; f || t





7) You call the standard library function rand(). Writing a good pseudorandom function is very difficult. Since computers are deterministic, the functions are not truly random, that's why they are called pseudorandom.
Reply:1. What is an unsigned int?


This is an integer value that is 0 or greater (ie: it does not include negative numbers).





2. When writing a boolean expression are numbers (other than 0) and "true" interchangable? (ie. bool a = true; is the same as bool a = 23;)


I think so. Its not 100% intuitive, but I can remember writing code like 'if (count) { //do something }', but its been a while.





3. Can a float be an integer? (ie. float a = 6;)


I think it has to be 6.0 (although assigning it to 6 might be okay, although it may be stored as 6.0). I am not 100% sure on that one.





4. What are substrings and how do you use them?


Substrings I think are just parts of a string. You can pull out parts and do what you want with them.





5. What order do you do commands like %26amp;%26amp;, || and ! in?


%26amp;%26amp; (and) and || (or) come in between logical arguments eg: (a %26amp;%26amp; B). ! (not) comes before !a





6. In relation to the commands above, what combos produce what? (f || t %26amp;%26amp; t || t yield what?)


f || t -%26gt; False or True = True


t || t -%26gt; True or True = True


(So I believe the entire statement is True)





7. How do you create random numbers (I remember my GSI telling us about special libraries that have to be called, what are they?)


I believe this is rand() or srand(int). The only difference is that srand takes a seed number that is used for calculating the random number. This is useful if you want to create a repeatable randomization. rand() always returns a different number. srand(100) always returns the same random number sequence.
Reply:1-





true = 1 false = 0


however something like this if (23){} a non-zero will be interpreted as true (non-NULL where NULL=0).





2-


unsigned int


The unsigned int type represents an unsigned integer comprised of 2 bytes. It has a range of 0 to 65535.





3- float a =6 is correct





4- a substring is a part of a bigger string. You can take a look at the String class to see how to manipulate strings.





http://www.cppreference.com/cppstring/in...








5- !(not) is always in front of a variable


when using a complicated expression its allways good to use brackets. If you don't use brackets the order is from left to right.





6- f || t %26amp;%26amp; t || l yield should never be used like this.


it should be something like this:


( (f || t) %26amp;%26amp; (t || l ))





7- the answer is described in detail and all different forms are also mentioned.





http://www.daniweb.com/forums/thread1769...
Reply:Hey, I am not sure about all of your questions, but I will try to answer them as best I can:





1. An int that does not have a sign, like 99, 53, 24. -5, -34, -67 all have signs, the '-'. Basically, just any non-negative number.





2. Not really sure, why not try it?





3. I am pretty sure it can, but would be a slight waste of memory to declare it as one, but not use it as one, plus the range of numbers floats can handle is smaller than ints im pretty sure.





4. Substrings are parts of other strings. So, if you have the string "Hello World", you could say that "Hello", "World", " ", "ello worl", "lo wo", or any other part of that string.





5 %26amp; 6. I am not really sure I understand you... first of all, %26amp;%26amp; and || are in a separate category of operators from !. Also, you can use parentheses to group logical statements. So, (f || t) %26amp;%26amp; (t || t) would return true, but I am not sure how it would go without the parentheses. Yet again, just try it!





7. I think you need stdlib.h, and you can use srand(int) to seed the generator, and then when ever you call rand(), a random number will be generated. Remember! Computers can not create random numbers! Thus, in order to create more true random numbers, always seed the generator with something that will be unique each time the program is run, like the current time/date in milliseconds. Try it! do:





cout %26lt;%26lt; rand();





three times inside of a normal program with iostream and stdlib included. You will get the same numbers each time!


Now, add srand(someInt); near the top, and be amazed!


No comments:

Post a Comment