1. Write C++ statements to do the following: a) Declare string pointer variables
ID: 3587551 • Letter: 1
Question
1. Write C++ statements to do the following: a) Declare string pointer variables named strPtr1 and strPtr2. b) Allocate an anonymous string variable, and make strPtr1 point to it. c Input a string value from the keyboard and store it in the anonymous variable of part (b). d) Display the length of the anonymous string variable of part (b). e Cause strPtr2 to point to the anonymous variable of part (b). f Declare a double pointer variable named doublePt:r g) Allow the user to enter n, the number of values to be processed; then allocate an anonymous array of n double values, and make doublePtr point to it. h) Fill the anonymous array of part (g) with n input values, entered from the keyboard. i) Deallocate the storage of the anonymous variable of part (b) j) Deallocate the anonymous array of part (g).Explanation / Answer
a) string *strPtr1, *strPtr2;
b) string *str = new string();
strPtr1 = str;
c) cin >> *str;
d) cout << (*str).length();
e) strPtr2 = str;
f) double *doublePtr;
g) int n;
cin >> n;
double *arr = new double[n];
doublePtr = arr;
h) for (int i = 0; i < n; i++) {
cin >> arr[i];
}
i) delete str;
j) delete [] arr;
Here is full code:
#include <iostream>
using namespace std;
int main()
{
string *strPtr1, *strPtr2;
string *str = new string();
strPtr1 = str;
cin >> *str;
cout << (*str).length();
strPtr2 = str;
double *doublePtr;
int n;
cin >> n;
double *arr = new double[n];
doublePtr = arr;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
delete str;
delete [] arr;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.