Implementing the Linear Array Algorithm/Data Structure

Solution

In this tutorial, we will learn how to implement a Linear Array Data Structure/Algorithm. 

Using some of the OOP concepts to implement the Algorithm, we will use the likes of classes and functions.

What we will learn:

  • How to implement the insert functionality
  • How to implement the  edit functionality
  • How to implement the display functionality

The code snippet below:

Linear Array

class LinearArray{
public:
    void LinearArrayMenu(){
    //int n;
    //cin >> n;
    int n,x,i,flag=0;
    cout<<"How many elements: ";
    cin>>n;
    string name[n];
    int arr[n], listitems[n];
    for(i=0;i<n; i++){
        cout<<"\nEnter names of students for the array at position: " << i << "\n";
        cin>>name[i];
        listitems[i] = arr[i];
    }
    cout << "Your list of items: ";
    for(i=0; i<n; i++){
        cout <<name[i] <<" ";
    }
    for (int i = 0; i < n; i++) {
    cout <<"\nposition" << "(" << i << "): " << name[i] << "\n";
    }
    cout << "Insert an element at any position of your choice from your list to update after inserting new elements\n" << endl;
    string new_item;

    cout << "Choose position to insert new elements: ";
    int k;
    cin >> k;
    int j = n - 1;
    cout << "\nelements after inserting new elements" << endl;
    while (j >= k) {
    name[j] = name[j - 1];
    j = j - 1;
  }
    cout << "Enter new item: ";
    cin >> new_item;
    name[k] = new_item;
    for (int i = 0; i < n; i++) {
    cout <<"\nposition" << "(" << i << "): " << name[i] << "\n";
    }
        }
};

 

CSC305 Exam Focus/Materials 4/4/2021 Seth 0 Answer(s) 484 views



Author

Seth 1413

Leave a reply

Post your answer

Answers ( 0 )