dimanche 25 octobre 2015

C++ Closest Array Search

OK so for my homework I have to make an array with 5 random values in it, ask the user for a number to search, then return the index of the number searched. All this is easy(or at least ive done it) but now if the number the user enters doesn't exist in the array then i have to still return the closest to it.

Example 1:
Int iArr[SIZE] = {45, 38, 198, 36, 781};

Int iSearchTarget = 199; Returned Index = 2 (the index of 198)

. . .

Example 2:
Int iArr[SIZE] = {45, 38, 198, 36,781};

Int iSearchTarget = 37;

Returned Index = 1 (the index of 38)




Aucun commentaire:

Enregistrer un commentaire