ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 26 May 2017 My Price 9.00

Create a pointer to integer pi

hello, sir 

here's a questions

chapter 9 and chapter 10

 

Chapter 10PointersPart 1Create an inti with a value of 7.Create a pointer to integer pi.Point your pointer pi to your int variable i.Print out your pointer, the address of your pointer and a dereference of your pointer.Create a pointer to your integer pointer ppi.Point it to your pointer to int pi. Print out ppi, the address ofppi, a dereference to ppi and a double dereference to ppi.Part 2Understanding deep vs. shallow copy is essential for a programmer.You will get into severe problemstrying to code if you do not understand it.The essence of the problem is that 2 objects, which should have independent memory storage, accidentlywind up sharing memory.I want you to wrap a character array (and array with 'a','b','c','d','e' is strictly speaking not a string since itdoes not end in '\0') with a class (this is just a class that contains an array) and then properly(in Deep)and improperly (in Shallow) assign memory.Make a class WrapArrayDeep that has a private pointer to char.Your default constructor should allocatean array of size 5.You should have a copy constructor that does a deep copy. (allocates a new array)Your WrapArrayDeep class should start like:class WrapArrayDeep{char *pch;WrapArrayDeep(){pch = new char[5];*pch = 97; //etc.}WrapArrayDeep(WrapArrayDeep wad){// correct copy constructor.}}Make a similar class, WrapArrayShallow, that has an improper copy constructor that causes your copy topoint to the array in the source object. (instead of making a new array, have pch point to the originalarray)Demonstrate the difference between the classes useWrapArrayDeepwad1, *wad2;for the variables holding your WrapArrayDeeps and for WrapArrayShallow:WrapArrayShallow was1, *was2;Be sure to include a destructor in each class – note it must be an ARRAY destructor put a cout in thedestructor showing it was called..In WrapArrayDeep:Use pointer arithmetic to load your array with ASCII values for letters.*pca = 97;*(pca+1) = 98;etc.Use array notation to print your array.

C

hapter 9Use an STL stack to reverse a line of input characters that are read into a string.Your stack shouldcontain the characters of the user's string.Use getline() for input – it needs to be part of your C++ toolinventory.A note on getline:Suppose I am doing the following -This program reverses a string using the STL stackEnter your string of less than 80 characters followed by an ENTERa string inputEnter another? 1 = continue. Anything else to stop1Enter your string of less than 80 characters followed by an ENTERa second stringThis code will not work by simply using the following loop:intgo = 1;cout <<"This program reverses a string using the STL stack"<< endl;while(go == 1){cout <<"Enter your string of less than 80 characters followed by an ENTER"<<endl;char* s = (char*)malloc(80);cin.getline(s,81,'\n');cout << s << endl;cout <<"Enter another? 1 = continue. Anything else to stop"<< endl;cin >> go;}Try it and see what happens!Also note that I never got rid of the memory I allocated with malloc – yourcode must get rid of it. Also, malloc outside the loop for more efficient code.You must use a getchar() (part of the cstdio library) after cin >> go;The reason is that when you enter 1 you also use the Enter key.This is still in the buffer when you hitgetline again!So you will read in '\n'Also note that when you get something off of the STL stack you must use .top() to look at it followed by.pop() to remove it.Example:This program reverses a string using the STL stackEnter your string of less than 80 characters followed by an Enterm&m cheeto mayooyam oteehc m&mEnter another? 1 = continue. Anything else to stop1Enter your string of less than 80 characters followed by an Entercelery lettuce appleelppa ecuttel yrelecEnter another? 1 = continue. Anything else to stop0Press any key to continue . . .

Attachments:

Answers

(11)
Status NEW Posted 26 May 2017 07:05 AM My Price 9.00

-----------

Not Rated(0)