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 n