Program: Linear search through a 1D array of structures


Uses a structure to hold 4 facts about each place.

A 1D array holds several these structures.

For loop begins at first structure in the array, examining the value in a certain field to see if it meets the search criteria.

If it does, details from that structure are displayed on the screen and the count of successful matches is increased by one.

After each search, the number of successful matches is also displayed.

Compiles on a Raspberry Pi, Apple Mac or Linux PC. Alternatively, paste into repl.it

Code below:

// Array of structures - Phil Gardner
#include <iostream>

using namespace std;

// - - - - - - - - - - - - - - - -

// A structure that packages together several
// related data items about a place e.g. town or city.
struct place
{
  string name;
  float population;
  bool inEurope;
  string country;
};

// - - - - - - - - - - - - - - - -

int main()
{
    const int NUM_PLACES = 6;

    // Make a 1D array of place structures
    place destination[ NUM_PLACES ];

    // populate the array with some data abbout the places
    destination[0] = { "London", 8.6, true, "UK" };
    destination[1] = { "Paris", 2.2, true, "France" };
    destination[2] = { "Iquitos", 0.36, false, "Peru" };
    destination[3] = { "New Yawk", 8.4, false, "USA" };
    destination[4] = { "Weston Super Mare", 0.05, true, "UK" };
    destination[5] = { "Melbourne", 5.2, false, "Australia" };

    int count;  // Will be used to count results found


    cout << endl << endl;


    // Find all places in Europe...
    cout << "These places are in Europe..." << endl;
    count = 0;
    for (  int i = 0;  i < NUM_PLACES;  i++  )
    {
        if ( destination[i].inEurope == true )
        {
          cout << '\t' << "Found " << destination[i].name << endl;
          count = count + 1;
        } // end of if decision

    } // end of for loop that searches for European places
    cout << "Found " << count << " places in Europe" << endl;


    cout << endl << endl;


    // Find all places with a population over 5 million...
    cout << "These places have over 5 million residents..." << endl;
    count = 0;
    for (  int i = 0;  i < NUM_PLACES;  i++  )
    {
        if ( destination[i].population > 5 )
        {
          cout << '\t' << "Found " << destination[i].name;
          cout << " with " << destination[i].population << " million residents" << endl;
          count = count + 1;
        } // end of if decision

    } // end of for loop that searches through populations
    cout << "Found " << count << " places with over 5 million residents" << endl;


    cout << endl << endl;


    // Find all non-UK places...
    cout << "These places are outside the UK..." << endl;
    count = 0;
    for (  int i = 0;  i < NUM_PLACES;  i++  )
    {
      if ( destination[i].country != "UK" )
      {
        cout << '\t' << "Found " << destination[i].name;
        cout << " is in " << destination[i].country << endl;
        count = count + 1;
      } // end of if decision

    } // end of for loop that searches for non-UK places
    cout << "Found " << count << " places outside the UK" << endl;


    cout << endl << endl;


   // Find all UK places...
    cout << "These places are in the UK..." << endl;
    count = 0;
    for (  int i = 0;  i < NUM_PLACES;  i++  )
    {
      if ( destination[i].country == "UK" )
      {
        cout << '\t' << "Found " << destination[i].name << endl;
        count = count + 1;
      } // end of if decision

    } // end of for loop that searches for places in the UK
    cout << "Found " << count << " places in the UK" << endl;

} // end of main function