Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ homework 5 // -------------------------------------------------------------------------------- // Pre-compiler Directives // -------------------------------------------------------------------------------- #pragma once // Include this file only once even if referenced multiple times // --------------------------------------------------------------------------------

C++

homework 5

// --------------------------------------------------------------------------------

// Pre-compiler Directives

// --------------------------------------------------------------------------------

#pragma once // Include this file only once even if referenced multiple times

// --------------------------------------------------------------------------------

// Includes

// --------------------------------------------------------------------------------

#include

#include

#include

template

class CResizableArray {

private:

T* m_paingValues;

long m_IngArraySize;

public:

CResizableArray() : m_paingValues(nullptr), m_IngArraySize(0) {

}

~CResizableArray() {

delete[] m_paingValues;

}

void Initialize() {

delete[] m_paingValues;

m_paingValues = nullptr;

m_IngArraySize = 0;

}

void SetSize(long IngNewSize) {

if (IngNewSize < 0) {

throw std::invalid_argument("Invalid size: size cannot be negative.");

}

T* newValues = new T[IngNewSize]();

long copySize = std::min(m_IngArraySize, IngNewSize);

for (long i = 0; i < copySize; i++) {

newValues[i] = m_paingValues[i];

}

delete[] m_paingValues;

m_paingValues = newValues;

m_IngArraySize = IngNewSize;

}

long GetSize() const {

return m_IngArraySize;

}

void SetValueAt(T IngValue, long IngIndex) {

if (IngIndex < 0 || IngIndex >= m_IngArraySize) {

throw std::out_of_range("Invalid index: index is out of range.");

}

m_paingValues[IngIndex] = IngValue;

}

T GetValueAt(long IngIndex) const {

if (IngIndex < 0 || IngIndex >= m_IngArraySize) {

throw std::out_of_range("Invalid index: index is out of range.");

}

return m_paingValues[IngIndex];

}

void AddValueToFront(T IngValue) {

InsertValueAt(IngValue, 0);

}

void AddValueToEnd(T IngValue) {

InsertValueAt(IngValue, m_IngArraySize);

}

void InsertValueAt(T IngValue, long IngIndex) {

if (IngIndex < 0) {

IngIndex = 0;

}

else if (IngIndex > m_IngArraySize) {

IngIndex = m_IngArraySize;

}

T* newValues = new T[m_IngArraySize + 1]();

for (long i = 0, j = 0; i <= m_IngArraySize; i++, j++) {

if (i == IngIndex) {

newValues[i] = IngValue;

j--;

}

else {

newValues[i] = m_paingValues[j];

}

}

delete[] m_paingValues;

m_paingValues = newValues;

m_IngArraySize++;

}

void RemoveAt(long IngIndex) {

if (IngIndex < 0 || IngIndex >= m_IngArraySize) {

throw std::out_of_range("Invalid index: index is out of range.");

}

T* newValues = new T[m_IngArraySize - 1]();

for (long i = 0, j = 0; i < m_IngArraySize - 1; i++, j++) {

if (j == IngIndex) {

j++;

}

newValues[i] = m_paingValues

[j];

}

delete[] m_paingValues;

m_paingValues = newValues;

m_IngArraySize--;

}

T& operator[](long IngIndex) {

if (IngIndex < 0 || IngIndex >= m_IngArraySize) {

throw std::out_of_range("Invalid index: index is out of range.");

}

return m_paingValues[IngIndex];

}

};

int main() {

CResizableArray myArray;

myArray.SetSize(4);

myArray.SetValueAt(2, 0);

myArray.SetValueAt(4, 1);

myArray.SetValueAt(6, 2);

myArray.SetValueAt(8, 3);

std::cout << "Before:" << std::endl;

for (long i = 0; i < myArray.GetSize(); i++) {

std::cout << myArray.GetValueAt(i) << std::endl;

}

myArray.AddValueToFront(10);

std::cout << "After adding value to the front:" << std::endl;

for (long i = 0; i < myArray.GetSize(); i++) {

std::cout << myArray.GetValueAt(i) << std::endl;

}

myArray.InsertValueAt(5, 2);

std::cout << "After inserting value in the middle:" << std::endl;

for (long i = 0; i < myArray.GetSize(); i++) {

std::cout << myArray.GetValueAt(i) << std::endl;

}

myArray.RemoveAt(2);

std::cout << "After removing value:" << std::endl;

for (long i = 0; i < myArray.GetSize(); i++) {

std::cout << myArray.GetValueAt(i) << std::endl;

}

return 0;

}

Step 1

  1. Continued with your code from homework 5. I recommend you make a copy of the folder/project and keep your homework 5 separate.

  1. Add the following constructors.

// constructors

CResizableArray ();

CResizableArray ( long lngInintialSize);

CResizableArray ( long lngInintialSize, long lngDefaultValue );

  1. Make the Initialize method protected and add two parameters: initial size and default value. Call Initialize from all the the constructors.

  1. Add a destructor and a protected clean up method.Call clean up from the destructor.Delete any allocated memory.Either delete any allocated memory and set all properties to zero or call SetSize( 0 ). Either way make sure you dispose of memory correctly.

  1. Make get size a constant method.

  1. Make get value at a constant method.

  1. Make print a constant method. Make the caption parameter a const char*.

  1. Make sure you test your code thoroughly. For example, insert a value into a size zero array and remove a value from a size zero array. WARNING: do not procedurize any code in main. You may exceed the 1-page limit for main for this assignment.

(continued on the next page)

Step 2 - A Mystery

  1. Using your CResizableArray class, create the following main and test subroutines. Run your code. Make sure the application terminates normally. Do NOT click the X on the command window.

// --------------------------------------------------------------------------------

// Prototypes

// --------------------------------------------------------------------------------

void Test1( CResizableArray &clsValues ); // By Reference

void Test2( CResizableArray clsValues ); // By Value

// --------------------------------------------------------------------------------

// Name: main

// Abstract: This is where the program starts.

// --------------------------------------------------------------------------------

void main()

{

CResizableArray clsValues;

// Create the array

clsValues.SetSize(4);

// Populate Array (value, index)

clsValues.SetValueAt( 2, 0 );

clsValues.SetValueAt( 4, 1 );

clsValues.SetValueAt( 6, 2 );

clsValues.SetValueAt( 8, 3 );

// Test 1

Test1( clsValues );

printf( "After Test 1 ------------------------------ " );

clsValues.Print( );

// Test 2

Test2( clsValues );

printf( "After Test 2 ------------------------------ " );

clsValues.Print( );

}

// --------------------------------------------------------------------------------

// Name: Test1

// Abstract: Pass by reference

// --------------------------------------------------------------------------------

void Test1( CResizableArray &clsValues )

{

printf( "Test 1 ------------------------------ " );

clsValues.Print( );

}

// --------------------------------------------------------------------------------

// Name: Test2

// Abstract: Pass by value

// --------------------------------------------------------------------------------

void Test2( CResizableArray clsValues )

{

printf( "Test 2 ------------------------------ " );

clsValues.Print( );

}

(continued on the next page)

Step 3 - What is happen?

  1. Explain in comments, at the top of your program, what is happening. Your answer is worth 31% of the grade for this assignment.
  2. Propose one or more possible solutions. Making variables global is a hack not a solution. Always passing by reference is a hack not a solution.

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

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions