Thursday, July 9, 2009

C++, array letter?

how do i find the index of the smallest and largest character in an array letters of 20 characters?

C++, array letter?
What do you mean, the smallest/largest letter? You mean find the one with the smallest/largest decimal value? If so, it's the same as you would do it with an array of numeric data types.





char letters[20];


/* fill it with some data */


int smallestLocation = 0, largestLocation = 0;


for (int i = 1; i %26lt; 20; i++)


{


if (letters[i] %26lt; letters[smallestLocation]) smallestLocation = i;


if (letters[i] %26gt; letters[largestLocation]) largestLocation = i;


}


cout %26lt;%26lt; "Smallest letter is at index " %26lt;%26lt; smallestLocation %26lt;%26lt; endl;


cout %26lt;%26lt; "Largest letter is at index " %26lt;%26lt; largestLocation;


No comments:

Post a Comment