Need Help ?

Home / Expert Answers / Other / question-2-20-marks-part-1-c-program-to-insert-a-new-element-in-an-array-4-marks-consider-the-code-g

(Answered): Question 2: (20 Marks) Part 1 -C++ program to insert a new element in an array (4 Marks) Consider t ...




Question 2: (20 Marks) Part 1 -C++ program to insert a new element in an array (4 Marks) Consider the code given below. ExplaPart 2 - C++ program to insert a new element in an array (2 Marks) What will be the values in the final array? Run the code a(3 Marks) Part 4 - C++ program to find out if a string is a palindrome Consider for (len = 0; check[len] != ; ++len); Exp
Question 2: (20 Marks) Part 1 -C++ program to insert a new element in an array (4 Marks) Consider the code given below. Explain the two major steps in terms of logic happening in the highlighted part to insert a new element in an array. // C++ progran to insert a new element in an array include<iostream> Ninclude <cstdlib> for using rand() /srand() #include <ctine // for using (int) time() using namespace std; int main() int i, number, position; const int size - 11; int array[size](); // for randon initialization of array srand((int) time (e)); for (i = 0; i < (size - 1); i++) array[i] - (rand() % 2000) + 1; 1/ just to see what is in the array: cout << "Data in the array is: n"; for (i = 0; i < size; i++) cout << array[1] << ) 1/position to insert number position = 5; // we want to insert a new element at the sth index number = 3; // we want to insert the number 38 for (i size-2; i > position ; --1) array[i+1] = array[i]; > array(position) - number cout << "nnNew Array is: n"; for (i = 0; i < size; i++) cout << array[i] << cout << "n"; return a; 2 Part 2 - C++ program to insert a new element in an array (2 Marks) What will be the values in the final array? Run the code and write your own output. Remember this is a randomly initialized array based on the Unix timestamp. So run your own code. Part 3 - C++ program to find out if a string is a palindrome (3 Marks) Consider the code given below to find out if a string is palindrome or not. Up to how many character will getline allow us to enter in cin.getline(check, 2e, 'n');? Explain you answer. Also, what will happen if we enter more than the allowed characters? #include<iostream> #include <cstring> using namespace std; int main() int i; int j = @; int len -e; int flag - 1; char check [20]; cout << "Enter any string: n"; cin.getline(check, 20, 'n'); for (len = 8; check[len] != ''; ++len); ilen. 1; // to start checking from end of string for (i = 0; i < len / 2; ++i) if (check[1] !- check[j]) flag = ; } if (flag -- 1) cout << "nThe entered string " << check << is a Palindrome. n"; else cout << "nThe entered string * « check << is NOT Palindrome. n"; return; (3 Marks) Part 4 - C++ program to find out if a string is a palindrome Consider for (len = 0; check[len] != ''; ++len); Explain what this line is doing and what will be final value in len? Part 5 - C++ program to find out if a string is a palindrome (1 Marks) If the string to check is RADAR, how many times will the loop execute? Also, is RADAR a palindrome according to the program? Part 6 - C++ program to find out if a string is a palindrome (3 Marks) What will be values of check[j] and check[i] for each iteration of the loop? (Hint: Use debugging. All you need to do is place the debug point at the correct place.) Iteration number State of check[j] and check[i] Part 7 - std::string vs C-style strings (4 Marks) From C-style strings and std::strings, which one should be preferred? Give 4 reasons to prove your answer.


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer to Question 2: (20 Marks) Part 1 -C++ program to insert a new element in an array (4 Marks) Consider the code given below. ...
We have an Answer from Expert

Buy This Answer $4

Place Order