Test your C++ knowledge with these questions!
-
Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck! 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; 2) What is wrong with the following piece of code? int sample_array[10]; for (int index=1; index<=10; index++) sample_array[index] = 3*index; 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”<
-
Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck! 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; 2) What is wrong with the following piece of code? int sample_array[10]; for (int index=1; index<=10; index++) sample_array[index] = 3*index; 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”<
soer wrote: 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; Yes, the array x is being defined 3 different times. soer wrote: 2) What is wrong with the following piece of code? int sample_array[10]; You should be using
std::vector
instead of a static array. soer wrote: 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”< Yes, there is no space between the words and the numbers being printed out. soer wrote: 4) Consider the following function definition: Void tripler(int& n) { n=3*n; } That should be:n *= 3;
and "Void" should have a lower-case v. soer wrote: 5) What (if anything) is wrong with the following code? The definition of tripler is given in #11. int b[5]={1,2,3,4,5}; for (int i=1;i<=5;i++) tripler(b[i]); The line containing "tripler" is not indented properly. soer wrote: 6) Which of the following are acceptable function calls? too2(my_array, 29); too2(my_array, 10); The function name is incorrect. It should be "tutu". So how did I do? :-D :-D Sometimes I feel like I'm a USB printer in a parallel universe. -
soer wrote: 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; Yes, the array x is being defined 3 different times. soer wrote: 2) What is wrong with the following piece of code? int sample_array[10]; You should be using
std::vector
instead of a static array. soer wrote: 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”< Yes, there is no space between the words and the numbers being printed out. soer wrote: 4) Consider the following function definition: Void tripler(int& n) { n=3*n; } That should be:n *= 3;
and "Void" should have a lower-case v. soer wrote: 5) What (if anything) is wrong with the following code? The definition of tripler is given in #11. int b[5]={1,2,3,4,5}; for (int i=1;i<=5;i++) tripler(b[i]); The line containing "tripler" is not indented properly. soer wrote: 6) Which of the following are acceptable function calls? too2(my_array, 29); too2(my_array, 10); The function name is incorrect. It should be "tutu". So how did I do? :-D :-D Sometimes I feel like I'm a USB printer in a parallel universe.hehehe :-D Ant.
-
hehehe :-D Ant.
-
Hi, I just came up with some quick C++ problems that I thought some people might like to try out. Post your answers as replies. Good luck! 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; 2) What is wrong with the following piece of code? int sample_array[10]; for (int index=1; index<=10; index++) sample_array[index] = 3*index; 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”<
Maybe those problems are concerned with as folloing: 1.Overflow 2.Reference ... and I can't understand the last problem. :)
-
soer wrote: 1) Identify any errors in the following declarations. a. int x[4] = { 8,7,6,4,3 }; b. int x[] = { 8,7,6,4 }; c. const int SIZE = 4; int x[SIZE]; Yes, the array x is being defined 3 different times. soer wrote: 2) What is wrong with the following piece of code? int sample_array[10]; You should be using
std::vector
instead of a static array. soer wrote: 3) Suppose we expect the elements of the array a to be ordered so that a[0] <= a[1] <= a[2] <=… for (int index = 0; index <10; index++) if(a[index] > a[index +1]) cout <<”Array elements”< Yes, there is no space between the words and the numbers being printed out. soer wrote: 4) Consider the following function definition: Void tripler(int& n) { n=3*n; } That should be:n *= 3;
and "Void" should have a lower-case v. soer wrote: 5) What (if anything) is wrong with the following code? The definition of tripler is given in #11. int b[5]={1,2,3,4,5}; for (int i=1;i<=5;i++) tripler(b[i]); The line containing "tripler" is not indented properly. soer wrote: 6) Which of the following are acceptable function calls? too2(my_array, 29); too2(my_array, 10); The function name is incorrect. It should be "tutu". So how did I do? :-D :-D Sometimes I feel like I'm a USB printer in a parallel universe.