SUBROUTINES

 

Problem # 1

 

Write a subroutine that takes 3 integers and returns the maximum , the minimum , and the difference between maximum and minimum. Write a main program to test the subroutine.

 

Problem # 2

 

Write a subroutine that takes 4 integer values and returns them  after subtracting the minimum from each value. Write a main program to test the subroutine.

 

Problem # 3

 

Write a program that reads three integer numbers then it calls a subroutine that takes the three numbers and finds the minimum and maximum numbers among them. Your program should print the three numbers, the minimum and the maximum with suitable messages.

 

Problem # 4

 

Write a subroutine that converts a number of seconds into hours, minutes, and seconds. For example, 3724 seconds is 1 hour, 2 minutes, and 4 seconds. Write a main program to test the subroutine.

 

Problem # 5

 

Write a subroutine that splits  a 3-digits integer number into hundreds, tens, and ones. Write a main program to test the subroutine.

 

Problem # 6

 

Given two points P1(X1,Y1) and P2(X2,Y2) ,Write a subroutine that computes the distance between the two points and the slope of the line connecting the two points. Write a main program to test the subroutine.

 

Slope = ( Y2 - Y1 ) / ( X2 - X1)

                      ______________________

Distance =  Ö ( X2 - X1) 2 + ( Y2 - Y1) 2

 

Note: Your program should take care of the case:  X2  = X1

 

Problem # 7

 

Given the number of hours worked by an employee in a week and the payment per hour, write a subroutine that returns the regular payment, over-time payment, and the total payment. The over-time payment is calculated as follows: every hour after 40 is considered as 1.6 hours. Write a main program to test the subroutine.

 

 

Problem # 8

 

Write a program which reads the length and width of a rectangle and prints the area and perimeter of that rectangle. The area and perimeter must be computed by a subroutine ARCIRC called by the program. Use any positive numbers as data.

 

Problem # 9

 

Write a program that reads a real number X and calls a subroutine SPLIT with three parameters X, N1, and N2. The subroutine should return the integer part of X as N1 and the fraction part of X as N2. The program should print the number, its integer part, and its fraction part.

Example: if X is 372.81 then N1 should be 372 and N2 should be 81

 

Problem # 10

 

Write a program that reads two integer numbers N1 and N2 and calls a subroutine JOIN with three parameters  N1, N2 and A. The subroutine should return the real number A such that the integer part of A is N1 and the fraction part of A is N2. The program should then print the real number and its two parts.

Example: if N1 is 30 and N2 is 694 then A should be 30.694

 

Problem # 11

 

Write a program that reads an integer number J and calls a subroutine SPLIT with four parameters J, K, L and M. The subroutine should return the first digit from right in M, the second digit from right in L and all other digits in K. The main program should print the values of J, K, L, and M. Assume that the input is always 3 digits or more.

Example: If J = 5732, then M should be 2, L should be 3 and K should be 57

Example: If J = 640, then M should be 0, L should be 4 and K should be 6

 

Problem # 12

 

Write a program which reads the surface area s of  a  right  circular cylinder of height r / Ö 3.0 where r is the radius. The program then calls a subroutine that  computes the radius r and the volume v of that cylinder. And pass them back to the main program. The main program then prints these two values.

 

Use the data: 155.0

Your output should be in the form:

 

RADIUS  =  6.5353451  CM

SURFACE AREA  =  155.0000000  CM SQUARED

VOLUME  =  506.4880371  CM CUBED

 

NOTE:                      ___                                ___

            v  =  pr 3  /  Ö 3.0  ,      s  =  2pr 2  /  Ö 3.0  ,    p  =  3.14159

 

 

Problem # 13

 

The roots X1 and X2 of a quadratic equation :

   

  AX2  + BX + C = 0

 

are given by:

                             _____________               

                 -B +  Ö( B2  -   4AC )

   X1  =   ___________________

                              2A

 

                           ______________  

                 -B - Ö( B2   -   4AC )

   X2  =   ___________________

                              2A

 

Write a program which will prompt for and read values for A, B, and C and then it passes these values to a subroutine which computes the roots of the corresponding quadratic equation. The subroutine then passes the computed roots to the main program where they are printed.

 

Use the following data:

  1.0    3.0    1.0

 

Problem # 14     

 

Write a program which reads the volume  v of a sphere and then  it calls a subroutine which computes the radius r and the surface area s of that sphere. The computed values are then passed to the main program where they are printed.

 

USE THE DATA:  25.0

 

