Consider the following function definition and variable declarations:
void square(int &n){n= n*n;}
int arr[] = {1, 2, 3};
int number = 4;
Which of the following function calls are acceptable? (can have multiple answer)
a.square(1);
b.square(2);
c.square(arr[number]);
d.square(number);
What is the output of the following code segment?
int arr[] = {1, 4, 1, 0};
for (int i=0; i < 4; ++i)
cout<<arr[i]*2;
a.1014
b.1 4 1 0 (space in between each number)
c.1410
d.0140
e.None of the above
Given array declaration int a[] = {1, 2, 3};, the value of a[3] is
a.1
b.2
c.3
d.Undefined (or out-of-bound error)
e.None of the above
Question 1:
Answer :
c.square(arr[number]);
d.square(number);
Explanation :
*********************************
Question 2:
Answer :e.None of the above
Explanation :
************************************
Question 3:
Answer :e.None of the above
Explanation :Here a[3] will be 0 because array is defined with three elements and a[3] will be 0.
************************************
Consider the following function definition and variable declarations: void square(int &n){n= n*n;} int arr[] = {1,...
int arr[] = {1,4, 1, 0); for (int i=0; i < 4; ++i) cout<<arr[i]*2; 0140 1014 1410 (space in between each number) 1410 None of the above void square(int &n){n= n*n;} int arr[] = {1, 2, 3}; int number = 4; Which of the following function calls are acceptable? square(1); square(arr[number]); square(number); square(2);
what is the output of the following code segment? C++ g. int arr[3][4]; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) arr[i][j] =i*4 + j; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) cout << arr[i][j] << " "; h. int next(int & x) { x= x + 1; return (x + 1); int main() { int y = 10;...
(a) Consider the following C++ function: 1 int g(int n) { 2 if (n == 0) return 0; 3 return (n-1 + g(n-1)); 4} (b) Consider the following C++ function: 1 bool Function (const vector <int >& a) { 2 for (int i = 0; i < a. size ()-1; i ++) { 3 for (int j = i +1; j < a. size (); j ++) { 4 if (a[i] == a[j]) return false; 5 6 } 7 return...
Consider the following code segment. int[]arr={1, 2, 3, 4, 5, 6, 7, 8}; for(int k=3; k<arr.length-1; R++ arr[k]-arr[k+1]; What are the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8, 8) b. {2, 2, 4, 5, 6, 7, 8, 8} C. {2, 4, 6, 5, 6, 7, 8,8} d. {4, 2, 4, 4, 6, 7, 8, 8) e. {6, 6, 4, 5, 6, 7, 8, 8} int) arr={7, 2.5, 3.0,...
QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...
Problem 1. Select the running time of each function. void print_array (int* A, int n) for (int í 0; i < n; ++i) cout << A[i] << endl; void print_array pairs (int* A, int n) for (inti 0; i < n; ++i) for (int j 0; j < n; ++j) cout << Ai] ALj]< endl; void print_array_start(int* A, int n) for (int i 0; i < 100 ; ++i) cout << A[i] << endl; void print_array_alt (int* A, int n)...
Implement merge sort and merge #include <iostream> using namespace std; void * merge(int arr[], int start1, int end1, int start2, int end2){ int * combined = new int[end2-start1 + 1]; } void mergeSort(int arr[], int start, int end){ //base case: down to 1 item, do nothing //recursive case: //MergeSort(left) //MergeSort(right) //Merge(left, right) int m = (end - start) / 2; if(start==end){ } else { mergeSort(arr, start, m); mergeSort(arr, m+1, end); merge(arr, start, m, m+1, end); } } void fill(int arr[],...
I have the following and it works. The requirement is that the void switchArray function should look like this: switchArray(int *array2D, int numRow, int numCol) but keep the declaration for switchRow function same like this void switchRow(int arr[], int length). Is their any way to change it. (c++ language) void switchArray(int array2D[][4], int numRow, int numCol){ for(int i=0; i < numRow; i++){ switchRow(array2D[i], COL); } } void switchRow(int arr[], int length){ if (length == 0)...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
Complete the definition of the following function void find_two_largest (int 3[), int n, int *largest, int * second_largest); When passed an array a of length n, the function will search a for its largest element and second largest element, storing them in the two variables pointed to by largest and second_largest, respectively. Test your function in the main function.