Advertisement
M4ritimeSeeker

PASS Week 9/29 Solutions

Sep 28th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.39 KB | None | 0 0
  1. /*** SOLUTIONS ***/
  2.  
  3.  
  4.  
  5. /** PROBLEM 1A **/
  6.  
  7. #include <stdio.h>
  8. #include <math.h>
  9.  
  10. int main(void)
  11. {
  12.     //Local Declarations
  13.     double base = 0.0;
  14.     double area = 0.0;
  15.  
  16.     //Local Statements
  17.  
  18.     //prompt user
  19.     printf("Enter in a value for the base of a square.\n");
  20.  
  21.     //get input
  22.     scanf("%lf", &base);
  23.  
  24.     //process data
  25.     area = pow(base, 2);
  26.  
  27.     //output information
  28.     printf("The area of a square is %8.2lf\n", area);
  29.  
  30.     return 0;
  31.  
  32. }//end main
  33.  
  34.  
  35.  
  36.  
  37.  
  38. /** PROBLEM 1B **/
  39.  
  40. #include <stdio.h>
  41. #include <math.h>
  42.  
  43. double ComputeAreaOfSquare(double);
  44.  
  45. int main(void)
  46. {
  47.     //Local Declarations
  48.     double base = 0.0;
  49.     double area = 0.0;
  50.  
  51.     //Local Statements
  52.  
  53.     //prompt user
  54.     printf("Enter in a value for the base of a square.\n");
  55.  
  56.     //get input
  57.     scanf("%lf", &base);
  58.  
  59.     //process data by calling your function
  60.     area = ComputeAreaOfSquare(base);
  61.  
  62.     //output information
  63.     printf("The area of a square is %8.2lf\n", area);
  64.  
  65.     return 0;
  66.  
  67. }//end main
  68.  
  69. //You would write function descriptions here
  70. double ComputeAreaOfSquare(double baseIn)
  71. {
  72.     double area = pow(baseIn,2);
  73.     return area;
  74. }//end function
  75.  
  76.  
  77.  
  78.  
  79.  
  80. /** PROBLEM 1C **/
  81.  
  82. #include <stdio.h>
  83. #include <math.h>
  84.  
  85. void PrintInstructions(void);
  86. double GetInput(void);
  87. double ComputeAreaOfSquare(double);
  88. void PrintOutput(double);
  89.  
  90. int main(void)
  91. {
  92.     //Local Declarations
  93.     double base = 0.0;
  94.     double area = 0.0;
  95.  
  96.     //Local Statements
  97.     PrintInstructions();
  98.     base = GetInput();
  99.     area = ComputeAreaOfSquare(base);
  100.     PrintOutput(area);
  101.  
  102.     return 0;
  103.  
  104. }//end main
  105.  
  106. //You would write function descriptions here
  107. void PrintInstructions()
  108. {
  109.     printf("Enter in a value for the base of a square.\n");
  110.     return;
  111. }//end function
  112.  
  113. //You would write function descriptions here
  114. double GetInput()
  115. {
  116.     double baseIn = 0.0;
  117.     scanf("%lf", &baseIn);
  118.     return baseIn;
  119. }//end function
  120.  
  121. //You would write function descriptions here
  122. double ComputeAreaOfSquare(double baseIn)
  123. {
  124.     double area = pow(baseIn, 2);
  125.     return area;
  126. }//end function
  127.  
  128. void PrintOutput(double areaIn)
  129. {
  130.     printf("The area of a square is %8.2lf\n",areaIn);
  131.     return;
  132. }//end function
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. /** PROBLEM 2 **/
  143.  
  144. #include <stdio.h>
  145. #include <math.h>
  146.  
  147. double CalcHypotenuse(double, double);
  148. double CalcArea(double, double);
  149. double CalcPerimeter(double, double, double);
  150.  
  151. int main(void)
  152. {
  153.     //Local Decalarations
  154.     double sideA = 0.0;
  155.     double sideB = 0.0;
  156.     double area = 0.0;
  157.     double perimeter = 0.0;
  158.  
  159.     //Local Statements
  160.     printf("Enter in values for side A and side B of a right triangle: ");
  161.     scanf("%lf %lf", &sideA, &sideB);
  162.  
  163.     area = CalcArea(sideA, sideB);
  164.  
  165.     perimeter = CalcPerimeter(sideA, sideB, CalcHypotenuse(sideA, sideB));
  166.  
  167.     printf("The Area is: %10.2lf and the perimeter is: %10.2lf\n", area, perimeter);
  168.  
  169.     return 0;
  170.  
  171. }//end main
  172.  
  173. double CalcHypotenuse(double sideA, double sideB)
  174. {
  175.     double hypot = 0.0;
  176.     hypot = sqrt( (pow(sideA,2) + pow(sideB,2)) );
  177.     return hypot;
  178. }
  179.  
  180. double CalcArea(double sideA, double sideB)
  181. {
  182.     double area = 0.0;
  183.     area = .5 * (sideA * sideB);
  184.     return area;
  185. }
  186.  
  187. double CalcPerimeter(double sideA, double sideB, double sideC)
  188. {
  189.     double perimeter = sideA + sideB + sideC;
  190.     return perimeter;
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. /** PROBLEM 3 **/
  202.  
  203. /***********************************************
  204. * This program demonstrates seeding the random
  205. * number function.
  206. * By: Larry Snedden
  207. * *********************************************/
  208.  
  209. #include <stdio.h>
  210. #include <stdlib.h>
  211. #include <time.h>
  212.  
  213. int main(void)
  214. {
  215.     //srand(time(NULL));
  216.     printf("%d\n", rand());
  217.     printf("%d\n", rand());
  218.     printf("%d\n", rand());
  219.     printf("%d\n", rand());
  220.     printf("%d\n", rand());
  221.     printf("%d\n", rand());
  222.     printf("%d\n", rand());
  223.     printf("%d\n", rand());
  224.     printf("%d\n", rand());
  225.     printf("%d\n", rand());
  226.  
  227.     return 0;
  228.  
  229. }//end main
  230.  
  231.  
  232.  
  233.  
  234. /** PROBLEM 4 **/
  235.  
  236. /****************************************
  237.  * This program is used to demonstrate *
  238. * The random number functions in *
  239. * stdlib.h *
  240. * By: L. Snedden *
  241. * *************************************/
  242.  
  243. #include <stdio.h>
  244. #include <stdlib.h>
  245. #include <time.h>
  246.  
  247. int main(void)
  248. {
  249.     srand(time(NULL));//This makes the set random
  250.  
  251.     //produce numbers ranging from 10 to 20
  252.     //rand() % ((max - min) +1) + 10; //set the range and then shift to the right
  253.     printf(" %d\n", rand() % 11 + 10);
  254.     printf(" %d\n", rand() % 11 + 10);
  255.     printf(" %d\n", rand() % 11 + 10);
  256.  
  257.     return 0;
  258.  
  259. }//end main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement