ECS 30A

Homework #6

Winter 2009

 

Due: Monday, Feb 16, 23:59


Programs: using handin  to cs30b p6 directory.

Filenames:    trap.c, grade.c, binary.c, checksum.c

 

Programming: All programs should be able to compile with no warnings when compiled with the –Wall option.  You should put your name(s) in a comment on the first line of each file.  The prompts, and output format of each program must match the examples exactly.  To use functions from math.h, you must have –lm on your compile line (that is an “l” as in library) to link with the math library, e.g., gcc –Wall –lm reperr.c


YOU ARE RESPONSIBLE TO MATCH THE OUTPUT FROM ORACLES....WILL BE POSTED SHORTLY. THE SAMPLES SHOWN BELOW ARE INEXACT: PLEASE USE THE ORACLES. Look in /home/cs30b/p6

 

 #7. page 364. Filename: checksum.c

 

[…@pc…]$ ./checksum.out
Enter an abitrarily long string, ending with carriage return > abcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkmabcdefghijklkm
Check sum is $

 

[…@pc…]

 

 #6, page 363. Filename: trap.c . The first two functions g and h are as in the book. For w, use the function
f(x) = x*x.  Important: Use double type for all floating point variables.

 

franky:hw5 devanbu$ ./trap.out
Enter the number of subintervals n > 2
Approximated area under the curve g = 3.875795
Approximated area under the curve h = 4.000000
Approximated area under the curve w = 0.375000
franky:hw5 devanbu$ ./trap.out
Enter the number of subintervals n > 128
Approximated area under the curve g = 5.869109
Approximated area under the curve h = 6.278594
Approximated area under the curve w = 0.333344

 

#1, page 422. Filename: grade.c

This is a good program with which to practice top-down design.  Your main() function may only contain user defined function calls, fopen(), fclose(), variable declarations, one loop, and the return statement.  The only assignment statements allowed in main() are those for your two FILE*.  The solution I will be using as an oracle has  six functions other than main().  main() called all six.

 

[…@pc…]$ cat examdat.txt

5 dbbac

111 dabac

102 dcbdc

251 dbbac

[…@pc…]$ grade.out

[…@pc…]$ cat report.txt

          Exam Report

 

Question  1  2  3  4  5

Answer    d  b  b  a  c

 

 ID   Score(%)

111     80

102     60

251    100

 

Question    1  2  3  4  5

Missed by   0  2  0  1  0

 

 

#12, page 428. Filename: binary.c

Note that to use the binary search , you need to first use selection sort function select_sort() to sort the elements in order. Your program will read from a file named data.txt that contains up to 20 elements.  Once the elements are read into an array, your program should call select_sort().  Once the array is sorted, your program should the prompt the user for a value, and print out the position in the array of that value.  If the value is not in the array, the program should indicate so.  The program will terminate when the user enters -1.

 

[…@pc…]$ cat data.txt

12 7 8 17 2 20 19 16 14 3 2 1

[davis@lect2 p5]$ binary.out

Array before sort =  12  7  8 17  2 20  19  16  14  3  2  1

Array after sort  =  1  2  2  3  7  8  12  14  16  17  19  20

 

Please enter a value (-1 = done)> 3

3 is located at position 3 in the array.

 

Please enter a value (-1 = done)> 14

14 is located at position 7 in the array.

 

Please enter a value (-1 = done)> 4

4 is not in the array.

 

Please enter a value (-1 = done)> 90

90 is not in the array.

 

Please enter a value (-1 = done)> -1

[…@pc…]$