08.01.2023 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 ■ Arrays

• The first time, we compare num[1] with num[0]; since num[1], 72, is larger

than num[0], 25, we update big to 1. This means that the largest number so

far is in position 1.

• Next, we compare num[2], 17, with num[big] (that is, num[1]), 72; since

num[2] is smaller than num[1], we go on to the next number, leaving big at 1.

• Next, we compare num[3], 43, with num[big] (that is, num[1]), 72; since

num[3] is smaller than num[1], we go on to the next number, leaving big at 1.

• Next, we compare num[4], 84, with num[big] (that is, num[1]), 72; since

num[4] is larger than num[1], we update big to 4. This means that the largest

number so far is in position 4.

• Next, we compare num[5], 14, with num[big] (that is, num[4]), 84; since

num[5] is smaller than num[4], we go on to the next number, leaving big at 4.

• Next, we compare num[6], 61, with num[big] (that is, num[4]), 84; since

num[6] is smaller than num[4], we go on to the next number, leaving big at 4.

• Since there is no next number, the process ends with the value of big being

4, the position of the largest number. The actual number is denoted by

num[big]; since big is 4, this is num[4], which is 84.

We can express the process just described by the following pseudocode:

big = 0

for h = 1 to 6

if num[h] > num[big] then big = h

endfor

print "Largest is ", num[big], " in position ", big

We will now write a function, getLargest, to find the largest value in an array. To be general,

we will specify which portion of the array to search for the value. This is important since, most

times, we declare an array to be of some maximum size (100, say) but do not always put 100

values in the array.

When we declare the array to be of size 100, we are catering for 100 values. But, at any time,

the array may have less than this amount. We use another variable (n, say) to tell us how many

values are currently stored in the array. For example, if n is 36, it means that values are stored in

elements 0 to 35 of the array.

So when we are finding the largest, we must specify which elements of the array to search. We

will write the function such that it takes three arguments – the array num, and two integers lo and

hi—and returns the position of the largest number from num[lo] to num[hi], inclusive. It is up

to the caller to ensure that lo and hi are within the range of subscripts declared for the array. For

instance, the call

• getLargest(score, 0, 6) will return the position of the largest number

from score[0] to score[6]; and the call

• getLargest(mark, 10, 20) will return the position of the largest number

from mark[10] to mark[20].

234

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!