1-DIMENSIONAL ARRAYS

 

Problem # 1

The data which represents the IDs and grades of M students in N quizzes is arranged as:

 

     4      5

     1000            50.0     67.0     80.0     40.0     70.0

     1001            90.0     55.0     75.0     60.0     80.0

     1002            68.0     80.0     92.0     55.0     88.0

     1003            85.0     30.0     66.0     49.0     30.0

 

     where 4 in the first data line is the number of students and 5 is the number of quizzes.

 

     Write a program that determines for each student the average of his N - 1 highest grades. Assume that the maximum number of quizzes is 10. Your output should be in the form:

 

            STUDENT ID                          QUIZ AVERAGE

                   1000                                     66.750000

                   1001                                     76.250000

                   1002                                     82.000000

                   1003                                     57.500000

 

Hint: * Use a one-dimensional array to read each students grades.

         * For each student find his minimum grade and subtract it from the sum of his grades.

 

Problem # 2

 

Write a program which reads a 1-D INTEGER array of size 6 it then prints the average of the positive elements in the array (Regard 0 as a positive element). The average must  computed by an REAL function AVERG invoked by the program.

 

Use the data: -2     3    2    5    -4    2

 

Your output should be:

AVERAGE OF POSITIVE ELEMENTS =                 3.000000

 

Problem # 3

 

Write a program which reads a 1-D INTEGER array of size 6 it then prints the product of the positive elements (i.e. elements greater than zero) in the array. The product must be computed by an INTEGER function PRDCT invoked by the program.

 

 Use the data: -2     3    2    5    -4    2

 

Your output should be:

PRODUCT OF POSITIVE ELEMENTS =                 60

 

 

Problem # 4

 

The data which represents the IDs and answers of M students in a true-false quiz of N questions is arranged as:

 

     5      6

                         0         1          0          0          1          0      <<<  Correct answers

 

     22                0          1          1          0          1          0

     33                0          1          0          0          1          0

     44                1          1          1          1          1          1

     55                0          0          0          1          1          1

66                    1          1          1          1          0          1

 

where 5 in the first data line is the number of students and 6 is the number of questions. 1 represents true and 0 represents false.

The correct answers are:

                        0          1          0          0          1          0

as given in the second data line.

 

Write a program that will read the number of students and the number of questions and then it reads the correct answers in a one-dimensional INTEGER array SOLTN of maximum size 30. The program finally determines for each student the number of correct answers. Use a one-dimensional INTEGER array ANS to read each students answers. Your output should be in the form:

            STUDENT ID                          NUMBER OF CORRECT ANSWERS

                        22                                                        5

                        33                                                        6

                        44                                                        2

                        55                                                        3

                        66                                                        1

 

Problem # 5

 

Write a program that reads two integer arrays A and B each of size 4. The program then calls a subroutine which computes the product of corresponding elements in both arrays and returns the result to the main program in an integer array C of the same size. Your program should then print the input arrays and the result with proper messages.

Test your program with the following data:

2          4          3          3         

5          6          2          4         

 

Your output should be:

ARRAY A:                  2                      4                      3                      3

ARRAY B:                   5                      6                      2                      4

ARRAY C:                10                    24                      6                    12

 

 

Problem # 6

 

Write a program that reads the grades of students in N sections. The maximum number of students in each section is 25. The program then finds:

   (a) the average of each section .

   (b) the average of all the sections.

 

Use the following data in which the 3 in the first data line is the value for N and then each value in the first column represent the number of students in  a particular section, this is followed by the grades of that section :

 

3

5          80.0     60.0     70.0     45.0     55.0

4          75.0     90.0     65.0     65.0

2          67.0     78.0

 

HINT: Use a one-dimensional array GRADES to read the grade of each section.

 

Your output should be:

 

            SECTION#                 AVERAGE

                   1                           62.000000

                   2                           73.750000

                   3                           72.500000

 

            AVERAGE OF ALL SECTIONS:       68.181820

 

Problem # 7

 

Write a program to read a 1-D array A of size 7 and another 1-D array B of size 4. Then the program finds and prints the number of occurrences of each element of  array B in array A.

 

Use the following data:

4          3          4          6          4          2          3         

2          4          3          9

 

Your output should be:

ELEMENT OF B                     NUMBER OF OCCURRENCES IN A

                        2                      1

                        4                      3

                        3                      2

                        9                      0

 

 

Problem # 8

 

A shop sells 5 items with codes 1, 2, 3, 4, and 5. The unit prices of these items are to be read and stored in a one-dimensional REAL array PRICE. Write a program which will prompt for and read:

            1. The unit prices.

            2. The number M of customer to be processed.

The program then reads for each customer his ID and the number of items he bought for each of the item codes 1, 2, 3, 4, and 5 and it finally computes the total bill of the customer.

 

Use the following data:

 

10.5     15.0     2.0       8.0       50.0

3

1001    3          6          0          20        0

1002    0          0          10        0          3

1003    1          4          3          10        5

 

Your output should be:

 

            CUSTOMER ID                      TOTAL BILL

                    1001                                  281.500000

                    1002                                  170.000000

                    1003                                  406.500000

 

Problem # 9

 

Write a function that takes a 1-D integer array and returns the average of its elements. Then write a main program that reads a 1-D integer array of size 6, calls the Function and prints the average of the elements of the 1-D array.

 

Use the data:

2          4          10        8          5          3

 

Your output should be:

ARRAY AVERAGE =             5.333333

 

Problem # 10

 

Write program that reads the list of students taking ICS101 as 1-D array and the list of their grades into another 1-D array. The first line has the number of students ( maximum 30 ) taking the course. The program prints the IDs of those students with grades above the average. Your program should also print the number of the students above the average.

 

 

Problem # 11

 

Each of the faculty in the ICS department has one of the IDs: 1, 2, 3, 4, . . . , 15 to use for photocopying on the departments photocopier. The following data, in which the first column represents Faculty IDs and the second the number of papers used, represents the number of papers used by various faculties in one week:

 

              3        15

              1        40

              8        25

              1          3

              4        50

              3        20

            12        17

              8        40

            13          8

              4        15

             -1        -1

 

The last data line is the sentinel.

 

Write a program which will read the above data and then it prints:

    (1)    For each faculty who photocopied: his ID and the total number of papers he used.

    (2)    The total number of papers used by all faculties.

 

Hint: Use two integer arrays FCLTY and PAPERS each of size 15. Initialize the elements of FCLTY to 1,2,3, . . .,15 and each element of PAPERS to zero.

 

Your output should be:

FACULTY ID                         NUMBER OF PAPERS USED

                     1                         43

                     3                        35

                     4                         65

                     8                         65

                   12                         17

                   13                           8

           TOTAL           233

  

Problem # 12

 

Write a program that reads the grades of a number of students that does not exceed 20. The program should then pass these grades to a subroutine that computes and returns: (1) the maximum grade scored, and, (2) the number of grades that are below the average of the grades. The values returned to the main program should then  be printed.

 

 

Problem  # 13

 

Write a program that reads the number of students at KFUPM from year 1990 to 1995 and prints the following:

1.      The year with maximum number of students.

2.      The year with minimum number of students.

3.      The average number of students through all years 1990 to 1995.

Assume that no two years have the same number of students. You must use the         1-dimensional array declaration INTEGER NUMST(1990:1995). Test your program with the following input:

 

                                    4000    3000    4005    5000    4500    4550

Your output should be:

YEAR WITH MAXIMUM NUMBER OF STUDENTS IS     1993

YEAR WITH MINIMUM  NUMBER OF STUDENTS IS      1991

AVERAGE NUMBER OF STUDENTS IS         4175.833000

 

Problem  # 14

 

A company employs a group of 6 salesmen who are paid commission on that portion of their sales which exceeds the average sales of the group. Write a program that reads the ID number and sales value for each salesman and prints out the ID numbers of the salesmen who qualify for commission.

You must use two 1-dimensional array declarations:

                        INTEGER   ID(6)

                        REAL   SALES(6)

Use the following data:

2          2500.0

4          1000.0

1          5000.0

6          3000.0

3          1500.0

5          6000.0

 

Your output should be:

SALESMEN WHO QUALIFY FOR COMMISSION:

                        1

                        5

 

Problem #15

 

Write a program that reads 7 real numbers into 1-dimensional array and prints the following:

 

1.      The array reversed on one line.

2.      The number of negative elements.

3.      The number of positive elements.

 

 

Problem # 16

 

Write a function to compute the dot product of two vectors A and B. The size of each vector is 5. Write a main program to test your function.

 

Dot Product  = A(1)*B(1) + A(2)*B(2) +  .  .  .   + A(5)*B(5).

 

Problem#17

 

Write a program that reads a 1-dimensional array of size 6 it then prints out the partial sums of the array. You should write a function to compute the partial sums.

 

Use the following data:  2          5          7          3          1          4

 

The output of your program should be:

 

THE SUM OF THE FIRST                 1   ELEMENT(S) IS                  2

THE SUM OF THE FIRST                 2   ELEMENT(S) IS                  7

THE SUM OF THE FIRST                 3   ELEMENT(S) IS                14

THE SUM OF THE FIRST                 4   ELEMENT(S) IS                17

THE SUM OF THE FIRST                 5   ELEMENT(S) IS                18

THE SUM OF THE FIRST                 6   ELEMENT(S) IS                22

 

Problem#18

 

Write a subroutine SPLIT that receives a 1-dimensional integer array of size 7 and returns two 1-dimensional arrays: ODD and EVEN. The array ODD should contain the odd numbers and the array EVEN should contain the even numbers. Write a main program to test your subroutine. The main program should print the message NO EVEN ELEMENT or NO ODD ELEMENT if there is no even element or no odd element respectively.

 

Use the following data:  2          5          6          3          1          8          9

 

Your output should be:

EVEN ELEMENTS:                2          6          8

ODD ELEMENTS:                  5          3          1          9

 

Problem # 19

 

Write a program to find the minimum even element in a one-dimensional integer array of size 7. If the array has no even elements the program should print the message:                NO EVEN ELEMENTS. Use the following data to test your program:

 

1          3          8          5          2          4          9

 

Your output should be:

MINIMUM EVEN ELEMENT IS       2

 

 

Problem#20

 

Write a program which prompts for and read the number of elements in a one-dimensional real array of maximum size 20. The program then reads the array and passes it to a subroutine which returns the positions of all the positive elements in the array (Regard 0 as a positive element). If the array has no positive elements the main program should print the message:  NO POSITIVE ELEMENTS , otherwise it should produce an output of the form:

 

POSITIONS OF POSITIVE ELEMENTS:

XX       XX       XX    . . .          XX

 

Use the following data:

7

-1.0      3.0       6.0       -2.0      4.0       -5.0      3.0

 

Your output should be:

 

POSITIONS OF POSITIVE ELEMENTS:

          2                      3                      5                        7

 

 

Problem # 21(*)

 

Write a program which prompts for the number of students in a course. The program then reads the student IDs and scores of those students in an exam. The program should compute and print the average (AVRG) of the scores, the standard deviation (STDEV). The program also determines the grade category of each student as follows:

           

if  SCORE  >  AVRG  +  STDEV , the grade is outstanding

            if  SCORE  <   AVRG  -  STDEV , the grade is unsatisfactory

            otherwise, the grade is satisfactory

 

As the program determines the grade category for each student, it should print the student ID, score, and category. Assume that the maximum number of students the program should be able to deal with is 100. Assume no invalid input.

 

The standard deviation of  N scores is given by:

   __________________________________________________

Ö ( score12 + score22 + score32 +  . . . + scoreN2 ) / N  - AVRG2

 

