Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a List class which has a dynamic integer array with size initially unknow

ID: 3712747 • Letter: C

Question

Create a List class which has a dynamic integer array with size initially unknown. Create an object from this class, ask the user for size of the array in the object, dynamically create it and fill it with random numbers. Then create two more objects and set them to the first object. Replace all odd numbers with their negatives in the second object and replace all even numbers with their negatives in the third object. Print all three arrays on screen. Copy constructor is needed for this question. I need all files .h, .cpp, main.cpp

Explanation / Answer

.h file:

#include<iostream>

using namespace std;

class List{

private:

int *array;

int size;  

public:

List();

List(int sz);

List(const List &obj);

void print();

void odd();

void even();

};

.cpp file:

#include <iostream>
#include "main.h"
using namespace std;


List::List(int sz){
size = sz;
array = new int[size];
for (int i = 0; i < size; ++i)
{
/* code */
array[i]=rand();
}
}


List::List(const List &obj){
size = obj.size;
array = new int[obj.size];
for (int i = 0; i < obj.size; ++i)
{
/* code */
this->array[i] = obj.array[i];
}

}

void List::print(){
cout<<endl;
for (int i = 0; i < size; ++i)
{
cout<<array[i]<<" ";

}
cout<<endl;
}
void List::odd(){
for (int i = 0; i < size; ++i)
{
/* code */
if(array[i]%2==1){
array[i]=-1*array[i];
}
}

}
void List::even(){
for (int i = 0; i < size; ++i)
{
/* code */
if(array[i]%2==0){
array[i]=-1*array[i];
}
}

}

main.cpp file:

#include "main.h"

#include <iostream>

using namespace std;

int main()

{

int size;

cout<<"Enter size of array: ";

cin>>size;

List obj1(size);

List obj2 = obj1; //copy constructor for 1

List obj3 = obj1; //copy constructor for 2

obj2.odd();

obj3.even();

obj1.print();

obj2.print();

obj3.print();

return 0;

}