Question

In: Computer Science

Below is a class called pointerDataClass that store data in a one-dimensional array using pointer. #include<iostream>...

Below is a class called pointerDataClass that store data in a one-dimensional array using pointer.

#include<iostream>

using namespace std;

class pointerDataClass

{ int maxSize;//variable to store the maximum size of p

int length;//variable to store the number of elements in p

int *p;// pointer to an int array

public:
//Constructor to create an array of the size specified by the parameter size.

pointerDataClass(int size);

//Destructor to deallocate the memory space occupied by the array p

~pointerDataClass();

//the function insertAt inserts num into array p at the position specified by  
//index

void insertAt(int index, int num);

//The function displayData displays all the array elements in p

void displayData();

   };

  1. Implement (write code) all constructors, functions, and the destructor ~pointerDataClass.   
  2. Write a main function to perform the following:
    • Create a pointerDataClass object called list11 with a size of 10 elements
    • Insert the following numbers into the array p of the object list11:
      0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.
    • Display the array p of the object list11

PLEASE DO THIS WITH C++

Also, using declaration of array and using [] notation for accessing element of array

Solutions

Expert Solution

CPP code:

#include<iostream>
using namespace std;

class pointerDataClass{
   int maxSize;//variable to store the maximum size of p
   int length;//variable to store the number of elements in p
   int *p;// pointer to an int array
   public:
   //Constructor to create an array of the size specified by the parameter size.
   pointerDataClass(int size);

   //Destructor to deallocate the memory space occupied by the array p
   ~pointerDataClass();

   //the function insertAt inserts num into array p at the position specified by
   //index
   void insertAt(int index, int num);

   //The function displayData displays all the array elements in p
   void displayData();

};

//Constructor to create an array of the size specified by the parameter size.
pointerDataClass::pointerDataClass(int size){
   // allocate dynamic memory for integer array of size length
   // and assigning it to pointer p
   this->p=new int[size];
   // setting maxSize of array as size
   this->maxSize=size;
   // initially setting the number of elements in array to 0
   this->length=0;

   // initially setting all elements of array to 0
   // to avoid gibberish data
   for(int index=0; index<size; index++){
       this->p[index]=0;
   }
}

//Destructor to deallocate the memory space occupied by the array p
pointerDataClass::~pointerDataClass(){
   // deallocating/freeing dynamic memory of the array
   delete this->p;
}

//the function insertAt inserts num into array p at the position specified by index
void pointerDataClass::insertAt(int index, int num){
   // set value of array element (at location=index) to num
   this->p[index]=num;
   // increase number of elements in array by 1
   this->length++;
}

//The function displayData displays all the array elements in p
void pointerDataClass::displayData(){
   cout<<"Displaying all the elements in the array (from start to end)"<<endl;

   // iteratively display each element of array from start to end
   for(int index=0; index<this->maxSize; index++){
       cout<<this->p[index]<<' ';
   }
   cout<<endl;
}

// main function
int main(){
   // Create a pointerDataClass object called list11 with a size of 10 elements
   pointerDataClass list11(10);

   // inserting 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 to array
   for(int index=0; index<10; index++){
       // at location=index, set value of element=index
       // example, at location 2, set value of element=2
       list11.insertAt(index, index);
   }
   // Display the array p of the object list11
   list11.displayData();

   return 0;
}

Output:


Related Solutions

HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill...
HOW TO PRINT THE CONTENTS OF A TWO-DIMENSIONAL ARRAY USING POINTER ARITHMETIC (USING FUNCTIONS) (C++) Fill out the function definition for "void print_2darray_pointer(double *twoDD, int row, int col)". Should match the output from the function void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col) # include using namespace std;    const int ARRAY_SIZE = 5;    const int DYNAMIC_SIZE = 15;    const int TIC_TAC_TOE_SIZE = 3;    // function definitions:    void print_2darray_subscript(double twoDD[][ARRAY_SIZE], int row, int col)        //...
Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.
Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact {...
#include <iostream> #include <string> #include <iomanip> #include <cstdlib> #include "Contact.h" using namespace std; class Contact { public: Contact(string init_name = "", string init_phone = "000-000-0000"); void setName(string name); void setPhone(string phone); string getName()const; string getPhone()const; friend ostream& operator << (ostream& os, const Contact& c); friend bool operator == (const Contact& c1, const Contact& c2); friend bool operator != (const Contact& c1, const Contact& c2); private: string name, phone; }; Contact::Contact(string init_name, string init_phone) { name = init_name; phone = init_phone;...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void...
Array based application #include <iostream> #include <string> #include <fstream> using namespace std; // Function prototypes void selectionSort(string [], int); void displayArray(string [], int); void readNames(string[], int); int main() {    const int NUM_NAMES = 20;    string names[NUM_NAMES];    // Read the names from the file.    readNames(names, NUM_NAMES);    // Display the unsorted array.    cout << "Here are the unsorted names:\n";    cout << "--------------------------\n";    displayArray(names, NUM_NAMES);    // Sort the array.    selectionSort(names, NUM_NAMES);    //...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using...
Develop and pseudocode for the code below: #include <algorithm> #include <iostream> #include <time.h> #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Static methods used for testing //============================================================================ /** * Display the bid information...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT