Exercice Supplementer CSC 300
Exercice Supplementer CSC 300
Exercice 1 :
Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which
is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a
number already read. Provide for the “worst case” in which all 20 numbers are different. Use the
smallest possible array to solve this problem.
Sample Outputs:
Exercice 2 :
Use a single-subscripted array to solve the following problem. A company pays its salespeople on
a commission basis. The salespeople receive $200 per week plus 9 percent of their gross sales for
that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9
percent of $5000, or a total of $650. Write a program (using an array of counters) that determines
how many of the salespeople earned salaries in each of the following ranges (assume that each
salesperson’s salary is truncated to an integer amount):
a) $200–$299
b) $300–$399
c) $400–$499
d) $500–$599
e) $600–$699
f) $700–$799
g) $800–$899
h) $900–$999
i) $1000 and over
Sample Outputs:
Exercice 3 :
A selection sort searches an array looking for the smallest element in the array. Then, the smallest
element is swapped with the first element of the array. The process is repeated for the subarray
beginning with the second element of the array. Each pass of the array results in one element being
placed in its proper location. This sort performs comparably to the bubble sort—for an array of n
elements, n - 1 passes must be made, and for each subarray, n - 1 comparisons must be made to
find the smallest value. When the subarray being processed contains one element, the array is
sorted. Write recursive function selectionSort to perform this algorithm.
Sample Outputs:
Sample Outputs:
Exercice 5 :
Modify the program in Fig. 4.19 to use recursive function linearSearch to perform a linear search
of the array. The function should receive an integer array and the size of the array as arguments. If
the search key is found, return the array subscript; otherwise, return –1.
Sample Outputs:
Command Meaning
1 Pen up
2 Pen down
3 Turn right
4 Turn left
5,10 Move forward 10 spaces (or a number other than 10)
6 Print the 20-by-20 array
9 End of data (sentinel)
Suppose that the turtle is somewhere near the center of the floor. The following “program”
would draw and print a 12-by-12 square and end with the pen in the up position:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to 1’s. When
the 6 command (print) is given, wherever there is a 1 in the array, display an asterisk or some other
character you choose. Wherever there is a zero, display a blank. Write a program to implement the
turtle graphics capabilities discussed here. Write several turtle graphics programs to draw
interesting shapes. Add other commands to increase the power of your turtle graphics language.
Sample Outputs: