Advertisement
akosiraff

10 CPP Programs

Oct 16th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/10-cpp-programs/
  3. Exercise P1.1. Write a program that prints the message, “Hello, my name is Hal!”
  4. Then, on a new line, the program should print the message “What would you like
  5. me to do?” Then it’s the user’s turn to type in an input. You haven’t yet learned how
  6. to do it—just use the following lines of code:
  7. string user_input;
  8. getline(cin, user_input);
  9. Finally, the program should ignore the user input and print the message “I am sorry,
  10. I cannot do that.”
  11. This program uses the string data type. To access this feature, you must place the
  12. line
  13. #include
  14. before the main function.
  15. Here is a typical program run. The user input is printed in boldface.
  16. Hello, my name is Hal!
  17. What would you like me to do?
  18. Clean up my room
  19. I am sorry, I cannot do that.
  20. When running the program, remember to hit the Enter key after typing the last
  21. word of the input line.
  22. P1.3. Write a program that computes the sum of the first ten positive integers,
  23. 1 + 2 + … + 10. Hint: Write a program of the form
  24. int main()
  25. {
  26. cout <<
  27. return 0;
  28. }
  29. Exercise P2.3. Write a program that prompts the user for two integers and then
  30. prints
  31. • The sum
  32. • The difference
  33. • The product
  34. • The average
  35. • The distance (absolute value of the difference)
  36. • The maximum (the larger of the two)
  37. • The minimum (the smaller of the two)
  38. Hint: The max and min functions are defined in the algorithm header.
  39. Exercise P2.30. Your boss, Juliet Jones, is getting married and decides to change her
  40. name. Complete the following program so that you can type in the new name for
  41. the boss:
  42. int main()
  43. {
  44. Employee boss("XXXXX, XXXXXt", 45000.00);
  45. // Your code goes here; leave the code above and below unchanged
  46. cout << "Name: " << boss.get_name() << "\n";
  47. cout << "Salary: " << boss.get_salary() << "\n";
  48. return 0;
  49. }
  50. The problem is that there is no set_name member function for the Employee class.
  51. Hint: Make a new object of type Employee with the new name and the same salary.
  52. Then assign the new object to boss.
  53. Exercise P3.3. Write a program that reads in three floating-point numbers and prints
  54. the largest of the three inputs. For example:
  55. Please enter three numbers: 4 9 2.5
  56. The largest number is XXXXX
  57. Exercise P3.16. Write a program that first asks the user to type in today’s exchange
  58. rate between U.S. dollars and Japanese yen, then reads U.S. dollar values and converts
  59. each to Japanese yen. Use 0 as the sentinel value to denote the end of dollar
  60. inputs. Then the program reads a sequence of yen amounts and converts them to
  61. dollars. The second sequence is terminated by the end of the input file.
  62. Exercise P4.2. Write a procedure void sort2(int& a, int& b) that swaps the values
  63. of a and b if a is greater than b and otherwise leaves a and b unchanged. For example,
  64. int u = 2;
  65. int v = 3;
  66. int w = 4;
  67. int x = 1;
  68. sort2(u, v); // u is still 2, v is still 3
  69. sort2(w, x); // w is now 1, x is now 4
  70. Exercise P4.8. Write a function
  71. double get_double(string prompt)
  72. that displays the prompt string, followed by a space, reads a floating-point number
  73. in, and returns it. (In other words, write a console version of cwin.get_double.)
  74. Here is a typical usage:
  75. salary = get_double("Please enter your salary:");
  76. perc_raise =
  77. get_double("What percentage raise would you like?");
  78. If there is an input error, abort the program by calling exit(1). (You will see in
  79. Chapter 5 how to improve this behavior.)
  80. Exercise P4.15. Consider the following buggy function:
  81. Employee read_employee()
  82. {
  83. cout << "Please enter the name: ";
  84. string name;
  85. getline(cin, name);
  86. cout <> salary;
  87. Employee r(name, salary);
  88. return r;
  89. }
  90. When you call this function once, it works fine. When you call it again in the same
  91. program, it won’t return the second employee record correctly. Write a test harness
  92. that verifies the problem. Then step through the function. Inspect the contents of
  93. the string name and the Employee object r after the second call. What values do you
  94. get?
  95. Exercise P5.1. Implement all member functions of the following class:
  96. class Person
  97. {
  98. public:
  99. Person();
  100. Person(string pname, int page);
  101. void get_name() const;
  102. void get_age() const;
  103. private:
  104. string name;
  105. int age; // 0 if unknown
  106. };
  107.  
  108. Download: http://solutionzip.com/downloads/10-cpp-programs/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement