yanofsky

Quantitative Reasoning In a Computer Programming Class

 

Noson S. Yanofsky

Department of Computer and Information Sciences

noson@sci.brooklyn.cuny.edu

 

We have implemented a program that incorporates Quantitative Reasoning (QR) in a first year computer science class.  By placing several different QR-type exercises into students programming assignments, we give the students a chance to learn and work with QR.  For example, the students will have to write a program that makes a bar graph or that prints out a percentages table of quantitative data.  A student that programs a computer to print a bar graph will surely know what a bar graph is.  It is our firm hope, that getting these students to do these assignments will be a successful method of teaching them programming as well as QR methods.

 

The computer and information science (CIS) department at Brooklyn College requires that every computer science major take a four-course sequence of programming classes: CIS 1.5 (Introduction to Programming in C), CIS 15 (Advanced Techniques in C), CIS 22 (Data Structures) and CIS 26 (Object-Oriented Programming).  These courses are designed to give the students the foundations of a career in computer science.  Although QR methods may be introduced into the first semester (CIS 1.5), it is our feeling that the students would not gain much from QR at such a low level.  Rather, we feel that a student needs a certain amount of programming savvy before he or she can begin to program QR type problems.  CIS 15 seems to be the right venue for teaching QR in a programming class.

 

The central part of any programming class is the programming assignments.  The only way to learn how to program is to practice programming.  Through the semester, there are nine programming assignments.  Each assignment is supposed to take about a week-and-a-half to complete.  The students write, change, debug, rewrite, debug, rewrite and finally run their programs.  There is no substitute for this type of exercise.  Since the programs are the central part of the class, about a quarter of the class is directed to the assignment.  The professor must show the students how to do a particularly complicated part of the program or explain clearly what is expected from their program.

 

The assignments will be increasingly difficult as the semester continues.  In fact, as the semester goes on, the students will be incorporating their earlier programs into later ones.  One of the central ideas taught in CIS 15 is separate compilation.  Separate compilation is a method of bringing together different parts of programs to make a large program.  This is used in all programming jobs and is fundamental for any computer scientists.  We incorporate separate compilation by slowly building a working database program that has many different QR features.  The database starts in assignment 5, and more and more features are added to the program in each assignment.  At the end of the semester, the student has created a fairly large working database with many features.  This adds a tremendous feeling of accomplishment for the student and brings him/her into the world of advanced programming. 

 

Here is a list of the assignments for the Spring 2002 semester.

1.       The student works with weighted averages.

2.       Magic squares are constructed.

3.       Uniformly and normally distributed numbers are generated.

4.       Bar charts are generated (of the numbers generated in assignment 3).

5.       A basic database of students’ records is set up.

6.       Bar charts (assignment 3) and sorting are added to the basic database.

7.       Outlier statistics are added to the database.

8.       Percentages tables are created for the database.

9.       Basic recursion is done with simple mathematical functions.

 

 

Below is a sample of some of the programming assignments that will be distributed in the Spring 2002 semester.

 

 

 

 

 

 

 

Brooklyn College                                                                                                                    CIS 15

N. Yanofsky                            Weighted Averages         Assignment 1

 

Introduction:

In this program, we will calculate weighted averages.  Write a complete C program that helps a teacher find the weighted average of a student’s grades.  The program should also help a teacher answer the silly nagging question that many students ask: “How much do I have to get on the final in order to get an _ in the class?”

 

Specification:  

The program should open with a nice heading on the screen.  The program should ask the teacher how many tests will be given throughout the semester.  The program should then send this number, n, and an empty integer array, percentages, to a void function.  The void function should fill percentages by asking the user what percent of the grade is each test.  Notice that the sum of all the percentages must be 100.  If the sum is not 100, give an appropriate error message and make the user reenter the information.

 

Once the percentages array is full, you should give the user two options. Each of these options should be executed by a separate function.

 

a) Allow the user to enter the n test grades and calculate the weighted average of the grades.  (Hint: if pi is the i-th pecentage and ti is the i-th test grade, then

average = åni=1 pi * t­i  .  )

Example.  If the percentages are

 

1

2

3

4

5

10

33

21

20

16

 

and the test grades array is

 

1

2

3

4

5

96

78

84

94

100

 

then the average is 0.10*96 + 0.33*78 + 0.21*84 + 0.20*94 + 0.16*100 = 87.78

 

b) Allow the user to enter n-1 grades.  Ask the user what he/she would like the final grade point average to be.  Read the response into a double variable above.  Then calculate and print what the student needs to get in the n-th test to earn a grade higher than above.  (Hint: If x denotes the unknown n-th grade then we have the following inequality:

åi¹n pi * t­i  + pn*x > above

Now simply solve for x.)

 

 

 

 

Example.  If the percentages are

 

1

2

3

4

5

10

33

21

16

20

 

the tests grades are

 

1

2

3

4

5

96

78

84

100

x

 

and the student wants to get above an 85, then we have the following inequality:

0.10*96 + 0.33*78 + 0.21*84 + 0.16*100 + 0.20*x > 85.

Solving for x, we get x > 80.1.

 

Once this is done, allow the teacher to enter the information for another student.  Stop when the teacher is bored with the program (ask the teacher.)

 

Extra Credit:

1) Rather than calculate what the student must get for a specific above, make a chart that tells the student what average he/she will get for each of the possible n-th test scores.  So you should have a chart with two rows: n-th test score and weighted average.  You need not have every n-th test score, rather go in increments of 10 (0, 10, 20, . . ., 100.)

 

2) Do the same for the weighted standard deviation as well as the weighted average.

 

Good Luck!

 

Assignment Due: February 14, 2002.


Brooklyn College                                                                                                                    CIS 15

N. Yanofsky                             2-Dimensional Arrays            Assignment 2

                                                           

Introduction:

In order to gain some experience working with two-dimensional arrays, you are going to make magic squares.  A magic square is a square of numbers such that if you add up any column, row or diagonal, you get the same sum.  For more information see e.g.: http://freespace.virgin.net/mark.farrar1/msfmsq01.htm

Here is a way of making a magic square of odd size.  Consider the following three squares.

 

8

1

6

3

5

7

4

9

2

                                                              

17

24

1

8

15

23

5

7

14

16

4

6

13

20

22

10

12

19

21

3

11

18

25

2

9

 

 

30

39

48

1

10

19

28

38

47

7

9

18

27

29

46

6

8

17

26

35

37

5

14

16

25

34

36

45

13

15

24

33

42

44

4

21

23

32

41

43

3

12

22

31

40

49

2

11

20

 

 

You place 1 in the central position and place 2, 3, ...n­2 in their correct position by “wrapping” the other numbers around the square following the observed rules.  Before trying to get the computer to do this, you should try a few examples on paper.

 

Specification:

Your main function will interact with the user.  Main will ask the user what size magic square is desired.  The function will ensure that the user entered an odd number that is not too big.  The main function will then call the appropriate functions to fill a two-dimensional array.  Once the matrix is full, it should be sent to a function to check that the square is, in fact, a magic square.  Finally, main should create another matrix that is the transpose of the first matrix.  (Remember: B is a transpose of A if B[i,j]=A[j,i]).  One should confirm that this new matrix is also a magic square.  Main will go on to offer the user another chance to see another magic square.

 

There will be a function that accepts a 2-dimensional array of a specified size and will fill it with the numbers.  The function will have to go through the rules to see where to place the number.  Hints: 1) make a flow-chart of the rules, 2) set the entire array to 0 first, 3) make this function last (do the other easier functions first).

 

There will be another function that accepts a two-dimensional array of a specified size and will check that it is a magic square.  The function should create two 1-dimensional arrays: a) the sum of each of the rows and b) the sum of each of the columns.  Once these are calculated, the function should make sure that all the numbers are the same and that the numbers are the same as the sum of diagonals.  The function should print an appropriate message if they are not the same (We don’t expect to see this output, since you will follow the rules and come out with perfect magic squares.  However, it should be in the source code of the program.)

 

There will be another function that also accepts a 2-dimensional array and its size and prints it out nicely.  Print appropriate titles.

 

Hint: Write each function separately.  Test each one and make sure each one works before you go on to the next one.

 

Output:

Print three different magic squares.  Print the largest magic cube possible on one sheet of paper.

 

Extra Credit:

1) Make a very large magic square (one that takes more than one sheet of paper).  The print function might have to be adjusted.

2) Notice that there are other ways of getting a magic square.  In our example, we are always going up and to the right.  How about always going down and to the left, or always going up and to the right?  Give the user an option of what type of magic square is desired.

3) Make Magic Cubes.

 

Good Luck!

 

Assignment Due: February 28, 2002.


 

Brooklyn College                                                                                                                    CIS 15

N. Yanofsky                             Random Numbers                             Assignment 3

                                                           

Introduction:

In this program, we will generate random numbers.  We will generate two types of sequences: uniformly distributed and normally distributed.  Once each of these sequences has been created, you will find its mean and median.

 

Specification:

We are now mature enough that each function and call does not have to be specified.  Make sure that your program is suitably modular and clear.

 

You should first generate a uniformly distributed sequence of numbers.  First, ask the user how many whole numbers to generate.  You may assume that no more than 300 numbers will be desired.  Then ask the user what the largest and smallest numbers should be.  Generate the numbers.  Calculate the max, min, average and standard deviation of the numbers.  Print out all the relevant information.  Then ask the user if he/she would like to save the numbers generated.  If yes, ask for a file name and save them (you might save them to a file one at a time or save the whole array with one print statement.)

 

Once this is done, you should generate normally distributed random numbers.  First, ask the user how many whole numbers he/she would like to generate.  You may assume that no more then 300 numbers will be needed.  Then ask the user what the mean and standard deviation (sd) of the numbers should be.  The numbers should be generated with the following secrete code:

                        u1=(double)(rand()%99+1) / 100;

                        u2=(double)(rand()%99+1) / 100;

                        x= sqrt(-2*log(u1))* cos(2*3.14159*u2);

                        arr[i]=(int)(x*sd + mean);

Once this is done, calculate the actual average and standard deviation of the numbers.  Print out all the relevant information.  Then ask the user if he/she would like to save the numbers generated.  If yes, ask for a file name and save.

 

To Generate Random Numbers:

1) At the top of the program:

                        #include <stdlib.h>

2) In main(), after all the variables have been declared, before any other statement,

                        srand(time(0));

3) If you want the variable y to be any number between 1 and n,

                        y = rand() % n + 1;

4) If you want the variable y to be any whole number between min and max inclusively,

                        y = rand() % (max – min + 1) + min

5) If you want the variable to be some real number between 0 and 1 (not inclusively),                                y = (double) (rand() % 99+1) / 100;

 

 

 

 

 

 

Sample Screen Output:

How many random numbers would you like?  70

What mean would you like?  120

What standard deviation would you like?  7

118     110     118     124     118     124     109     118     126     116

 

126     111     126     119     117     128     108     118     125     120

 

119     119     118     116     105     125     123     115     122     113

 

123     124     121     128     118     118     130     113     124     104

 

122     118     115     122     116     134     135     125     117     121

 

124     114     122     106     111     136     117     130     105     104

 

132     126     129     116     124     119     112     116     127     121

 

The actual mean and actual sd are 119.614286 and 7.270867.

 

 

Extra Credit:

1) Calculate median and mode of the random sequences.

2) Make bimodal (two modes. . .“camel hump curve”) normal distributions.  This is done by choosing your random numbers from one “hump” (i.e. with one mean and s.d.) and then from the other (i.e. another mean and s.d.).  Do this repeatedly.

 

Good Luck!

 

Assignment Due: March 12, 2002.

 


Brooklyn College                                                                                                                    CIS 15

N. Yanofsky                                         Bar Charts                                        Assignment 4

                                                           

Introduction:

In this program, we shall use the numbers generated in assignment 3 and make bar charts of them.

 

Specification:

We are now mature enough that each function and call does not have to be specified.  Make sure that your program is suitably modular and clear.

 

Main should ask the user for a file name and read in a series of numbers (This obviously depends how you saved them into the file last assignment.  Did you use a header value?)  Once the values are in, ask the user what should be the minimum (min) and maximum (max) value in the chart (give the user the option for the computer to calculate what are the actual minimum and maximum in the array.)  Ask the user how many bars they would like (bars).  Calculate the number of values that each bar should have:

                        width = (max - min + 1) / bars;

