Please, explain clearly each line of code with your answers.
Question 1 Consider the following code snippet:
int ctr = 0;
int myarray[3];
for (int i = 0; i < 3; i++)
{
myarray[i] = ctr;
ctr = ctr + i;
}
cout << myarray[2];
What is the output of the code snippet?
Question 2
Consider the following code snippet:
int cnt = 0;
int numarray[2][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
numarray[j][i] = cnt;
cnt++;
}
}
What is the value of numarray[1][2] after the code snippet is executed?
|
2 |
||
|
5 |
||
|
3 |
||
|
4 |
Question 3
Consider the following code snippet:
int numarray[2][3] = { { 3, 2, 3 }};
cout << numarray[0][0];
cout << numarray[1][0];
What is the output of the given code snippet?
|
00 |
||
|
31 |
||
|
30 |
||
|
03 |
Question 4
Which one of the following statements is valid for declaring a vector as a function parameter?
|
A vector declared as a function parameter should be accompanied with its size. |
||
|
A vector declared as a function parameter is a reference parameter by default. |
||
|
An element of the vector cannot be modified in a function when the vector is declaredas a parameter. |
||
|
An element of the vector can be modified if declared using the & sign as a function parameter. |
Question 5
Which one of the following is a correct declaration for a function named passvect with the vector parameter mynum of size 10, so that the function modifies the contents of the actual parameter mynum?
|
void passvect(vector<int>& mynum(10)) |
||
|
void passvect(vector<int> mynum) |
||
|
void passvect(vector<int>& mynum) |
||
|
void passvect(mynum) |
Question 6
What is the output of the following code snippet?
vector<int> num(3);
num.push_back(3);
cout << num.size();
|
1 |
||
|
2 |
||
|
3 |
||
|
4 |
Question 7
What is the result of the following definition of a vector?
vector<double> points;
|
The statement causes a compile-time error as the size of the vector is not defined. |
||
|
The statement creates a vector of size 0. |
||
|
The statement creates a vector with unlimited size. |
||
|
The statement creates a vector of size 1. |
Question 8
Which one of the following code snippets accepts the integer input in a vector named num1 and stores the even integers of num1 in another vector named evennum?
|
vector<int> num1; vector<int> evennum; int data; for (int i = 0; i < 10; i++) { cin >> data; num1.push_back(data); if (num1[i] % 2 == 0) { evennum.push_back(num1[i]); } } |
||
|
vector<int> num1; vector<int> evennum; int data;for (int i = 0; i < 10; i++) { cin >> data; num1.push_back(data); if (num1[i] % 2 != 0) { evennum.push_back(num1[i]); } } |
||
|
vector<int> num1; vector<int> evennum; int data; for int i = 0; i < 10; i++) { cin >> data; num1.push_back(data); if (num1[i] % 2 != 0) { evennum[i] = num1[i]); } } |
||
|
vector<int> num1; vector<int> evennum; int data; for (int i = 0; i < 10; i++) { cin >> data; num1[i] = data; if (num1[i] % 2 == 0) { evennum[i] = num1[i]); } } |
Question 9
Consider the following code snippet:
vector<int> num(3);
num[0] = 1;
num[1] = 2;
num[2] = 3;
num.pop_back();
Which element or elements of the vector are removed by the pop_back() function in the given code snippet?
|
The element with index 0 |
||
|
The element with index 1 |
||
|
The element with index 2 |
||
|
All the elements |
Question 10
When a compiler encounters a function parameter for a single-subscripted array of the form int a[], it converts the parameter to:
|
int a |
||
|
int &a |
||
|
int *a |
||
|
No conversion is necessary. |
Question 11
Given that k is an integer array starting at location 2000, kPtr is a pointer to k and each integer is stored in 4 bytes of memory, what location does kPtr + 3 point to?
|
2003 |
||
|
2006 |
||
|
2012 |
||
|
2024 |
Question 12
What does the following statement declare?
int *countPtr, count;
|
Two int variables. |
||
|
One pointer to an int and one int variable. |
||
|
Two pointers to ints. |
||
|
The declaration is invalid. |
Question 13
Which of the function prototypes below is correct for a function which processes a 2D array?
|
int max2d(int a[][], int rows); |
||
|
int max2d(int a[][], int rows, int columns); |
||
|
int max2d(int a[][5], int rows, int columns); |
||
|
int max2d(int a[5][], int rows; |
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thanks.
1).

2)

3)

4)
Correct choice is below...
An element of the vector can be modified if declared using the & sign as a function parameter.
NOTE that::: As per Chegg policy only first 4 subparts is allowed to answer.
Please, explain clearly each line of code with your answers. Question 1 Consider the following code...
Please explain clearly your answers (step by step): 1) What is the output of the following code snippet? int my_fun (int A[], int size) { int result = 0; for (int i = 0; i < size; i++) { result = result + A[i]; } return result; } int main() { int myarr[10] = { 5, 6, 10, 1, 2, 4, 8, 1, 2, 1 }; for (int i = 0; i < 3; i++) { cout << my_fun(myarr, i);...
Please, explain your answer clearly (step-by-step) What output does this loop generate? int j = 1; do { int value = j * 2; j++; cout << value << ", "; } while (j <= 5); What is the output of the following code snippet? int my_fun (int A[], int size) { int result = 0; for (int i = 0; i < size; i++) { result = result + A[i]; } return result; } int main() { int...
1.The following statement gets an element from position 4 in an array named myArray: x = myArray[4]; What is the equivalent operation using an array list named list.? A x = list.get(); B x = list.get(4); C x = list.get[4]; D x = list[4]; 2.Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 &&...
In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task. Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop int cnt = 1; do { cnt += 3; } while (cnt < 25); cout << cnt; It runs ________ times ...
Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){ int rows = 5; int cols = 5; int x; int** arr = new int[rows][cols] cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x]; return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){ int rows; int cols; int x;...
1. What would be the output of the following lines of code: int num = 2; switch(num){ case 1: cout << "1"; case 2: cout << "2"; case 3: cout << "3"; case 4: cout << "4"; } 2. What would be the output of the following code: char op = '*'; switch(op){ case '+': cout << "Addition"; break; case '-': cout << "Subtraction"; break; case '/': cout << "Division"; break; default: cout << "Invalid"; } 3. What would be...
may i ask for help with this c++ problem?
this is the code i have for assignment 4 question 2:
#include<iostream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
int main()
{
string inputStr;
stack <int> numberStack;
cout<<"Enter your expression::";
getline(cin,inputStr);
int len=inputStr.length();
stringstream inputStream(inputStr);
string word;
int val,num1,num2;
while (inputStream >> word)
{
//cout << word << endl;
if(word[0] != '+'&& word[0] != '-' && word[0] !=
'*')
{
val=stoi(word);
numberStack.push(val);
// cout<<"Val:"<<val<<endl;
}
else if(word[0]=='+')
{
num1=numberStack.top();
numberStack.pop();
num2=numberStack.top();
numberStack.pop();...
Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....
C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...