int main(void)  {      int arr[] = { 2, 3, 4, 10, 40 };      int n = sizeof(arr) / sizeof(arr[0]);      int x = 2;      int result = binarySearch(arr, 0, n - 1, x);      (result == -1) ? printf(&quot;Element is not present in array&quot;)                     : printf(&quot;Element is present at index %d&quot;,                              result);      return 0;  }   so right beside the (result==-1) is the ? and : . now i've never seen these things before and don't know how they work, are they like True of false statements or like switch-cases?
610
I've always wondered how to write the "A ? B : C" syntax in a C++ compatible language.  I think it works something like: (Pseudo code)  If A &gt; B    C = A Else    C = B   Will any veteran C++ programmer please help me out?
4478
As Joel points out in , in  (aka: K &amp; R), there is mention of this property of arrays in C: a[5] == 5[a]  Joel says that it's because of pointer arithmetic but I still don't understand. Why does a[5] == 5[a]?
6134
I had a task where I had to figure if the argument was true or not.  $A=\{ n ∈ ℤ \mid n^2 &lt; 5 \}, \quad  B=\{ 7, 8, \{2\}, \{2, 7, 8\}, \{\{7\}\} \}$  The first one was $\{-1, 2\} ∈ A$ and the answer to this was not true  since the set is not an integer.  The second one was $\{-1, 2\} ⊂ A$ and the answer to this was true since  -1 and 2 are elements of $A$.  I just can't seem to understand the difference between ∈ and ⊂ in this task and why the first argument was not true. Also it confuses me that the answer to the first one was that not true because the set is not an integer but aren't -1 and 2 integers?  There is other parts of this task that also add more confusion:     {2, 7, 8} ∈ B Answer: True since {2, 7, 8} is an element of set B      {2, 7, 8} ⊂ B Answer: Not true, for example 2 is not an element in set   B
79818
This is probably pretty basic, but to save me an hour or so of grief can anyone tell me how you can work out the number of bits required to represent a given positive integer in Java?  e.g. I get a decimal 11, (1011). I need to get the answer, 4.  I figured if I could work out how to set all the bits other than the most significant bit to 0, and then >>> it, I'd get my answer. But... I can't.
32837
int temp = 0x5E; // in binary 0b1011110.   Is there such a way to check if bit 3 in temp is 1 or 0 without bit shifting and masking.  Just want to know if there is some built in function for this, or am I forced to write one myself.
17976
I'm using C++ to understand how exactly pointers work. I have this piece of code using arrays, which I'm using just to understand how the equivalent works with pointers.  int main() {         int arr[10] = {1,2,3};         char arr2[10] = {'c','i','a','o','\0'};     cout &lt;&lt; arr &lt;&lt; endl;     cout &lt;&lt; arr2 &lt;&lt; endl; }   However when I run this, arr outputs the address of the first element of the array of ints (as expected) but arr2 doesn't output the address of the first element of the array of chars; it actually prints "ciao".  What is it that I'm missing or that I haven't learned yet about this?
25150
This is a question that's been nagging me for some time. I always thought that C++ should have been designed so that the delete operator (without brackets) works even with the new[] operator.  In my opinion, writing this:  int* p = new int;   should be equivalent to allocating an array of 1 element:  int* p = new int[1];   If this was true, the delete operator could always be deleting arrays, and we wouldn't need the delete[] operator.  Is there any reason why the delete[] operator was introduced in C++?  The only reason I can think of is that allocating arrays has a small memory footprint (you have to store the array size somewhere), so that distinguishing delete vs delete[] was a small memory optimization.
114370
This is about files straight from the compiler, say g++, and the -o (outfile) flag.  If they are binary, shouldn't they just be a bunch of 0's and 1's?  When you cat them, you get unintelligible output but also intact words.  If you file them, you get the answer immediately - there seem to be no computation. Do the binary files in fact have headers with this kind of information?  I thought a binary executable was just the program just compiled, only in the form of machine instructions that your CPU can instantly and unambiguously understand. If so, isn't that instruction set just bit patterns? But then, what's all the other stuff in the binaries? How do you display the bits?  Also, if you somehow get hold of the manual of your processor, could you write a binary manually, one machine instruction at a time? That would be terribly ineffective, but very fascinating if you got it to work even for a "Hello World!" demo.
21013
Most properties of a single binary operation can be easily read of from the operation's table. For example, given $$\begin{array}{c|ccccc} \cdot &amp; a &amp; b &amp; c &amp; d &amp; e\\\hline a     &amp; e &amp; d &amp; b &amp; a &amp; c\\ b     &amp; d &amp; c &amp; e &amp; b &amp; a\\ c     &amp; b &amp; e &amp; a &amp; c &amp; d\\ d     &amp; a &amp; b &amp; c &amp; d &amp; e\\ e     &amp; c &amp; a &amp; d &amp; e &amp; b \end{array}$$ it is easy to check that it is closed (no elements occur in the table which don't occur as row or column index), commutative (the table is symmetric), has a neutral element (the row and column of $d$ are copies of the index row/column), and has an inverse element for each element (there's a $d$ in each row and column). In other words, almost all important properties can immediately be seen. The only part missing is associativity.  Therefore my question: Is there a simple way to see directly from the operation's table (i.e. without doing explicitly all the calculations) if an operation is associative?
81943
How does one parse an integer to string(char* || char[]) in C? Is there an equivalent to the Integer.parseInt(String) method from Java in C?
47168
I was trying to understand something with pointers, so I wrote this code:  #include &lt;stdio.h&gt;  int main(void) {     char s[] = "asd";     char **p = &amp;s;      printf("The value of s is: %p\n", s);     printf("The direction of s is: %p\n", &amp;s);      printf("The value of p is: %p\n", p);     printf("The direction of p is: %p\n", &amp;p);      printf("The direction of s[0] is: %p\n", &amp;s[0]);     printf("The direction of s[1] is: %p\n", &amp;s[1]);     printf("The direction of s[2] is: %p\n", &amp;s[2]);      return 0; }   When compiling it with gcc I get these warnings:  $ gcc main.c -o main-bin -ansi -pedantic -Wall -lm main.c: In function ‘main’: main.c:6: warning: initialization from incompatible pointer type main.c:9: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘char (*)[4]’ main.c:11: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘char **’ main.c:12: warning: format ‘%p’ expects type ‘void *’, but argument 2 has type ‘char ***’   (The flags for gcc are because I must be C89)  Why incompatible types of pointer? Isn't the name of an array a pointer to it's first element? So if s is a pointer to 'a', &amp;s must be a char **, no? And why do I get the other warnings? Do I have to cast the pointers with (void *) in order to print them?   And when running I get something like this:  $ ./main-bin The value of s is: 0xbfb7c860 The direction of s is: 0xbfb7c860 The value of p is: 0xbfb7c860 The direction of p is: 0xbfb7c85c The direction of s[0] is: 0xbfb7c860 The direction of s[1] is: 0xbfb7c861 The direction of s[2] is: 0xbfb7c862   How can the value of s and it's direction (and of course the value of p) be the same?
82601
I'm trying to convert an incoming sting of 1s and 0s from stdin into their respective binary values (where a string such as "11110111" would be converted to 0xF7). This seems pretty trivial but I don't want to reinvent the wheel so I'm wondering if there's anything in the C/C++ standard libs that can already perform such an operation?
93317
I'm learning how to write programs in C using the k&amp;r book (The C Programming Language) and I have a problem with one of the exercises. It's asking me to detect and remove a character in string s1, which matches any characters in the string s2.  So, say s1 = "A";  And s2 = "AABAACAADAAE"  I want it to return "BCDE"  I know I'm on the right path towards it, i just don't know how to design programs very well, could you give me any additional tips. I tried to read about the binary search tree algorithm, but felt it was a little too advanced for this mundane task.  Thanks everyone!  /* An alternate version of squeeze(s1, s2) that deletes each character in  * s1 that matches any character in the string s2  *  * Angie@odfx.org  */  #include &lt;stdio.h&gt; #include &lt;string.h&gt;  void squeeze(char s[], char t[]);  char string[] = "BAD"; char sstring[] = "ABC";  int main(void) {     squeeze(string, sstring);     return 0; }  void squeeze(char s[], char t[]) {     int i, j, d;      d = 0;     if(strstr(s, t) == NULL)         printf("%c", s[i]);     s[j] = '\0'; }
13470
I am a little surprised by the following.  Example 1:  char s[100] = "abcd"; // declare and initialize - WORKS   Example 2:  char s[100]; // declare s = "hello"; // initalize - DOESN'T WORK ('lvalue required' error)   I'm wondering why the second approach doesn't work. It seems natural that it should (it works with other data types)? Could someone explain me the logic behind this?
119081
I am trying to output the values of array element but I am getting weird output. Please have a look at it and help me solve it. Thank you.  n=2 declare -a myarray[$n] myarray[0]=hey myarray[1]=hello myarray[2]=bye for i in ${myarray[@]} do         echo $i done   Output:  robin@robin-VirtualBox:~/lx$ sh array.sh array.sh: 2: array.sh: declare: not found array.sh: 3: array.sh: myarray[0]=hey: not found array.sh: 4: array.sh: myarray[1]=hello: not found array.sh: 5: array.sh: myarray[2]=bye: not found array.sh: 6: array.sh: Bad substitution
42562
How could I go about detecting (returning true/false) whether an ArrayList contains more than one of the same element in Java?  Many thanks, Terry  Edit Forgot to mention that I am not looking to compare "Blocks" with each other but their integer values. Each "block" has an int and this is what makes them different. I find the int of a particular Block by calling a method named "getNum" (e.g. table1[0][2].getNum();
91230
What does the following code do in C/C++?  if (blah(), 5) {     //do something }