where AVRG is the average of the N scores

 

Use the following data as input:

 

6

960001            85.0

960002            20.0

960003            75.0

960004            90.0

960005            60.0

960006            40.0

 

Your output should be:

 

AVERAGE =               61.666670

STANDARD DEVIATION =              24.944380

 

            ID                    SCORE             GRADE CATEGORY

      96001                 85.000000             SATISFACTORY

      96002                 20.000000             UNSATISFACTORY

      96003                 75.000000             SATISFACTORY

      96004                 90.000000             OUTSTANDING

      96005                 60.000000             SATISFACTORY

      96006                 40.000000             SATISFACTORY

 

 

Problem # 22(***)

 

Two one-dimensional integer arrays of size 5, each with distinct elements, represent two sets. Write a program which will read the arrays and then it determines and prints the UNION and the INTERSECTION of the two sets. If the INTERSECTION is an empty set the message: EMPTY SET should be printed for the INTERSECTION.

 

Hint: Assign all union elements to an integer array UNION of size 10

        Assign all intersection elements to an integer array INTSCT of size 5

 

Use the following data:

2          6          4          1          3

1          3          8          5          9

 

Your output should be:

UNION:

2                                              6                      4                      1                      3                      8         5        9

INTERSECTION:

          1                        3

 

Problem # 23

 

The best line through the points (x1, y1) , (x2, y2) , (x3, y3) , . . . , (xn, yn) has the equation y = mx + b , where m and b are given by:

                        n           _            _

                        å (xi  -  x )(yi  -  y )

                       i = 1                                                    __                      __

            m  =   ¾¾¾¾¾¾¾¾      ,           b  =  y   -   m  x

                              n           _      

                             å (xi  -  x ) 2

                            i = 1

               __          __

Where  x  and  y  are the averages of the x and y values respectively.    

 

Write a program which will determine m and b for a given set of points.

           

 

Problem # 24 (***)

 

If the x- and y-coordinates of a set of points (x1, y1) , (x2, y2) , (x3, y3), . . . , (xn, yn) are known  and the x-coordinate of a point (xi, yi) which lies between any two of the above points is also known then the y-coordinate of that point, if it is not known, is given by the formula:

 

          (xi - x2)(xi - x3) ... (xi - xn)           (xi - x1)(xi - x3) ... (xi - yn)                (xi- x1)(xi- x2) ... (xi- xn - 1)

yi =   ¾¾¾¾¾¾¾¾¾¾  y1 +   ¾¾¾¾¾¾¾¾¾¾  y2 + . . .  ¾¾¾¾¾¾¾¾¾¾¾ yn

          (x1 - x2)(x1- x3) ... (x1- xn)            (x2- x1)(x2- x3) ... (x2- xn)                (xn- x1)(xn- x2) ... (xn- xn - 1)

 

Write a program which will find the value of y for x = -2.0 using the following data:

(-5.5, -1.0) , (-4.8, 0.0) , (-2.9, 1.0) , (-1.6, 1.9) , (0.0, 2.5) , (1.2, 3.1) , (2.3, 4.0)

 

 

Problem # 25 (***)

 

Write a program which reads values in a one-dimensional INTEGER array. It then find the mode, i.e. the most frequently occurring value, in the array.

 

Problem # 26(*)

 

Write a program which prompts for a positive integer N not greater than 15 and a positive integer M not greater than 20. It then reads a sequence of M positive integers where each integer is not greater than N. The program should then determine which integer or integers in the range 1 to N is or are missing or duplicated in the sequence, if any. Example:

 

N

M

INPUT SEQUENCE

OUTPUT

6

6

4     2    5    1    6    3

PERFECT SEQUENCE

8

6

1     3    2    4    5    7

MISSING   6

MISSING   8

11

12

8   1   2   4   7   4   4   5   11    2    6   10

DUPLICATE  2 (2 TIMES)

MISSING  3

DUPLICATE  4  (3 TIMES)

MISSING  9