Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design a C++ class cCheck that encapsulates an array of numbers and: returns the kth number from the array upon the kth query(), for example:

  1. Design a C++ class cCheck that encapsulates an array of numbers and:
  1. returns the kth number from the array upon the kth query(), for example:

If the array is [7, 14, 25, 15]

the first call to query() returns 7, the second call returns 14,

  1. when the last number in the array is returned from query()

resets the query count

expands the array: e.g. [7, 14, 25, 15] becomes, say, [7, 14, 25, 15, 35]

  1. supports replacing the encapsulated array only if the replacement is not smaller
  2. supports efficient, deep copying

Define this class, providing all implementation details.

You must use arrays and may not use another container (such as vector).

public class cCheck {

private int[] arr;

private const int DEFAULT_SIZE = 10;

private const int ARRAY_MULTIPLIER = 5;

private uint queryCount;

public cCheck() {

queryCount = 0;

arr = new int[DEFAULT_SIZE];

for (int k = 0; k < DEFAULT_SIZE; k++) {

arr[k] = (k+1) * ARRAY_MULTIPLIER;

}

}

public int query() {

if (queryCount == arr.length()) {

queryCount = 0;

arr = new int[arr.length() * 2];

for (int k = 0; k < arr.length(); k++) {

arr[k] = (k+1) * ARRAY_MULTIPLIER;

}

}

int save = arr[queryCount];

queryCount++;

return save;

}

public void replace(int[] x) {

if (x.length() > arr.length()) {

delete[] arr;

arr = new int[x.length()];

for (int k = 0; k < arr.length(); k++) {

arr[k] = x[k];

}

}

}

public void copy(const cCheck& src) {

arr = new int[src.length()]

for (int k = 0; k < src.length(); k++) {

arr[k] = src.arr[k];

}

queryCount = src.queryCount;

}

public void operator=(const cCheck& src) {

if (this == &src) return;

delete[] arr;

copy(src);

return *this;

}

public ~cCheck() {

delete[] arr;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions