Advertisement
M4ritimeSeeker

PASS Week 3/3

Feb 28th, 2021 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. PROBLEM #1
  2.  
  3. Create a program using the skeleton below that will prompt the user to enter in two integer
  4. values: nbr1 and nbr2. Use an if/else construct to print out if nbr1 <= nbr2 or else if nbr1 > nbr2.
  5.  
  6. In your if/else statements, print out the statements verbatim like this:
  7.     printf("nbr1 <= nbr2\n");
  8.  
  9.  
  10. /*** SKELETON CODE ***/
  11.  
  12. #include <stdio.h>
  13.  
  14. int main (void)
  15. {
  16.  
  17.     // Local Declarations
  18.     int nbr1;
  19.     int nbr2;
  20.  
  21.     // Statements
  22.     printf("Please enter two integers: ");
  23.     scanf ("%d %d", &nbr1, &nbr2);
  24.  
  25.     //Your code here
  26.  
  27.     return 0;
  28. } // End main
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. PROBLEM #2
  39.  
  40. Modify the program from Problem 1 to use NESTED if/else blocks of code to determine in
  41. the case if nbr1 <= nbr2, the case is also true if nbr1 < nbr2 or else nbr1 == nbr2
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. PROBLEM #3
  58.  
  59. In this problem, we will be calculating a students letter grade by using the percentage.
  60. Treat the percentage as an integer between 0-100.
  61. Using the skeleton code below complete a SWITCH statement in the
  62. function that takes the score value and returns the char value that
  63. corresponds to the letter grade earned. 90 to 100 is an ‘A’, 80 to 89
  64. is a ‘B’ etc. Most of the code is given to you. You just have to
  65. write the switch statement itself.
  66.  
  67. HINT: You can narrow the number of cases by changing the
  68. value from a 93 to a 9 or a 26 to a 2 by using integer division. What
  69. if the user supplies a floating point?
  70.  
  71. /*** SKELETON CODE ***/
  72.  
  73. #include <stdio.h>
  74.  
  75. // Function Declarations
  76. char scoreToGrade (float score);
  77.  
  78. int main (void)
  79. {
  80.     // Local Declarations
  81.     float score;
  82.     char grade;
  83.  
  84.     // Local Statements
  85.     printf("Enter the test score (0-100): ");
  86.     scanf ("%f", &score);
  87.  
  88.     grade = scoreToGrade (score);
  89.  
  90.     printf("The grade is: %c\n", grade);
  91.  
  92.     return 0;
  93. } // end main
  94.  
  95.  
  96. /* ==================== scoreToGrade ===================
  97. This function calculates the letter grade for a score.
  98.  Pre the parameter score
  99.  Post returns the grade
  100. */
  101. char scoreToGrade (float score)
  102. {
  103.     // Local Declarations
  104.     char grade;
  105.     int temp;
  106.  
  107.     // Statements
  108.     temp = (int)(score / 10); //Without the cast it would still be narrowed to an integer
  109.  
  110.     //YOUR SWITCH STATEMENT HERE
  111.  
  112.     return grade;
  113. }//end function ScoreToGrade
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. PROBLEM #4
  130.  
  131. Using the random number generator compose a program that seeds the
  132. random number generator using system time. Use the random number
  133. function to returns a value within a range of 1 - 3. Use a switch
  134. statement to determine the following. If the number returned is 1 print
  135. out “The computer chose Rock.”, if it’s 2 print out “The computer chose
  136. Paper.”, if it’s 3 print out “The computer chose Scissors.”
  137.  
  138. Set the range: range = (max value - min value) + 1;
  139.  
  140.  
  141.  
  142. /*** SKELETON CODE ***/
  143.  
  144. #include<stdio.h>
  145. #include<time.h>
  146.  
  147. #define MAX 3
  148. #define MIN 1
  149.  
  150. int main(void)
  151. {
  152.    
  153.  
  154.     int range = ;
  155.     int compChoice = ;
  156.  
  157.     switch(compChoice)
  158.     {
  159.        
  160.     }
  161.  
  162.     printf("Good bye!\n");
  163.     return 0;
  164. }//end main
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. BONUS PROBLEM
  178.  
  179. Using nested if/else conditions, write a program that will prompt for then take three integer
  180. values as input. Your if/else construct should determine which value is the median value of
  181. the three. If you use a comparison operator ‘==’ make sure not to confuse it with the ‘=
  182. assignment operator. It might make it easier to draw the various conditions using pencil and
  183. paper. This is a simple example of where a flowchart design tool could help verify your logic.
  184.  
  185. /*** SKELETON CODE ***/
  186.  
  187. #include <stdio.h>
  188.  
  189. int main(void)
  190. {
  191.     int a, b, c, median;
  192.  
  193.     printf("Enter in 3 integer values.\n");
  194.     scanf("%d", &a);
  195.     scanf("%d", &b);
  196.     scanf("%d", &c);
  197.  
  198.     //YOUR CODE HERE
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement