Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. 17. a, d
  2. 18.
  3. void func(char*);
  4. 19.
  5. for(int j=0; j<80; j++)
  6. *s2++ = *s1++;
  7. 20. b
  8. 21.
  9. char* revstr(char*);
  10. 22.
  11. char* numptrs[] = { “One”, “Two”, “Three” };
  12. 23. a, c
  13. 24. wasted
  14. 25. memory that is no longer needed
  15. 26.
  16. p->exclu();
  17. 27.
  18. objarr[7].exclu();
  19. 28. a, c
  20. 29.
  21. float* arr[8];
  22. 30. b
  23. 31. 0..9 at one end; 3..* at the other
  24. 32. b
  25. 33. false
  26. 34. a
  27. Solutions to Exercises
  28. 1.
  29. // ex10_1.cpp
  30. // finds average of numbers typed by user
  31. #include <iostream>
  32. using namespace std;
  33. int main()
  34. {
  35. float flarr[100]; //array for numbers
  36. char ch; //user decision
  37. int num = 0; //counts numbers input
  38. do
  39. {
  40. Appendix G
  41. 950
  42. 24 3087 App G 11/29/01 2:22 PM Page 950
  43. cout << “Enter number: “; //get numbers from user
  44. cin >> *(flarr+num++); //until user answers ‘n’
  45. cout << “ Enter another (y/n)? “;
  46. cin >> ch;
  47. }
  48. while(ch != ‘n’);
  49. float total = 0.0; //total starts at 0
  50. for(int k=0; k<num; k++) //add numbers to total
  51. total += *(flarr+k);
  52. float average = total / num; //find and display average
  53. cout << “Average is “ << average << endl;
  54. return 0;
  55. }
  56. 2.
  57. // ex10_2.cpp
  58. // member function converts String objects to upper case
  59. #include <iostream>
  60. #include <cstring> //for strcpy(), etc
  61. #include <cctype> //for toupper()
  62. using namespace std;
  63. ////////////////////////////////////////////////////////////////
  64. class String //user-defined string type
  65. {
  66. private:
  67. char* str; //pointer to string
  68. public:
  69. String(char* s) //constructor, one arg
  70. {
  71. int length = strlen(s); //length of string argument
  72. str = new char[length+1]; //get memory
  73. strcpy(str, s); //copy argument to it
  74. }
  75. ~String() //destructor
  76. { delete str; }
  77. void display() //display the String
  78. { cout << str; }
  79. void upit(); //uppercase the String
  80. };
  81. //--------------------------------------------------------------
  82. void String::upit() //uppercase each character
  83. {
  84. char* ptrch = str; //pointer to this string
  85. while( *ptrch ) //until null,
  86. {
  87. *ptrch = toupper(*ptrch); //uppercase each character
  88. Answers to Questions and Exercises
  89. G
  90. A
  91. NSWERS TO
  92. Q
  93. UESTIONS AND
  94. E
  95. XERCISES
  96. 951
  97. 24 3087 App G 11/29/01 2:22 PM Page 951
  98. ptrch++; //move to next character
  99. }
  100. }
  101. ////////////////////////////////////////////////////////////////
  102. int main()
  103. {
  104. String s1 = “He who laughs last laughs best.”;
  105. cout << “\ns1=”; //display string
  106. s1.display();
  107. s1.upit(); //uppercase string
  108. cout << “\ns1=”; //display string
  109. s1.display();
  110. cout << endl;
  111. return 0;
  112. }
  113. 3.
  114. // ex10_3.cpp
  115. // sort an array of pointers to strings
  116. #include <iostream>
  117. #include <cstring> //for strcmp(), etc.
  118. using namespace std;
  119. const int DAYS = 7; //number of pointers in array
  120. int main()
  121. {
  122. void bsort(char**, int); //prototype
  123. //array of pointers to char
  124. char* arrptrs[DAYS] = { “Sunday”, “Monday”, “Tuesday”,
  125. “Wednesday”, “Thursday”,
  126. “Friday”, “Saturday” };
  127. cout << “\nUnsorted:\n”;
  128. for(int j=0; j<DAYS; j++) //display unsorted strings
  129. cout << *(arrptrs+j) << endl;
  130. bsort(arrptrs, DAYS); //sort the strings
  131. cout << “\nSorted:\n”;
  132. for(j=0; j<DAYS; j++) //display sorted strings
  133. cout << *(arrptrs+j) << endl;
  134. return 0;
  135. }
  136. //--------------------------------------------------------------
  137. void bsort(char** pp, int n) //sort pointers to strings
  138. {
  139. Appendix G
  140. 952
  141. 24 3087 App G 11/29/01 2:22 PM Page 952
  142. void order(char**, char**); //prototype
  143. int j, k; //indexes to array
  144. for(j=0; j<n-1; j++) //outer loop
  145. for(k=j+1; k<n; k++) //inner loop starts at outer
  146. order(pp+j, pp+k); //order the pointer contents
  147. }
  148. //--------------------------------------------------------------
  149. void order(char** pp1, char** pp2) //orders two pointers
  150. { //if string in 1st is
  151. if( strcmp(*pp1, *pp2) > 0) //larger than in 2nd,
  152. {
  153. char* tempptr = *pp1; //swap the pointers
  154. *pp1 = *pp2;
  155. *pp2 = tempptr;
  156. }
  157. }
  158. 4.
  159. // ex10_4.cpp
  160. // linked list includes destructor
  161. #include <iostream>
  162. using namespace std;
  163. ////////////////////////////////////////////////////////////////
  164. struct link //one element of list
  165. {
  166. int data; //data item
  167. link* next; //pointer to next link
  168. };
  169. ////////////////////////////////////////////////////////////////
  170. class linklist //a list of links
  171. {
  172. private:
  173. link* first; //pointer to first link
  174. public:
  175. linklist() //no-argument constructor
  176. { first = NULL; } //no first link
  177. ~linklist(); //destructor
  178. void additem(int d); //add data item (one link)
  179. void display(); //display all links
  180. };
  181. //--------------------------------------------------------------
  182. void linklist::additem(int d) //add data item
  183. {
  184. link* newlink = new link; //make a new link
  185. newlink->data = d; //give it data
  186. Answers to Questions and Exercises
  187. G
  188. A
  189. NSWERS TO
  190. Q
  191. UESTIONS AND
  192. E
  193. XERCISES
  194. 953
  195. 24 3087 App G 11/29/01 2:22 PM Page 953
  196. newlink->next = first; //it points to next link
  197. first = newlink; //now first points to this
  198. }
  199. //--------------------------------------------------------------
  200. void linklist::display() //display all links
  201. {
  202. link* current = first; //set ptr to first link
  203. while( current != NULL ) //quit on last link
  204. {
  205. cout << endl << current->data; //print data
  206. current = current->next; //move to next link
  207. }
  208. }
  209. //--------------------------------------------------------------
  210. linklist::~linklist() //destructor
  211. {
  212. link* current = first; //set ptr to first link
  213. while( current != NULL ) //quit on last link
  214. {
  215. link* temp = current; //save ptr to this link
  216. current = current->next; //get ptr to next link
  217. delete temp; //delete this link
  218. }
  219. }
  220. ////////////////////////////////////////////////////////////////
  221. int main()
  222. {
  223. linklist li; //make linked list
  224. li.additem(25); //add four items to list
  225. li.additem(36);
  226. li.additem(49);
  227. li.additem(64);
  228. li.display(); //display entire list
  229. cout << endl;
  230. return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement