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

Computer science Pointers and dynamic storage. COSC 1410 Spring 2016 Assignment

ID: 3695038 • Letter: C

Question

Computer science Pointers and dynamic storage.

COSC 1410 Spring 2016 Assignment 11: Pointers and Dynamic Storage [1] Objective: This assignment will serve as an exercise for using pointers and dynamic storage. You will be using the new operator to create storage space (without a static variable name) to store data. You will also be using delete to remove data stored in the dynamic storage. [2] Description: In this assignment you will implement a truly dynamic array to store integer values ofa list. In C++, you can use new to create a dynamic array of a size that is determined at run time. This is better than the static array that you must specify the size at compile time. However, the size of the dynamic array is fixed once it is created (at run time). We are going to implement an even more dynamic array such that we can add new elements into the array (or list) without limit theoretically. How is this possible? You create a dynamic array of an initial size first. Whenever it is not enough, replace it with an array twice the size. That will last for a while. The following pseudocode shows how to replace an array of size N with a new array of size 2N. This is possible because we are using dynamic storage, not a static array. int **list; // no class is used in this example list new int [N]; // allocating an array of size N Store values in list int **newlist; / newl is t = new int* [2"N]; // allocating a new array of size 2N copy the values from old list to new list list = newlist; Keep in mind that you have to copy the values in the smaller array to the larger array before deleting the smaller one. Similar technique can be used when replacing a larger array with a smaller one. You have to define a class called dynArray. If you know how to use constructor, you may use a constructor. Otherwise use a function to initialize the array to an empty array. The dynArray must have the following three members: (1) An array of pointer to integer (2) The physical size of the array (initially 2) aka array_capacity, and (3) The actual size of the list (initially 0) aka number_of_elements. Capacity-4 numElement = 2

Explanation / Answer

#include <iostream>
using namespace std;

class dynArray{
public:
   int *array;
   int array_capacity;
   int number_of_elements;
   dynArray(){
       array = new int[2];
       array_capacity = 2;
       number_of_elements = 0;
   }
   void showArray(){
       for(int i = 0; i < number_of_elements; i++){
           cout << "Array[" << i << "] = " << array[i] << " ";
       }
       cout << " Physical Array Size = " << array_capacity;
       cout << " Number of Elements = " << number_of_elements << " ";
   }
   void addElem(){
       doubleSize();
       array[number_of_elements] = number_of_elements + 1;
       number_of_elements++;
       showArray();
   }
   void removeElem(){
       if(number_of_elements == 0){
           cout << "You have no element in the array. Nothing to remove ";
           return;
       }
       number_of_elements--;
       halfSize();
       showArray();
   }
   void doubleSize(){
       if(number_of_elements == array_capacity){
           int *newArr = new int[2 * array_capacity];
           array_capacity *= 2;
           for(int i = 0; i < number_of_elements; i++){
               newArr[i] = array[i];
           }
           array = newArr;
           cout << "*** Array size doubled to " << array_capacity << " ";
       }
   }
   void halfSize(){
       if(number_of_elements == array_capacity / 4){
           int *newArr = new int[array_capacity / 2];
           array_capacity /= 2;
           for(int i = 0; i < number_of_elements / 2; i++){
               newArr[i] = array[i];
           }
           array = newArr;
           cout << "*** Array size reduced to " << array_capacity << " ";
       }
   }
   int menu(){
       cout << "1. Add element 2. Remove element 3. Show array 4. Quit Enter choice: ";
       int choice;
       cin >> choice;
       return choice;
   }
};

int main(){
   dynArray a;
   while(1){
       int choice = a.menu();
       if(choice == 4) break;
       switch(choice){
           case 1:
               a.addElem();
               break;
           case 2:
               a.removeElem();
               break;
           case 3:
               a.showArray();
               break;
           default:
               cout << "Wrong input. Enter again. ";
       }
   }
}

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