Question

Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can...

Lanuage C++ (Beginner level)

Please...I need help with this assignment

If I still have question, can i contact you?

--------------------------

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for MyArrayClass.cpp:

#include <iostream>
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."<<endl;
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;  
        }
}
};

And the code for MyArrayClassDemo.cpp:

#include "MyArrayClass.cpp"
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
}

And the output screenshot is:

Terminal Shell Edit View Window Help 1.73 GB <D < > u. [.] -))) 96% E ], Thu 23 Mar 13:21 ANANDA KUMAR THUMMAPUDI a E Current

Add a comment
Know the answer?
Add Answer to:
Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Compute for Miles Per Gallon in C++

    Make a program that will calculate and compute for the quotient of miles and gallons (mpg: miles per gallons). Your program should must ask the user to specify the size of the array (using dynamic array) for the following variable: miles ,gallons and mpg. Prompt the user to Initialize  the value of miles (value for miles should be 100-250) and gallons (values should be from 5-25). Use pointer galPtr for gallons, milPtr for miles and mpgPtr for mpg.  Use function...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • 2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration....

    2 Class Vec Message1 2.1 Introduction • Write a class Vec Message1 with the following declaration. class Vec_Message1 { public: Vec_Message1(); Vec_Message1(int n); Vec_Message1(int n, const Message1 &a); Vec_Message1(const Vec_Message1 &orig); Vec_Message1& operator= (const Vec_Message1 &rhs); ~Vec_Message1(); int capacity() const; int size() const; Message1 front() const; Message1 back() const; void clear(); void pop_back(); void push_back(const Message1 &a); Message1& at(int n); private: void allocate(); void release(); int _capacity; int _size; Message1 * _vec; }; 2.2 allocate() and release() • Examine the...

  • I have updated my previously posted C++ question to ensure that I have included all of...

    I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that...

  • I need help with the following code... Instructions: This lab builds on the skills from Lab...

    I need help with the following code... Instructions: This lab builds on the skills from Lab 2c, in which you read data values from a file and kept a count of the number of invalid values. In this lab, your program will be using the same code to read each data entry from the file, but you will also save each value in an array named allMags after each is read in. When all values in the file have been...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT