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

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...
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() {...
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.
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include...
Data Structure using C++ Complete and test the Sudoku problem main.cpp #include <iostream> #include <cmath> #include "sudoku.h" using namespace std; int main() { cout << "See Programming Exercise 18." << endl; } sudoku.cpp #include <iostream> #include "sudoku.h" using namespace std; sudoku::sudoku() { cout << "Write the definition. ." << endl; } sudoku::sudoku(int g[][9]) { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid() { cout << "Write the definition." << endl; } void sudoku::initializeSudokuGrid(int g[][9]) { cout << "Write...
Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints...
Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints the following summary information: a) all array elements at an even index b) every fifth element c) all elements but the first and last Write a program that demonstrates this method by using an array of 20 random integers from 100 to 200. Q2: Write a method called displayGrid that accepts a two-dimensional array of integer values from 0 to 99 and prints this...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream&...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; class Prescription {    friend ostream& operator<<(ostream&, const Prescription&);    friend istream& operator>>(istream&, Prescription&);    private: int idNum; int numPills; double price;    public: Prescription(const int = 0, const int = 0, const double = 0.0); }; Prescription::Prescription(const int id, const int pills, const double p) {    id = id;    numPills = pills;    price = p; } ostream& operator<<(ostream& out, const Prescription pre) {    out <<...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class...
what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class DivSales { private:    int quarterSales[4];    static double totalSales; public:    void add(int, int, int, int);    int sales(int);    static double getValue()    {        return totalSales;    };    void DivSales::add(int s1, int s2, int s3, int s4)    {        quarterSales[0] = s1;        quarterSales[1] = s2;        quarterSales[2] = s3;        quarterSales[3] = s4;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT