Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

All code within the ***...*** needs to be implemented! Variable Size Array with

ID: 3691350 • Letter: A

Question

All code within the ***...*** needs to be implemented!

Variable Size Array with Classes, Implementation. Create a project titled Lab11_VarArrayClasses. Using the class ***(http://vega.cs.kent.edu/~mikhail/classes/csi/Labs/Lab11/vararray.h)*** implemented in the first part of this lab, implement the functionality of the second part of the previous lab. That is, write a program that asks the user to input numbers to add to and remove from the array and then prints its contents in the following format. A user may enter the number twice in which case, the repeated number should not be entered.

Here is an example program dialog:

The size of the user input can be arbitrarily large. For this program, to accommodate user input, you need to implement an integer array whose size varies as necessary. The numbers in the array should not be sorted.

Explanation / Answer

/*
* varArray.cpp
*
* Created on: Apr 19, 2016
*      Author: Satish-Ravi
*/

#include "varArray.h"

#include <iostream>
using namespace std;

varArray::varArray() {
   // TODO Auto-generated constructor stub
   dArray = NULL; // allocated on heap
   size = 0;


}

int varArray::arraySize() {
   return size;
} // returns the size of the array

int varArray::check(int number) // returns index of element containg "number" or -1 if none
{
      for(int i = 0; i < size;i++) {
          if(dArray[i] == number)
              return i;
      }
      return -1;
}
void varArray::addNumber(int data)    // adds number to the array
{
      if(dArray == NULL) {
          size = 0;
          dArray = new int[1];
          dArray[size++] = data;
      } else {
              int* temp = new int[size+1]; // create new bigger array.
                 for (int i=0; i<size; i++) {
                     temp[i] = dArray[i];       // copy values to new array.
                 }
                 temp[size++] = data;
                 delete [] dArray;              // free old array memory.
                 dArray = temp;

      }

}

void varArray::removeNumber(int num)// deletes the number from the array
{
      int* temp = new int[size];
      int cnt = 0;
      bool removed = false;
      for(int i = 0; i <size; i++) {
          if(dArray[i] != num)
              temp[cnt++] = dArray[i];
          else
              removed = true;
      }
      if(removed == true) {
      delete [] dArray;              // free old array memory.
      dArray = temp;
      size--;
      }

}
void varArray::output()     // prints the values of the array
{
      for(int i = 0; i <size; i++)
          cout << dArray[i] << " ";
      cout <<endl;
}

varArray::~varArray() {
   // TODO Auto-generated destructor stub
}


int main() {
   varArray arr;
   char option;
   int number;
   do {
       cout << "enter operation [a/r/q] and number:" <<endl;
       cin >> option;
       if(option == 'q')
           break;
       cin >> number;
       if(option == 'a')
           arr.addNumber(number);
       if(option == 'r')
           arr.removeNumber(number);
       arr.output();

   } while(option !='q');

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote