Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 17. a, d
- 18.
- void func(char*);
- 19.
- for(int j=0; j<80; j++)
- *s2++ = *s1++;
- 20. b
- 21.
- char* revstr(char*);
- 22.
- char* numptrs[] = { “One”, “Two”, “Three” };
- 23. a, c
- 24. wasted
- 25. memory that is no longer needed
- 26.
- p->exclu();
- 27.
- objarr[7].exclu();
- 28. a, c
- 29.
- float* arr[8];
- 30. b
- 31. 0..9 at one end; 3..* at the other
- 32. b
- 33. false
- 34. a
- Solutions to Exercises
- 1.
- // ex10_1.cpp
- // finds average of numbers typed by user
- #include <iostream>
- using namespace std;
- int main()
- {
- float flarr[100]; //array for numbers
- char ch; //user decision
- int num = 0; //counts numbers input
- do
- {
- Appendix G
- 950
- 24 3087 App G 11/29/01 2:22 PM Page 950
- cout << “Enter number: “; //get numbers from user
- cin >> *(flarr+num++); //until user answers ‘n’
- cout << “ Enter another (y/n)? “;
- cin >> ch;
- }
- while(ch != ‘n’);
- float total = 0.0; //total starts at 0
- for(int k=0; k<num; k++) //add numbers to total
- total += *(flarr+k);
- float average = total / num; //find and display average
- cout << “Average is “ << average << endl;
- return 0;
- }
- 2.
- // ex10_2.cpp
- // member function converts String objects to upper case
- #include <iostream>
- #include <cstring> //for strcpy(), etc
- #include <cctype> //for toupper()
- using namespace std;
- ////////////////////////////////////////////////////////////////
- class String //user-defined string type
- {
- private:
- char* str; //pointer to string
- public:
- String(char* s) //constructor, one arg
- {
- int length = strlen(s); //length of string argument
- str = new char[length+1]; //get memory
- strcpy(str, s); //copy argument to it
- }
- ~String() //destructor
- { delete str; }
- void display() //display the String
- { cout << str; }
- void upit(); //uppercase the String
- };
- //--------------------------------------------------------------
- void String::upit() //uppercase each character
- {
- char* ptrch = str; //pointer to this string
- while( *ptrch ) //until null,
- {
- *ptrch = toupper(*ptrch); //uppercase each character
- Answers to Questions and Exercises
- G
- A
- NSWERS TO
- Q
- UESTIONS AND
- E
- XERCISES
- 951
- 24 3087 App G 11/29/01 2:22 PM Page 951
- ptrch++; //move to next character
- }
- }
- ////////////////////////////////////////////////////////////////
- int main()
- {
- String s1 = “He who laughs last laughs best.”;
- cout << “\ns1=”; //display string
- s1.display();
- s1.upit(); //uppercase string
- cout << “\ns1=”; //display string
- s1.display();
- cout << endl;
- return 0;
- }
- 3.
- // ex10_3.cpp
- // sort an array of pointers to strings
- #include <iostream>
- #include <cstring> //for strcmp(), etc.
- using namespace std;
- const int DAYS = 7; //number of pointers in array
- int main()
- {
- void bsort(char**, int); //prototype
- //array of pointers to char
- char* arrptrs[DAYS] = { “Sunday”, “Monday”, “Tuesday”,
- “Wednesday”, “Thursday”,
- “Friday”, “Saturday” };
- cout << “\nUnsorted:\n”;
- for(int j=0; j<DAYS; j++) //display unsorted strings
- cout << *(arrptrs+j) << endl;
- bsort(arrptrs, DAYS); //sort the strings
- cout << “\nSorted:\n”;
- for(j=0; j<DAYS; j++) //display sorted strings
- cout << *(arrptrs+j) << endl;
- return 0;
- }
- //--------------------------------------------------------------
- void bsort(char** pp, int n) //sort pointers to strings
- {
- Appendix G
- 952
- 24 3087 App G 11/29/01 2:22 PM Page 952
- void order(char**, char**); //prototype
- int j, k; //indexes to array
- for(j=0; j<n-1; j++) //outer loop
- for(k=j+1; k<n; k++) //inner loop starts at outer
- order(pp+j, pp+k); //order the pointer contents
- }
- //--------------------------------------------------------------
- void order(char** pp1, char** pp2) //orders two pointers
- { //if string in 1st is
- if( strcmp(*pp1, *pp2) > 0) //larger than in 2nd,
- {
- char* tempptr = *pp1; //swap the pointers
- *pp1 = *pp2;
- *pp2 = tempptr;
- }
- }
- 4.
- // ex10_4.cpp
- // linked list includes destructor
- #include <iostream>
- using namespace std;
- ////////////////////////////////////////////////////////////////
- struct link //one element of list
- {
- int data; //data item
- link* next; //pointer to next link
- };
- ////////////////////////////////////////////////////////////////
- class linklist //a list of links
- {
- private:
- link* first; //pointer to first link
- public:
- linklist() //no-argument constructor
- { first = NULL; } //no first link
- ~linklist(); //destructor
- void additem(int d); //add data item (one link)
- void display(); //display all links
- };
- //--------------------------------------------------------------
- void linklist::additem(int d) //add data item
- {
- link* newlink = new link; //make a new link
- newlink->data = d; //give it data
- Answers to Questions and Exercises
- G
- A
- NSWERS TO
- Q
- UESTIONS AND
- E
- XERCISES
- 953
- 24 3087 App G 11/29/01 2:22 PM Page 953
- newlink->next = first; //it points to next link
- first = newlink; //now first points to this
- }
- //--------------------------------------------------------------
- void linklist::display() //display all links
- {
- link* current = first; //set ptr to first link
- while( current != NULL ) //quit on last link
- {
- cout << endl << current->data; //print data
- current = current->next; //move to next link
- }
- }
- //--------------------------------------------------------------
- linklist::~linklist() //destructor
- {
- link* current = first; //set ptr to first link
- while( current != NULL ) //quit on last link
- {
- link* temp = current; //save ptr to this link
- current = current->next; //get ptr to next link
- delete temp; //delete this link
- }
- }
- ////////////////////////////////////////////////////////////////
- int main()
- {
- linklist li; //make linked list
- li.additem(25); //add four items to list
- li.additem(36);
- li.additem(49);
- li.additem(64);
- li.display(); //display entire list
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement