[cpp] Tutorial 3 Solutions

Viewer

copydownloadembedprintName: Tutorial 3 Solutions
  1. //Exercise 1
  2. #include <iostream>
  3.  
  4. using namespace std;;
  5.  
  6.  
  7.  
  8. int main()
  9. {
  10.     const int min_height = 48;
  11.  
  12.  
  13.     int height = 60;
  14.     bool pregnant = true;
  15.  
  16.  
  17.     if(height < min_height || pregnant){
  18.         std::cout<<"Sorry"<<endl;
  19.     }else{
  20.         std::cout<<"Okay! have fun"<<endl;
  21.     }
  22.  
  23.     return 0;
  24. }
  25.  
  26.  
  27.  
  28. //Exercise 2
  29.  
  30. #include <iostream>
  31.  
  32. using namespace std;;
  33.  
  34.  
  35.  
  36. int main()
  37. {
  38.     int x = 60;
  39.     int y = 55;
  40.     int z = 63;
  41.  
  42.  
  43.     int max;
  44.  
  45.  
  46.     if((>= y) && (>= z))
  47.         max = x;
  48.     else if ((>= x) && (>= z))
  49.         max = y;
  50.     else
  51.         max = z;
  52.  
  53.      std::cout<<"Maximum integer is ";
  54.      std::cout<< max << endl;
  55.  
  56.      return 0;
  57. }
  58.  
  59.  
  60. //Exercise 3
  61.  
  62. #include <iostream>
  63.  
  64. using namespace std;;
  65.  
  66.  
  67.  
  68. int main()
  69. {
  70.     const int TRUE = 1;
  71.  
  72.     const int FALSE = 0;
  73.  
  74.  
  75.     int honors; // true if the student is an honors student
  76.  
  77.     int awards; // true if the student has won an academic award
  78.  
  79.     int good_student; // true under certain circumstances...
  80.  
  81.     if (honors == TRUE && awards == TRUE) {
  82.         good_student = TRUE;
  83.     }else{
  84.         good_student = FALSE;
  85.     }
  86.     
  87.     return 0;
  88. }
  89.  
  90.  
  91. //Excercise 4
  92. 'c' - "Medium Low"
  93. 'x' - "Off the Scale"
  94. 'D' - "Low"
  95.  
  96.  
  97. //Exercise 5
  98. #include <iostream>
  99.  
  100. using namespace std;;
  101.  
  102.  
  103.  
  104. int main()
  105. {
  106.  
  107.         double first_num;
  108.         double second_num;
  109.         int attempts  = 0;
  110.         double result;
  111.  
  112.         char code;
  113.  
  114.  
  115.        do {
  116.  
  117.            cout << "Enter first number" << endl;
  118.            cin >> first_num;
  119.  
  120.            cout << "Enter second number" << endl;
  121.            cin >> second_num;
  122.  
  123.  
  124.            cout << "Enter code of operation" << endl;
  125.            cin >> code;
  126.  
  127.            switch (code) {
  128.  
  129.                case '+' :
  130.                    result = first_num + second_num;
  131.                    cout << "Final result = " ;
  132.                    cout << result <<endl;
  133.                    break;
  134.  
  135.                case '-' :
  136.                    result = second_num - first_num;
  137.                    cout << "Final result = ";
  138.                    cout << result <<endl;
  139.                    break;
  140.  
  141.                case '*' :
  142.                    result = first_num * second_num;
  143.                    cout << "Final result = ";
  144.                    cout << result <<endl;
  145.                    break;
  146.  
  147.                case '/' :
  148.                    result = second_num / first_num;
  149.                    cout << "Final result = ";
  150.                    cout << result <<endl;
  151.                    break;
  152.  
  153.  
  154.                default :
  155.                    attempts++;
  156.                    cout << "Please re-enter operation" << endl;
  157.  
  158.            }
  159.        } while(attempts < 3);
  160.  
  161.         cout << "The code was not entered properly, the program has to terminate" << endl;
  162.  
  163.  
  164.     return 0;
  165. }
  166.  
  167. //Exercise 6
  168. output = "Cool is all we got"
  169.  
  170. int main()
  171. {
  172.     int x = 5;
  173.     int z = 22;
  174.     cout << "Cool";
  175.  
  176.     if (> z){
  177.         if (< 50) {
  178.             cout << " dudes";
  179.         }
  180.         else{
  181.             cout << " beans";
  182.         }
  183.       }else {
  184.         cout << " is all we got";
  185.     }
  186.     return 0;
  187. }

Editor

You can edit this paste and save as new:


File Description
  • Tutorial 3 Solutions
  • Paste Code
  • 27 Jan-2021
  • 3.14 Kb
You can Share it: