1. Write a program that initialises a vector with the following string values: “what” “book” “is” “that” “you” “are”

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

1. Write a program that initialises a vector with the following string values: “what” “book” “is” “that” “you” “are”

Post by answerhappygod »

1.
Write a program that initialises a vector with the followingstring values: “what” “book” “is” “that” “you” “are” “reading”. Display the contents of the vector on the screen to the user as aquestion and read inthe name of the book the user is reading (you can decide what itwill be). Have theprogram add the name of the book to the vector, word by word. Forexample, if I amreading “How to learn C++”, the program should add the words, “How” “to” “learn” “C++”, one by one to the vector. Display the new vector. Remember to plan your program!
2.
(a) What is a pointer?(b) What is a dereferencing operator?(c) What is the difference between assignment statements p1 =p2; and *p1 = *p2;
(d) What is a dangling pointer?(e) What is a dynamic variable?(f) What is the purpose of the new operator?(g) What is the purpose of the delete operator?(h) What is the freestore (also called the heap)?(i) What is the difference between dynamic variables and automaticvariables?(j) What is a dynamic array?(k) What is the advantage of using dynamic arrays?(l) What is the relationship between pointers and arrays?(m) Explain what is the difference between int* p1, p2; and typedef int* IntPtr;IntPtr p1, p2;
(n) For each of the following, write a single C++ statement thatperforms the identifiedtask. (7) (i) Declare two variables fPtr1 and fPtr2 to be pointers to objectsof typedouble. (ii) Create a dynamic variable to which fPtr1 points. (iii) If the pointer fPtr2 is undefined (i.e. it does not point toany variable), let itpoint to the same variable that fPtr1 points to. (iv) Print the address of the object pointed to byfPtr1. (v) Print the value of the object pointed to by fPtr2.(vi) Release the memory occupied by the dynamic variable to whichfPtr1 points.(vii) Assign null values to the pointers fPtr1 and fPtr2.
(o) Use diagrams similar to displays 9.1 and 9.3 inChapter 9 in Savitch to trace thefollowing C++ code and show the output. (5)
#include <iostream>int main(){int value1 = 20;int value2 = 30;int *ptr2 = &value1;int *ptr1 = &value2;*ptr1 -= *ptr2;ptr1 = ptr2;cout << *ptr1 << endl;cout << *ptr2 << endl;return 0;}(p) Write C++ statements to do the following: (7) (i) Define a pointer type int_ptr for pointer variables thatcontainpointers to int variables. (ii) Declare p2 to be a pointer to an int. (iii) Obtain an integer value nrElements from the userindicating thenumber of elements to allocate.(iv) Dynamically allocate an array of nrElements integers and storeitsaddress in p2.(v) Declare an int array a with 500 elements.(vi) Assume p2 has been initialized and copy the elements of p2 onebyone to the corresponding elements in a.(vii) Free the memory allocated to the variable that p2 is pointingto.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply