C++ Program 2 - WRITE ALL THE CODE in ONE FILE... MyArrayPtrArithTemp2.cpp Conve
ID: 3595163 • Letter: C
Question
C++
Program 2 - WRITE ALL THE CODE in ONE FILE... MyArrayPtrArithTemp2.cpp
Convert Program 1 to a template.
Test with and and
Use the test found above for program 1…
Program 1 - WRITE ALL THE CODE in ONE FILE... MyArrayPtrArith1.cpp
Step 1 - Define the class
Write a class, myArrayClass, that creates an integer array.
* Add an 'arraySize' variable, initialize to zero ( default constructor function )
* Create int * ptrArray ( default constructor set to NULL )
* Add a default constructor ( see above for what is should do )
* Add a parm constructor that can set the array size,
and then assigns new int[arraySize] to ptrArray. Validate size > 0.
* 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.
* 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.
* Add a printAll function that prints out the array...values using pointer arithmetic. Validate that ptrArray != NULL, if so then print out all values
---
testing: - code for int main() --
// Step 2 - Declare and Step 3 use it
// Test default constructor
myArrayClass Array1;
Array1.setSize(6);
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(7) Array2;
Array2.setAllValues();
Array2.printAll();
// Test with default constructor
myArrayClass * ptrArray1 = new myArrayClass;
....... // add code to call setSize function, use 7
....... // add code to call setAllValues function: input 100,150,200,250,300,350,420
....... // add code to call printAll function
Explanation / Answer
CASE 1
#include <bits/stdc++.h>
using namespace std;
class myArrayClass
{
// Access specifier
public:
// Data Members
int arraySize=0;
int i=0;
int *ptrArray;
myArrayClass(){
ptrArray=NULL;
}
myArrayClass(int size){
if(size>0){
ptrArray=new int[size];
arraySize=size;
}
}
// Member Functions()
void printAll()
{
if(ptrArray!=NULL){
cout<<"Array values are::"<<" ";
for(i=0;i<arraySize;i++)
{
cout<<ptrArray[i]<<" ";
}
}
}
void setAllValues()
{
if(ptrArray!=NULL){
for(i=0;i<arraySize;i++)
{
cout<<"Enter value::";
cin>>ptrArray[i];
}
}
}
void setSize(int size){
if(ptrArray==NULL){
if(size>0){
arraySize=size;
ptrArray=new int[arraySize];
}
}
}
};
int main() {
myArrayClass Array1;
Array1.setSize(6);
Array1.setAllValues();
Array1.printAll();
return 0;
}
output:
Enter value::10
Enter value::20
Enter value::30
Enter value::40
Enter value::50
Enter value::60
Array values are:: 10 20 30 40 50 60
CASE 2
#include <bits/stdc++.h>
using namespace std;
class myArrayClass
{
// Access specifier
public:
// Data Members
int arraySize=0;
int i=0;
int *ptrArray;
myArrayClass(){
ptrArray=NULL;
}
myArrayClass(int size){
if(size>0){
ptrArray=new int[size];
arraySize=size;
}
}
// Member Functions()
void printAll()
{
if(ptrArray!=NULL){
cout<<"Array values are::"<<" ";
for(i=0;i<arraySize;i++)
{
cout<<ptrArray[i]<<" ";
}
}
}
void setAllValues()
{
if(ptrArray!=NULL){
for(i=0;i<arraySize;i++)
{
cout<<"Enter value::";
cin>>ptrArray[i];
}
}
}
void setSize(int size){
if(ptrArray==NULL){
if(size>0){
arraySize=size;
ptrArray=new int[arraySize];
}
}
}
};
int main() {
myArrayClass Array1(7);
Array1.setAllValues();
Array1.printAll();
return 0;
}
output:
Enter value::10
Enter value::20
Enter value::30
Enter value::40
Enter value::50
Enter value::60
Enter value::70
Array values are:: 10 20 30 40 50 60 70
CASE 3
#include <bits/stdc++.h>
using namespace std;
class myArrayClass
{
// Access specifier
public:
// Data Members
int arraySize=0;
int i=0;
int *ptrArray;
myArrayClass(){
ptrArray=NULL;
}
myArrayClass(int size){
if(size>0){
ptrArray=new int[size];
arraySize=size;
}
}
// Member Functions()
void printAll()
{
if(ptrArray!=NULL){
cout<<"Array values are::"<<" ";
for(i=0;i<arraySize;i++)
{
cout<<ptrArray[i]<<" ";
}
}
}
void setAllValues()
{
if(ptrArray!=NULL){
for(i=0;i<arraySize;i++)
{
cout<<"Enter value::";
cin>>ptrArray[i];
}
}
}
void setSize(int size){
if(ptrArray==NULL){
if(size>0){
arraySize=size;
ptrArray=new int[arraySize];
}
}
}
};
int main() {
myArrayClass * ptrArray1 = new myArrayClass;
ptrArray1->setSize(7);
ptrArray1->setAllValues();
ptrArray1->printAll();
return 0;
}
output:
Enter value::10
Enter value::20
Enter value::30
Enter value::40
Enter value::50
Enter value::60
Enter value::70
Array values are:: 10 20 30 40 50 60 70
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.