Advertisement
Ccuevas3410

LAB 5 CODE

Mar 27th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. 5.3
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. cout << "CRISTIAN CUEVAS \n";
  8. cout << "\nActivity 1 \n==========\n";
  9. // Change the following do-while loop to a while loop. //DONE
  10. int inputNum;
  11. while(inputNum != 0){
  12. cout << "Enter a number (or 0 to quit): ";
  13. cin >> inputNum;
  14.  
  15. }
  16.  
  17. cout << "\nActivity 2 \n==========\n";
  18. // Change the following while loop to a do-while loop. //DONE
  19. char doAgain = 'y';
  20.  
  21. do {
  22. cout << "Do you want to loop again? (y/n) ";
  23. cin >> doAgain;
  24. }
  25.  
  26. while(doAgain == 'Y' || doAgain == 'y');
  27.  
  28.  
  29. cout << "\nActivity 3 \n==========\n";
  30. // Change the following while loop to a for loop. //DONE
  31. int count = 0;
  32.  
  33. for (count=0;count<5;count++){
  34. cout << "Count is " << count << endl;
  35. }
  36.  
  37.  
  38. cout << "\nActivity 4 \n==========\n";
  39. // Change the following for loop to a while loop.
  40. int x=5;
  41. while(x>0){
  42.  
  43. cout << x << " seconds to go. \n";
  44. x--;
  45. }
  46.  
  47.  
  48.  
  49.  
  50. cout << "\nActivity 5 \n==========\n";
  51. // Make the following changes to the code below that uses nested loops:
  52. // 1. The code is supposed to print 3 lines with a $ and 5 stars on
  53. // each line, but it contains a logic error. Find and fix the error.
  54. // 2. Then revise the code to follow each $ with just 4 stars, like this:
  55. // $****
  56. // $****
  57. // $****
  58. // 3. Change the two loop control variable names to be more descriptive.
  59. for (int counterS = 1 ; counterS <= 3; counterS++)
  60. {
  61. cout << '$';
  62.  
  63. for (int counterStar = 1; counterStar<= 4; counterStar++)
  64. cout << '*';
  65. cout<< endl;
  66. }
  67. return 0;
  68. }
  69.  
  70.  
  71.  
  72. 5.4
  73. // Lab 5 - cookies.cpp
  74. // This program finds the average number of boxes of cookies
  75. // sold by the children in a particular scout troop.
  76. // It illustrates the use of a counter, an accumulator,
  77. // and an end sentinel.
  78. // CRISTIAN CUEVAS
  79.  
  80. #include <iostream>
  81. using namespace std;
  82.  
  83. int main()
  84. {
  85. int numBoxes, // Number of boxes of cookies sold by one child
  86. totalBoxes, // Accumulates total boxes sold by the entire troop
  87. numSeller; // Counts the number of children selling cookies
  88.  
  89. double averageBoxes; // Average number of boxes sold per child
  90. // WRITE CODE TO INITIALIZE THE totalBoxes ACCUMLATOR TO 0 AND
  91. // THE numSeller COUNTER TO 1.
  92. totalBoxes = 0;
  93. numSeller = 1;
  94.  
  95.  
  96. cout << " **** Cookie Sales Information **** \n\n";
  97.  
  98. // Get the first input
  99. cout << "Enter number of boxes of cookies sold by seller " << numSeller
  100. << " (or -1 to quit): ";
  101. cin >> numBoxes;
  102.  
  103.  
  104. // WRITE CODE TO START A while LOOP THAT LOOPS WHILE numBoxes
  105. // IS NOT EQUAL TO -1, THE SENTINEL VALUE.
  106.  
  107. while(numBoxes!= -1)
  108. {
  109.  
  110.  
  111. // WRITE CODE TO ADD numBoxes TO THE totalBoxes ACCUMULATOR.
  112. totalBoxes += numBoxes;
  113.  
  114. // WRITE CODE TO ADD 1 TO THE numSeller COUNTER.
  115. numSeller++;
  116.  
  117.  
  118. // WRITE CODE TO PROMPT FOR AND INPUT THE NUMBER OF BOXES
  119. // SOLD BY THE NEXT SELLER.
  120. cout << "Enter the boxes from the next seller"<< endl;
  121. cin>> numBoxes;
  122.  
  123. while (numBoxes < -1){
  124.  
  125. cout<< "Wrong number \n";
  126.  
  127. cout << "Enter the boxes from the next seller"<< endl;
  128. cin>> numBoxes;}
  129. }
  130. // WHEN THE LOOP IS EXITED, THE VALUE STORED IN THE numSeller COUNTER
  131. // WILL BE ONE MORE THAN THE ACTUAL NUMBER OF SELLERS. SO WRITE CODE
  132. // TO ADJUST IT TO THE ACTUAL NUMBER OF SELLERS.
  133.  
  134. numSeller = numSeller-1;
  135.  
  136. if (numSeller == 0) // If true, -1 was the very first entry
  137. cout << "\nNo boxes were sold.\n";
  138. else
  139. { // WRITE CODE TO ASSIGN averageBoxes THE COMPUTED AVERAGE NUMBER
  140. // OF BOXES SOLD PER SELLER.
  141.  
  142.  
  143. // WRITE CODE TO PRINT OUT THE NUMBER OF SELLERS AND AVERAGE NUMBER
  144. // OF BOXES SOLD PER SELLER.
  145.  
  146. averageBoxes = (double)totalBoxes/numSeller;
  147. cout << "The average number of boxes sold by the " << numSeller << " sellers was "<< averageBoxes <<endl;
  148.  
  149. }
  150. return 0;
  151. }
  152.  
  153. 5.5
  154.  
  155.  
  156. #include <iostream>
  157.  
  158. #include <string>
  159. #include <cmath>
  160. using namespace std;
  161.  
  162. int main()
  163. {
  164. // DEFINE THE NAMED CONSTANT PI HERE AND SET ITS VALUE TO 3.14159
  165.  
  166. double pi = 3.14159;
  167. int choice;
  168. double radius;
  169.  
  170.  
  171.  
  172. do{
  173. cout << "Program to calculate areas of objects\n" << "1 --square\n" << "2 --circle\n" << "3--right triangle\n" << "4--quit" << endl;
  174. choice=0;
  175. cout << "ENTER YOUR CHOICE " << endl;
  176.  
  177. cin >> choice;
  178.  
  179. switch (choice)
  180. {
  181. case 1:
  182.  
  183. cout << "Radius of square: ";
  184. cin >> radius;
  185. cout << "Area = " << pow(radius, 2);
  186. cout<< "\n\n";
  187.  
  188.  
  189. break;
  190.  
  191. case 2:
  192.  
  193. cout << "Radius of circle: ";
  194. cin >> radius;
  195. cout << "Area = " << pi * pow(radius,2);
  196. cout<< "\n\n";
  197.  
  198. break;
  199.  
  200. case 3:
  201.  
  202. cout << "Radius of right triangle: ";
  203. double a, b;
  204. cout << "Enter a and b separated by a space" << endl;
  205. cin >> a >> b;
  206. cout << "Area = " << (a*b) / 2 << endl;
  207. cout<< "\n\n";
  208.  
  209.  
  210. break;
  211. case 4:
  212.  
  213. cout << " BYE ";
  214. return 0;
  215.  
  216. break;
  217. default:
  218.  
  219.  
  220. cout << "WRONG CHOICE SELECTED, TRY AGAIN";
  221.  
  222.  
  223. break;
  224.  
  225. }
  226. } while(choice!=4);
  227. 5.6
  228. #include <iostream>
  229. #include <cmath>
  230. #include <string>
  231.  
  232.  
  233. using namespace std;
  234.  
  235.  
  236.  
  237. int main()
  238. {
  239. int value,
  240. denom,
  241. finalDenom;
  242. double sum = 0.0;
  243.  
  244.  
  245. cout << "CRISTIAN CUEVAS \n";
  246. cout << "This program sums the series 1/2^1 + 1/2^2 + 1/2^3 + . . . + 1/2^n \n";
  247.  
  248.  
  249.  
  250.  
  251. cout << "\n\Enter your value for the multiple ";
  252. cin >> value;
  253.  
  254.  
  255. while (value < 2 || value > 10){
  256. cout << "Invalid input. "
  257. << " Enter a positive integer between 2 and 10: ";
  258. cin >> value;
  259. }
  260. cout << endl;
  261.  
  262.  
  263. sum = 0.0;
  264.  
  265. // Compute the denominator of the final term
  266. finalDenom = int (pow(2.0, value));
  267.  
  268. // Loop once for each term
  269. for (denom = 2; denom <= finalDenom; denom *=2)
  270. {
  271. // Print the term
  272. cout << "1/" << denom;
  273. if (denom < finalDenom)
  274. cout << " + ";
  275. else
  276. cout << " = ";
  277.  
  278. // Add the value of the term to the accumulator
  279. sum = sum + 1.0 / denom;
  280. }
  281. // Print the answer
  282. cout << sum << endl << endl;
  283.  
  284. cout << "GOOD BYE";
  285.  
  286. return 0;
  287. }
  288.  
  289.  
  290.  
  291. return 0;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement