Insert


Program Listing

Insert.cpp

/************************************************************************
*                                                                       *
*   Program:    Insert                                                  *
*                                                                       *
*   Purpose:    This program will read a list of numbers that are in    *
*               increasing order, then insert one more number into      *
*               the list                                                *
*                                                                       *
************************************************************************/

//  Header files

#include <iostream>
#include <string>

using namespace std;

//  Function prototypes

bool Insert(int array[], int& size, int maxSize, int Item);
void ReadList(int array[], int& size, int maxSize);

/************************************************************************
*                                                                       *
*   Function:   main                                                    *
*                                                                       *
*   Purpose:    To read a list of numbers that are in increasing        *
*               order, then insert one more number into the list        *
*                                                                       *
************************************************************************/

int main() {

//  Establish the array

    const int maxSize = 100;
    int array[maxSize];
    int size;

//  Read numbers into the array

    ReadList(array, size, maxSize);

//  Insert a new number into the array

    cout << "Enter a value to be inserted:  " << flush;
    int Item;
    cin >> Item;

    bool Inserted = Insert(array, size, maxSize, Item);

//  Report the results

    if (Inserted)
        cout << Item << " was inserted" << endl;
    else
        cout << Item << " was not inserted" << endl;
    
//  Display the list

    for (int i = 0; i < size; i++)
        cout << array[i] << ' ';
    cout << endl;
    
    return 0;
}

/************************************************************************
*                                                                       *
*   Function:   Insert                                                  *
*                                                                       *
*   Purpose:    To insert a new number into a list of numbers that are  *
*               in increasing order                                     *
*                                                                       *
************************************************************************/

bool Insert(int array[], int& size, int maxSize, int Item) {

//  If list is full, return false

    if (size == maxSize)
        return false;

//  If list is not full, insert new number and return true

    else {
    
    //  Search for the new number's proper position in the list
    //  and shift the list members
    
        int i;
        for (i = size - 1; i >= 0 && Item < array[i]; i--)
            array[i + 1] = array[i];
    
    //  Copy the new number into the list
    
        array[i + 1] = Item;
        size++;
        
        return true;
    }
}

/************************************************************************
*                                                                       *
*   Function:   ReadList                                                *
*                                                                       *
*   Purpose:    To read a list of numbers                               *
*                                                                       *
*   Note:       The input is controlled by a sentinel value             *
*                                                                       *
************************************************************************/

void ReadList(int array[], int& size, int maxSize) {
    const int SENTINEL = -1;

//  Read the first value

    cout << "Enter a list of integers in ascending order (" << SENTINEL
         << " to quit)." << endl;
    int i = 0;
    int value;
    cin >> value;

//  Read remaining values, until the sentinel is read

    while (i < maxSize && value != SENTINEL) {
        array[i++] = value;
        cin >> value;
    }
    
//  Store the size of the list

    size = i;
    return;
}

Sample Run

Enter a list of integers in ascending order (-1 to quit).
30 50 55 60 70 75 80 90 -1
Enter a value to be inserted:  65
65 was inserted
30 50 55 60 65 70 75 80 90 



Return to Lectures page


e-mail me at robbk@hsc.edu

This page was last modified on Tue Dec 7 16:04:57 1999 .

URL: http://people.hsc.edu/faculty-staff/robbk/Coms261/Examples/Insert.html

Return to Coms 261 home page