08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 ■ Searching, Sorting, and Merging

Write a program to read a set of n arbitrary positive integers (assume n < 100) and

print their mode; n is not given but 0 indicates the end of the data.

Write an efficient program to find the mode if it is known that the numbers all

lie between 1 and 999, inclusive, with no restriction on the amount of numbers

supplied; 0 ends the data.

8. An array num contains k numbers in num[0] to num[k-1], sorted in descending

order. Write a function insertInPlace which, given num, k and another number

x, inserts x in its proper position such that num[0] to num[k] are sorted in

descending order. Assume the array has room for x.

9. A multiple-choice examination consists of 20 questions. Each question has 5

choices, labeled A, B, C, D, and E. The first line of data contains the correct answers

to the 20 questions in the first 20 consecutive character positions, for example:

BECDCBAADEBACBAEDDBE

Each subsequent line contains the answers for a candidate. Data on a line consists

of a candidate number (an integer), followed by 1 or more spaces, followed by the

20 answers given by the candidate in the next 20 consecutive character positions.

An X is used if a candidate did not answer a particular question. You may assume

all data are valid and stored in a file exam.dat. A sample line is:

4325 BECDCBAXDEBACCAEDXBE

There are at most 100 candidates. A line containing a “candidate number” 0 only

indicates the end of the data.

Points for a question are awarded as follows:– correct answer: 4 points; wrong

answer: -1 point; no answer: 0 points.

Write a program to process the data and print a report consisting of candidate

number and the total points obtained by the candidate, in ascending order by

candidate number. At the end, print the average number of points gained by the

candidates.

10. An array A contains integers that first increase in value and then decrease in value,

for example:

It is unknown at which point the numbers start to decrease. Write efficient code to

copy the numbers from A to another array B so that B is sorted in ascending order.

Your code must take advantage of the way the numbers are arranged in A. (Hint:

perform a merge starting at both ends.)

276

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!