What's the difference between an Array and a Vector?
Vectors are Dynamic, while Arrays are Static meaning Vectors can be changed as needed.
How can we declare a Vector of type int?
vector <int> variable;
How can I declare a pointer called ptr
Is this valid :
int fish=5;
int& ref = fish;
Yes it is.
Are Arrays passed by a value or by a pointer?
Arrays are passed by a pointer.
Ex:
arr[]={1,2,3,4,5};
int* ptr = arr;
How can we declare a 2D vector of type string
vector <vector<string>> variable;
Is there anything missing from the following code?
If not what is the output
int x = 5;
int*ptr = x;
*ptr = 7;
cout << ptr <<" and " << x;
The code is missing
& sign on the line int*ptr = (&)x
After changing this the output should be
7 and 7
What's the output of the following code:
int fish=5;
int& ref = fish;
cout << ref << " and " << &ref;
5 and memory address of fish
True or false?
Arrays have a faster access time versus Vectors (o(n) constant time)
True
Given: vector<vector<int>> x;
x={{1,2,3},{4,5,6}}
what's a loop which will output both the rows and columns of this 2D vector?
for(int i =0; i < x.size();i++){
for(int col =0; col < x[i].size();col++){
cout << x[i][col];
}
}
What do pointers point to?
what will the following output:
int fish=5;
int*ptr = &fish;
cout << ptr;
The memory address of fish
Changing a reference variable changes the original variable's value?
True, both passing by pointer and reference will change the value if that variable is changed.
True or false:
datatype variable(parameters){
return datatype
}
Is this a correct way of creating a function?
True
What will the following function erase?
vector <int > x = {1,2,3,4,5};
x.erase(x.begin() +3)
It will erase the 3rd value in the vector,
So, this function will erase the value (3).
What's the output of the following code:
int arr[] = { 1,2,3,4,5 };
int* ptr = arr;
for (int i = 0; i < 5; i++) {
cout << ptr << " value:" << *ptr << endl;
ptr++;
}
It will output the
Memory address, followed by the value at each index
What is the header
#include <time.h>
used for?
For counting the elapsed time during our program
What will happen to the variable x:
int x=6;
int y = 10;
stuff(x);
void stuff(int &y){
y = 20;
}
The value of x will be changed to 20.
Given: vector <int> x;
How can I add 3 to this vector, as well as remove the last element in the vector?
ADD: x.push_back(3);
Remove: x.pop_back();
Will the following code compile?
int arr[]={1,2,3,4,5};
int* ptr = &arr;
cout << ptr << endl;
No, the following code will not compile because an array is passed by pointers and not reference, In order for the code to compile we would need to remove the &
Which part of the code would we enter our testing code for testing?
clock_t begin, end;
double elapsed_seconds;
begin = clock();
for (unsigned int i = 0; i < 100000000; i++)
{
}
end = clock();
elapsed_seconds = (double(end) - double(begin)) CLOCKS_PER_SEC;
cout << elapsed_seconds << endl;
Inside the for loop