Question
Need Help on C++ programming: WRITE ALL THE CODE in ONE FILE Convert Program 1 to a template. Test with and and Use the test
Need Help on C++ programming:
WRITE ALL THE CODE in ONE FILE
Convert Program 1 to a template.
Test with
Use the test found above for program 1
This is programing 1:
#include
using namespace std;
class MyArrayClass
{
//Add an 'arraySize' variable, initialize to zero ( default constructor function )
int arraySize;
//Create int * ptrArray ( default constructor set to NULL )
int *ptrArray;
public:
//Add a default constructor ( see above for what is should do )
MyArrayClass()
{
arraySize = 0;
ptrArray = NULL;
}
// Add a parm constructor that can set the array size,
// and then assigns new int[arraySize] to ptrArray. Validate size > 0.
MyArrayClass(int size)
{
if(size > 0)
{
arraySize = size;
ptrArray = new int[arraySize];
}
}
// Add a setSize function that lets the user input a size, arraySize, of an array of integers,
// and then assigns new int[arraySize] to ptrArray ( only if it is NULL ). Validate size > 0.
void setSize()
{
int size;
cout<<"Enter the size of the array: ";
cin>>size;
while(size <= 0)
{
cout<<"Size should be greater than zero."< cout<<"Enter the size of the array: "; cin>>size; } if(ptrArray == NULL) { arraySize = size; ptrArray = new int[arraySize]; } } // Add a function, setAllValues, Have the user prompted and then enter the values for the // array. Validate that ptrArray != NULL, if so then add values . Use pointer arithmetic // to specify the index of the array while the user is entering the values into the array. void setAllValues() { if(ptrArray != NULL) { cout << "Enter " << arraySize << " values to read into the array: "; for(int i = 0; i < arraySize; i++) cin >> *(ptrArray + i); } } // Add a printAll function that prints out the array...values using pointer arithmetic. // Validate that ptrArray != NULL, if so then print out all values void printAll() { if(ptrArray != NULL) { for(int i = 0; i < arraySize; i++) cout << *(ptrArray + i) << "\t"; cout << endl; } } }; int main() { // Step 2 - Declare and Step 3 use it // Test default constructor MyArrayClass Array1; Array1.setSize(); Array1.setAllValues(); // Code a Loop that asks for input for each value one at a time // Input 10,10,20,25,30,35,42 Array1.printAll(); // Test parm constructor MyArrayClass Array2(7); Array2.setAllValues(); Array2.printAll(); // Test with default constructor MyArrayClass * ptrArray1 = new MyArrayClass(); ptrArray1->setSize(); // add code to call setSize function, use 7 ptrArray1->setAllValues(); // add code to call setAllValues function: input 100,150,200,250,300,350,420 ptrArray1->printAll(); // add code to call printAll function }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started