You are on page 1of 2

C++ question and answer

(From Sreyka and Pich)


Pich
1. Does c++ have signed and unsigned int, float ?
->An int or unsigned int has 4bytes = 32bits, which can represent 232 different values.
An unsigned int represents 232 different values from 0 to 232 -1.
An int represents 232 different values from - 231 to 231 -1.
A short or unsigned short has 2bytes = 16bits, which can represent 216 different values.
An unsigned short represents 216 different values from 0 to 216 -1.
A short represents 216 different values from - 215 to 215 -1.
A char or unsigned char has 1bytes = 8bits, which can represent 256=28 different values.
An unsigned char represents 28 different values from 0 to 255=28 -1.
A char represents 28 different values from -128 = - 27 to 127 = 27 -1.
A float has 4bytes = 32bits can represent much wider range of values from -3.4X10 -38 to 3.4X10-38.
A double has 8bytes = 64bits can represent much wider range of values from -1.7X10-308 to 1.7X10-308.

2. When will we use do-while loop?

while (condition){ int i = 0;


do something; while (i<10){ for(int i=0;i<10;i++){
} cout << i << endl; cout << i << endl;
i++; }
}

3. What are the danger way to use pointer?


->You should not touch unallocated memory.
int ii[10]; // 10 int variables are allocated from the address ii to ii+9.
int *integerPointer;
integerPointer = ii + 5;
integerPointer[3] = 100; // OK. It is the same as ii[8] = 100;
integerPointer[7] = 200; // Not OK,
// since it is the same as ii[12] and it touches unallocated memory

4. Can we print all the value in array without for loop or in other way?
->You can print by using while (but, for is the best way.)
int ii[ ] = {100, 200, 300, 400};
int i = 0;
while (i<4){
cout << ii[i] << endl;
}

5. Why do we use global variables?


->You may use global variable when many functions use a variable. But it is not recommended to use
global variable because it is much probable to cause unexpected error in a very long program.

6. Can for (auto c : a ) use instead of for (int i=0 ; i<10;i++)?


->auto does something for every element of an array.
int ii[] = {100, 200, 300, 400}; int ii[] = {100, 200, 300, 400};
for(int i=0;i<4;i++){ for(auto c : ii) {
cout << ii[i] << endl; cout << c << endl;
} }
7. Can array contain string and number at the same array?
->Yes! That is what we want to do with “class” which we will learn in next class.

Sreyka

1. In C++, can assign an array to an array? For example : arr1 = arr2


->No.
int aa[4] = {100,200,300,400}, bb[4];
// not bb = aa; but,
for(int i=0; i<4; i++){
bb[i] = aa[i];
}//this is ok to use.
2. Is the size of an array does inherit from an array itself or what?
->There is no inheritance in array.
int aa[4], *bb; // aa is static memory. bb is a pointer.
// then sizeof array aa is 16bytes = 4 ints
cout << sizeof(aa) << endl;
bb = new int[6]; // then bb is a pointer which leads 6 int’s
// the memory is dynamic memory.
cout << sizeof(bb) << endl; // cout the size of address 4bytes or
// 8 bytes depending on the CPU type.
3. in C++, will an array name always decay to pointers in most context? And if so will it also impact
memory access?
->There is no decay in C++. But you may delete the dynamic allocated memory.
int *bb;
bb = new int [4]; // bb leads 16bytes of allocated memory.
delete [ ] bb; // release(or return) the memory.
bb = new int [8]; // bb leads 32bytes of allocated memory.
delete [ ] bb; // release(or return) the memory.
4. I’m not quite sure or understand the ‘Auto’ part. May you give a short example of its unique side of
using ?
->auto is used to decide variable type when it is used. In for-loop, auto visits every element of an array.
int aa[3] = {100, 200, 300};
float bb[4] = {3.14, 0.1, 0.2, -3.0};
for(auto c : aa) { cout << c << endl;} // print 100 200 300
// c becomes an int.
for(auto c : bb) { cout << c << endl;} // print 3.14 0.1 0.2 -3.0
// c becomes a float.

You might also like