Now we simply must have an array of counters that keep track of how many values are in each bar.  If your array of counters is called count[ ] and your values are in

arr[ ], then (arr[i] - min) / width is the bar of the value arr[i].  And so, for each value in arr[], we must increment that counter i.e.

                        count[(arr[i] - min) / width]++;

Once count is complete, print the chart.


A Sample Output:

How many random numbers would you like? 100

What mean would you like? 85

What standard deviation would you like?15

67      71      66      98      111     53      101     73      79      49

 

97      71      86      49      94      78      82      85      75      79

 

95      96      72      77      66      93      95      89      83      55

 

99      89      82      86      98      79      84      77      84      66

 

98      95      82      69      105     79      82      97      71      72

 

92      80      82      91      62      89      93      72      80      97

 

74      82      91      56      81      84      62      84      93      73

 

97      68      92      67      51      86      87      120     75      92

 

84      92      88      77      73      81      94      86      67      65

 

67      113     110     91      86      70      94      63      73      71

 

Max= 120    Min=49    How many bars would you like?7

count[0]= 6

count[1]= 12

count[2]= 20

count[3]= 28

count[4]= 27

count[5]= 3

count[6]= 3

[49...58]         ******

[59...68]         ************

[69...78]         ********************

[79...88]         ****************************

[89...98]         ***************************

[99...108]       ***

[109...118]     ***

There is no reason to print what the values of the counters are (as I have done above).

Print output for both uniformly distributed and normally distributed values. Print at least five nice charts.

Extra Credit:

1) Make your chart vertically.

2) Generate many numbers (in the thousands) and let each star correspond to many elements. After doing some printing experiments, show the computer how to calculate the number of elements each star represents. Print the appropriate message.

 

Good Luck!

 

Assignment Due: April ??, 2002.


Brooklyn College                                                                                                                    CIS 15

N. Yanofsky                                        Database: Tables                   Assignment 8

 

                                               

Introduction:

In order to better understand tables and quantitative information, we shall add a table-making feature to our database program.

 

Specification:

Write a function that accepts the array of structures and the number of elements in the array. The function should make a table displaying how the grades are distributed with respect to the students’ class. For this assignment we are going to make the (stupid) assumption that a student with an average between 90 and 100 will get an ‘A’; between 80 and 89 a ‘B’; between 70 and 79 a ‘C’; and all the rest get an ‘F’. Let us further assume that college is split up into two parts: Upper classmen (Seniors and Juniors) and Lower classmen (Sophomores and Freshmen). We are interested in knowing what percentage of each class received an ‘A’. What percentage of each class received a ‘B’ etc.

 

How is this done? Simply go through the array keeping count of populations. There are 4 classes (1,2,3,4) and there are one of 4 types of grades (A,B,C,F). We must keep a count for each of these 16 groups of students. So make a 2-dimensional array (remember assignment 2?) of counters. For each student, increment the appropriate counter. When this is complete, sum-up the number in each class. Divide the appropriate numbers and multiply by 100 to get the percentages. Also, calculate the appropriate information about lower classmen and upper classmen.

 

Sample Output:

For test information of the following type:

Cls. Grd        Cls. Grd        Cls. Grd        Cls. Grd

---- ---        ---- ---        ---- ---        ---- ---

   1  65           3  72           4  12           2  87

   1  97           3 100           2  67           4  58

   1  86           3  78           2  67           4  85

   4  77           1  86           4  75           2  56

   3  87           2  56           1  68           2  77

   3  79           2  90           2  91           1  93

   2  43           3  99           4  97           3  92

There should be a table:

                                 GRADES

                           A      B       C       F

Lower Cls                26.7%  20.0%   6.7%    46.7%

         Freshmen        33.3%  33.3%   0.0%    33.3%

         Sophomores      22.2%  11.1%   11.1%   55.6%

Upper Cls                30.8%  15.4%   38.5%   15.4%

         Juniors         42.9%  14.3%   42.9%   0.0%

         Seniors         16.7%  16.7%   33.3%   33.3%

Notice that the sum of each row is 100%.

 

Good Luck!

 

Assignment Due: June ??, 2002.