Searching arrays
To wrap up this part of the tour, let’s finish off by looking at how we can combine other control flow statements to work on things like searching through an array. This will help you gain a better understanding of how to work with arrays.
Have a go at building the shortest name function, and then build your own index of function. Being able to work with array indexes can be very useful, so have a go at exploring this with your code.
- #include "splashkit.h"using std::to_string;string read_string(string prompt){string result;write(prompt);result = read_line();return result;}int read_integer(string prompt){string line;line = read_string(prompt);return convert_to_integer(line);}/*** @brief Calculate the total length of all the names in the array** @param names the array of names* @param size the number of names in the array* @return int with the total length of all the names*/int total_length(string names[], int size){// Start the result at 0 - if there are no names the total length is 0int result = 0;// for each element in the arrayfor(int i = 0; i < size; i++){// Access the element...string name = names[i];// Add its length to the resultresult += length_of(name);}// Return the total lengthreturn result;}/*** @brief Tests if an array contains a given name** @param names the array of names to search* @param size the number of names in the array* @param name the name to search for* @return true when the name is found (case insensitive)* @return false when the name is not found*/bool contains(string names[], int size, string name){for(int i = 0; i < size; i++){if( to_lowercase(names[i]) == to_lowercase(name) ){return true;}}return false;}/*** @brief Returns the shortest name in the array** @param names the array of names* @param size the number of names in the array* @return string the shortest name in the array*/string shortest_name(string names[], int size){string min;// Return an empty string if the array is emptyif (size == 0){return "";}// Assume the first name is the shortest...min = names[0];// Search the rest of the array to find a shorter namefor(int i = 1; i < size; i++){// Is the current name in the array shorter?if ( length_of(min) > length_of(names[i]) ){// It is... so store it as the new shortest namemin = names[i];}}// Return the shortest name we foundreturn min;}int main(){// Declare a constant for the size of the arrayconst int SIZE = 3;// Declare an array of three strings// The array is called namesstring names[SIZE];// Create a variable to store the index of the array// This can be used to control the loops belowint i;// Assign values to the elements within the array// Start i at the first index - index 0i = 0;while ( i < SIZE ) // loop while i is less than the number of elements{// Store in the i-th element of the array// i changes each loop - so this will store in names[0], names[1], ...names[i] = read_string("Enter a name: ");i = i + 1; // increment i... could be written as i++}// Access elements in the array to write out names entered// Use a for loop to combine the loop control// - i is initialised to 0// - the loop continues while i is less than SIZE// - i is incremented each loop (at the end)for(i = 0; i < SIZE; i++){write_line(names[i]);}// Use the total_length function to calculate the total length of all the nameswrite_line("Total length of all names: " + to_string(total_length(names, SIZE)));bool has_andrew;has_andrew = contains(names, SIZE, "andrew");if ( has_andrew ){write_line("Contains Andrew");}write_line(shortest_name(names, SIZE));return 0;}
Hopefully this activity will have helped you see how to get started using arrays in your code.