Program: Searching through a 2D array



STOP PRESS! The full book is now available DIRECTLY from Amazon with lower shipping costs!

This program uses a 2D array to store the subjects in a school timetable, each day taking up a column in the array.


It also uses a 1D array to store the name of each day of the week.


The contents of the timetable are displayed using a pair of  for  loops, one nested inside the other.


Once displayed, the user is asked to type in a subject to search for. The results are then displayed on the screen.





Select and copy the C++ source-code at the bottom of this post.

Paste into a text editor, such as Nano or Geany.


Then save the new file, ending in .cpp


I used timetable.cpp


To compile from the command-line:


    g++ -o timetable.cpp timetable


To run from the command-line:


    ./timetable


In order to see how the nested  for  loops are used to display the contents of the  timetable  array, you can uncomment the call to the  usleep  function and  cout << flush;  statement so that the program pauses for half a second after displaying each subject.



Code is below...


#include <iostream>    // Code to display text on screen
#include <unistd.h>   // To slow down the program with usleep if you want to


using namespace std;    // Using standard names cout and endl

int main()
{

    // These constants control how large the timetable is
    const int DAYS_IN_WEEK = 5;
    const int LESSONS_PER_DAY = 6;


    // These colour codes will be used when displaying the timetable
    const string GREEN = "\e[32m";
    const string NORMAL = "\e[0m";

    const char TAB = "\t";

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

    // This 1D array hold the name of each day of the week
    string dayName[DAYS_IN_WEEK];

    dayName[0] = "Mon";
    dayName[1] = "Tues";
    dayName[2] = "Weds";
    dayName[3] = "Thur";
    dayName[4] = "Fri";


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

    // This 2D array will hold the lessons for each day
    string timetable[LESSONS_PER_DAY][DAYS_IN_WEEK];


    // Set values for Monday - day 0
    timetable[0][0] = "Maths";
    timetable[1][0] = "English";
    timetable[2][0] = "Art";
    timetable[3][0] = "Art";
    timetable[4][0] = "PE";
    timetable[5][0] = "PE";


    // Set value for Tuesday - day 1
    timetable[0][1] = "Science";
    timetable[1][1] = "Science";
    timetable[2][1] = "French";
    timetable[3][1] = "History";
    timetable[4][1] = "French";
    timetable[5][1] = "Maths";


    // Set value for Wednesday - day 2
    timetable[0][2] = "Art";
    timetable[1][2] = "Art";
    timetable[2][2] = "German";
    timetable[3][2] = "Maths";
    timetable[4][2] = "Science";
    timetable[5][2] = "Geog";


    // Set value for Thursday - day 3
    timetable[0][3] = "PE";
    timetable[1][3] = "PE";
    timetable[2][3] = "Drama";
    timetable[3][3] = "Geog";
    timetable[4][3] = "Music";
    timetable[5][3] = "History";


    // Set value for Friday - day 4
    timetable[0][4] = "Music";
    timetable[1][4] = "Art";
    timetable[2][4] = "German";
    timetable[3][4] = "Maths";
    timetable[4][4] = "D&T";
    timetable[5][4] = "D&T";


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

    // Display headings in green
    cout << GREEN << "Lesson\t";
    for ( int day = 0;  day < DAYS_IN_WEEK;  day++ )
        cout << dayName[day] << TAB << TAB;


    // Move on to next line, ready to display lessons
    cout << endl;


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

    // Display the lessons for each day
    for ( int lesson = 0;  lesson < LESSONS_PER_DAY;  lesson++ )
    {

        // Display lesson number in green text
        cout << GREEN << lesson + 1 << NORMAL << "\t";


        // Display name of each lesson in normal colour text
        for ( int day = 0;  day < DAYS_IN_WEEK;  day++ )
        {
            cout << timetable[lesson][day] << TAB << TAB;

            // Slow down the program so you can see how the loops work
            // Uncomment these next 2 lines for slower speed!
            //cout << flush;  // Update screen immediately
            //usleep(500000);  // half a second delay

        }  // end of for loop that controls day number

        // Go on to next line of screen after all days displayed for this row
        cout << endl;

    }  // end of for loop that controls lesson number

    // --------------------
   
    // Search through each day for a particular subject
    cout << endl;
    cout << "Which subject do you want to search for?" << endl;
    string subjectToFind;
    cin >> subjectToFind;
   
    int timesFound = 0;

   
    // Search results will be shown in green
    cout << GREEN;
   
    for ( int day = 0;  day < DAYS_IN_WEEK;  day++ )
    {

        for ( int lesson = 0;  lesson < LESSONS_PER_DAY;  lesson++ )
        {

            // Determine whether this is the subject you are searching for
            if ( subjectToFind == timetable[lesson][day] )
            {
                cout << "Lesson " << lesson + 1;
                cout << " on a ";
                cout << dayName[day] << endl;
                timesFound++;
            }


        }  // end of for loop that controls day number
    }  // end of for loop that controls lesson number


    // Warn user if the subject could not be found
    if ( timesFound == 0 )
        cout << "None found." << endl;

 
    // Set text back to normal colours for your terminal
    cout << NORMAL;
   
    // --------------------
   
    return 0;
}



Find out more - buy the book on Amazon...