CSIT 121 - Exercises

  1. Write a program to read in two real numbers and to display their minimum (i.e. the smallest).
  2. Solution:
    #include <iostream>
    using namespace std;
    int main()
    {
            float a, b;
            cout << "Enter two real numbers: ";
            cin >> a >> b;
            if ( a < b )
                    cout << "The minimum is: " << a << endl;
            else // means a >= b
                    cout << "The minimum is: " << b << endl;
            return 0;
    }                
    

  3. Write a program to read in a real number x and to display f(x), where f(x) = |x| if x is less than zero and f(x) = ln |x+1| if x is greater than or equal to zero.
  4. Solution:
    #include <iostream>
    #include  <cmath>
    using namespace std;
    int main()
    {
            float x, y;
            cout << "Enter a real number: ";
            cin >> x;
            if ( x < 0 )
                    y = fabs(x);
            else
                    y = log(fabs(x+1));
            cout << "f("<< x <<") = " << y << endl;
            return 0;
    }                
    

  5. A restaurant pays its employees the following:
  6. · It pays $6 per hour for the first 20 hours in the week.

    · It pays $8.5 per hour for every additional hour.

    Write a C++ program to compute the salaries of employees at that restaurant. The input to the program is the number of hours worked and the output is the salary of the employee. The input should be a nonnegative integer.

    Solution:
    #include <iostream>
    using namespace std;
    int main()
    {
            float hours, salary;
            cout << "Enter the number of hours: ";
            cin >> hours;
            if ( hours <= 20 )
                    salary = 6 * hours;
            else
                    salary = 6 * 20 + ( hours - 20) * 8.5;
            cout << "The salary is: " << salary << endl;
            return 0;
    }           
    

  7. Write a C++ program to read 3 floating-point numbers and to display the smallest.
  8. Solution:
    #include <iostream>
    using namespace std;
    int main()
    {
            float a, b, c, min1 , min2;
            cout << "Enter three real numbers: ";
            cin >> a >> b >> c;
            // First compare a and b.
    	if ( a < b )
                    min1 = a;
    	else
    		min1 = b;
    	// Now compare c with the minimum of a and b.
    	// Note that min1 is the minimum of a and b.
    	// min2 is the minimum of a, b, and c.
    	if ( min1 < c )
                    min2 = min1;
    	else
    		min2 = c;
            cout << "The minimum is: " << min2 << endl;
            return 0;
    }                
    

  9. Write a program to ask the user to enter a number between 0 and 9999. If the user enters a number between 0 and 9, the program should display "One Digit". If the user enters a number between 10 and 99, the program should display "Two Digits". If the user enters a number between 100 and 999, the program should display "Three Digits". If the user enters a number between 1000 and 9999, the program should display "Four Digits". If the number is not in the required range, the program should display "Out of Range".
  10. Solution:
    #include <iostream>
    using namespace std;
    int main()
    {
            int number;
            cout << "Enter a number: ";
            cin >> number;
            if ( (number >= 0) && (number <= 9) )
                    cout << "One Digit" << endl;
            else if ( (number >=10) && (number <= 99) )
                    cout << "Two Digits" << endl;
            else if ( (number >=100) && (number <= 999) )
                    cout << "Three Digits" << endl;
            else if ( (number >= 1000) && (number <= 9999) )
                    cout << "Four Digits" << endl;
            else
                    cout << "Out of Range" << endl;
            return 0;   
    }
    

  11. Write a program that reads a numeric grade (between 0 and 100) and displays the corresponding letter grade. Here is the scale: 0-59 (E), 60-69 (D), 70-79 (C), 80-89 (B), 90-100 (A). If the entered grade is not between 0 and 100, the program should display "Invalid Grade".
  12. Solution:
    #include <iostream>
    using namespace std;
    int main()
    {
            float Grade;
            cout << "Enter a grade: ";
            cin >> Grade;
            if (Grade <= 59)
                    cout << "E" << endl;
            else if (Grade >= 60 && Grade <= 69)
                    cout << "D" << endl;
            else if (Grade >= 70 && Grade <= 79)
                    cout << "C" << endl;  
     	else if (Grade >= 80 && Grade <= 89)
                    cout << "B" << endl;
            else if (Grade >= 90 && Grade <= 100)
                    cout << "A" << endl;
            else
                    cout << "Invalid Grade" << endl;
            return 0;
    }