0% found this document useful (0 votes)
59 views13 pages

Exercice Supplementer CSC 300

The document describes 6 exercises involving arrays and recursion in C++. Exercise 1 involves reading in 20 numbers and printing non-duplicate values using an array. Exercise 2 calculates sales commissions into ranges using an array of counters. Exercise 3 implements a selection sort using a recursive function. Exercise 4 finds the minimum element of an array recursively. Exercise 5 performs a linear search recursively. Exercise 6 simulates a turtle graphics program using a 2D array to represent the floor and draw shapes based on turtle commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views13 pages

Exercice Supplementer CSC 300

The document describes 6 exercises involving arrays and recursion in C++. Exercise 1 involves reading in 20 numbers and printing non-duplicate values using an array. Exercise 2 calculates sales commissions into ranges using an array of counters. Exercise 3 implements a selection sort using a recursive function. Exercise 4 finds the minimum element of an array recursively. Exercise 5 performs a linear search recursively. Exercise 6 simulates a turtle graphics program using a 2D array to represent the floor and draw shapes based on turtle commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

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:

Enter 20 integers between 10 and 100:


22 56 78 94 22 94 38 10 11 12 22 12 13 14 15 16 17 88 88 77
The nonduplicate values are:
22 56 78 94 38 10 11 12 13 14 15 16 17 88 77

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:

Enter employee gross sales (-1 to end): 10000


Employee Commission is $1100.00
Enter employee gross sales (-1 to end): 4235
Employee Commission is $581.15
Enter employee gross sales (-1 to end): 600
Employee Commission is $254.00
Enter employee gross sales (-1 to end): 12500
Employee Commission is $1325.00
Enter employee gross sales (-1 to end): -1
Employees in the range:
$200-$299 : 1
$300-$399 : 0
$400-$499 : 0
$500-$599 : 1
$600-$699 : 0
$700-$799 : 0
$800-$899 : 0
$900-$999 : 0
Over $1000: 2

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:

Unsorted array is:


957 848 727 597 384 617 228 424 878 10
Sorted array is:
10 228 384 424 597 617 727 848 878 957
Exercice 4 :
Write a recursive function recursiveMinimum that takes an integer array and the array size as
arguments and returns the smallest element of the array. The function should stop processing and
return when it receives an array of 1 element.

Sample Outputs:

Array members are:


7 84 951 884 404 167 905 93 744 115
Smallest element is: 7

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:

Enter the integer search key: 22


Found value in element 11
Exercice 6 :
The Logo language, which is particularly popular among personal computer users, made the
concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under
the control of a C++ program. The turtle holds a pen in one of two positions, up or down. While
the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about
freely without writing anything. In this problem, you will simulate the operation of the turtle and
create a computerized sketchpad as well.
Use a 20-by-20 array floor that is initialized to zeros. Read commands from an array that
contains them. Keep track of the current position of the turtle at all times and whether the pen is
currently up or down. Assume that the turtle always starts at position 0,0 of the floor with its pen
up. The set of turtle commands your program must process are as follows:

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:

Enter command (9 to end input): 2


Enter command (9 to end input): 5,6
Enter command (9 to end input): 3
Enter command (9 to end input): 5,12
Enter command (9 to end input): 3
Enter command (9 to end input): 5,12
Enter command (9 to end input): 3
Enter command (9 to end input): 5,12
Enter command (9 to end input): 1
Enter command (9 to end input): 6
Enter command (9 to end input): 9
The drawing is:
*******
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*******
Solution
Exercice 1 :
Exercice 2 :
Exercice 3 :
Exercice 4 :
Exercice 5 :
Exercice 6:

You might also like