Your output should be in the form:

 

               RADIUS =  1.8136721  CM

               VOLUME =  25.0000000  CM CUBED

               SURFACE AREA = 41.3525085  CM SQUARED

 

  NOTE:     v = 4.0 / 3pr3    ,  s = 4pr2      , p = 3.14159

 

 

Problem # 15

 

Write a program which reads the volume v of a right circular cylinder of height 3r2 where r is the radius. The program then calls a subroutine that computes the radius r and the surface area s of that cylinder. The computed values are then passed to the main program where they are printed.

 

USE THE DATA: 100.0

 

Your output should be in the form:

 

               RADIUS =  1.8046312  CM

               VOLUME =  100.0000000  CM CUBED

               SURFACE AREA =  55.4129333  CM SQUARED

 

NOTE:

             v  =  3pr 4     ,  s = 3pr3   ,    p = 3.14159

 

Problem # 16

 

Write a subroutine to compute the surface area  s and the volume v of a sphere given its radius r. Write a main program to test your subroutine.

 

NOTE:

             s = 4pr 2     ,  v = 4.0 / 3pr3   ,    p = 3.14159

 

Use the data:

3.2

 

Your output should be:

 

RADIUS  =  3.2000000  CM

SURFACE AREA  =  128.7313843  CM SQUARED

VOLUME  =  137.3133545  CM CUBED

 

Problem # 17

 

An employee is paid overtime if he works more than 40 hours in a week. Write a program which reads the number of hours (h ) an employee has worked in a week, the hourly rate of payment (r) and then it  calls a subroutine which computes the gross salary (g) and the net pay (p) as follows:

                   ì   h * r                             if  h £  40

          g  =   í

                   î   1.5r(h - 40) + 40r         if  h  >  40

 

                   ì   g                                  if  g £  65.00

          p  =   í

                   î   g - (15.00 + 0.045g)     if  g  >  65.00

 

Your  main program should print the hourly rate, the number of hours worked,  the gross salary, and the net pay.

Problem # 18

 

Write a program to compute the time a car trip takes and the cost of gasoline for that trip. The input to the program is the trip distance in kilometers, the average speed   (kilometer / hour), the number of kilometers traveled on one liter of gasoline, and the cost in Riyals of a liter of gasoline. The computations must be done in a subroutine called by the main program.

 

Problem # 19

 

Write a program that reads the coordinates of three ships: 1, 2 and 3 and  then it passes these coordinates to a subroutine which determines which two ships are closest. The subroutine should return either 1 and 2 or 1 and 3 or 2 and 3. (Assume that the three distances are not equal). The main program should then print a message of the form:

            SHIP X AND SHIP Y ARE CLOSEST

 

Hint: If (X1, Y1) are the coordinates of SHIP1 and (X2, Y2) are the coordinates of SHIP2, then the distance separating the two ships is given by:

             ____________________

          Ö (X1 - X2)2 + (Y1 - Y2)2

 

**Problem # 20

 

Write a subroutine to calculate the excess baggage charge and the excess weight for a SAUDIA passenger. The free baggage allowance is based on the following table:

 

CLASS

CLASS CODE

FREE BAGGAGE ALLOWANCE (KILOGRAMS)

FIRST

1

40

HORIZON

2

30

GUEST

3

20

 

Excess baggage is charged at 1 % of the first class single fare per kilogram.

 

The input to the program is : a class code, the passengers baggage weight in kilograms, and the first class single fare for his flight. All these should be read by the main program and passed to the subroutine. The subroutine should return the excess baggage charge and the excess weight.

 

The main program must output:

 

Baggage weight, Excess baggage weight, and excess baggage charge for the passenger.

 

Use the following data:

 

2                      52                    7348

 

Your output should be:

 

            WEIGHT                     EXCESS WEIGHT                      CHARGE

                 52                                       22                                  1616.5600586

 

Problem # 21

 

Three numbers A, B, and C form the sides of a triangle if A < B + C,  B < A + C and             C < A + B. Write a program which reads three REAL numbers A, B and C it then calls a subroutine which determines whether the numbers form a triangle or not. If the three sides form a triangle the subroutine returns both S and AREA to the main program where:

 

      S = (A + B + C) / 2

                         __________________________

      AREA =  Ö (S * (S - A) * (S - B) * (S - C) )

 

If the three sides do not form a triangle the subroutine returns a value of -1.0 in S and a value of -1.0 in AREA. The main program then prints, in both cases, the values of S and AREA.