Complete my C++ program: Instructions: You have to continue on implementing your Array List namely the following functions: Example ArrayList: [10, 30, 40, 50] void addAt(int num, int pos) This method will add the integer num to the posth position of the list. Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the array will now look like this: [10, 20, 30, 40, 50] When the value of pos is greater than the size + 1 or less than one, output "Position value invalid" void removeAt(int pos) Removes the number in the posth position of the list. Performing removeAt(3) in the example list will remove the 3rd element of the list and the updated array will be: [10, 30, 50] When the value of pos is greater than the size or less than one, output "Position value invalid" void removeAll(int num) Removes all instances of num in the array list. In this array [10, 10, 20, 30, 10], performing removeAll(10) will remove all 10's and the list will look like this: [20, 30] void flip() Flips the entire list in the opposite order. In the example array, performing flip() will make the list look like: [50, 40, 30, 10] void plus(int num) Adds the value of num to all elements of the integer list. Performing plus(5) in the example array will make the list look like: [15, 35, 45, 55] int summation() Returns the sum of all values in the array list. Performing summation() in the example array will return 130.
Complete my C++ program:
Instructions:
You have to continue on implementing your Array List namely the following functions:
Example ArrayList: [10, 30, 40, 50]
- void addAt(int num, int pos)
This method will add the integer num to the posth position of the list.
Performing addAt(20, 2) in the example list will add 20 at the 2nd position and the array will now look like this: [10, 20, 30, 40, 50]
When the value of pos is greater than the size + 1 or less than one, output "Position value invalid"
- void removeAt(int pos)
Removes the number in the posth position of the list.
Performing removeAt(3) in the example list will remove the 3rd element of the list and the updated array will be: [10, 30, 50]
When the value of pos is greater than the size or less than one, output "Position value invalid"
- void removeAll(int num)
Removes all instances of num in the array list.
In this array [10, 10, 20, 30, 10], performing removeAll(10) will remove all 10's and the list will look like this: [20, 30]
- void flip()
Flips the entire list in the opposite order.
In the example array, performing flip() will make the list look like: [50, 40, 30, 10]
- void plus(int num)
Adds the value of num to all elements of the integer list.
Performing plus(5) in the example array will make the list look like: [15, 35, 45, 55]
- int summation()
Returns the sum of all values in the array list.
Performing summation() in the example array will return 130.
main.cpp
#include <iostream>
#include "arraylist.h"
using namespace std;
int main() {
List* list = new ArrayList();
int test;
cin >> test;
switch (test) {
case 1:
list->add(10);
list->add(20);
list->remove(10);
list->print();
break;
case 2:
list->add(10);
list->add(30);
list->add(40);
list->add(50);
list->addAt(20, 2);
list->print();
break;
case 3:
list->add(10);
list->add(30);
list->add(40);
list->add(50);
list->removeAt(3);
list->print();
break;
case 4:
list->add(10);
list->add(10);
list->add(20);
list->add(30);
list->add(10);
list->removeAll(10);
list->print();
break;
case 5:
list->add(10);
list->add(30);
list->add(40);
list->add(50);
list->flip();
list->print();
break;
case 6:
list->add(10);
list->add(30);
list->add(40);
list->add(50);
list->plus(5);
list->print();
break;
case 7:
list->add(10);
list->add(30);
list->add(40);
list->add(50);
cout << list->summation();
break;
}
return 0;
}
list.h
class List {
public:
virtual void add(int num) = 0;
virtual void remove(int num) = 0;
virtual int get(int pos) = 0;
virtual int size() = 0;
virtual void addAt(int num, int pos) = 0;
virtual void removeAt(int pos) = 0;
virtual void removeAll(int num) = 0;
virtual void flip() = 0;
virtual void plus(int num) = 0;
virtual int summation() = 0;
virtual void print() = 0;
};
arraylist.h
#include "list.h"
#include <cstdlib>
#include <iostream>
using namespace std;
class ArrayList : public List { // : means "is-a" / extend
int* array;
int index;
public:
// CONSTRUCTOR
ArrayList() {
array = (int*) malloc(10);
index = 0;
}
void add(int num) {
*(array + index) = num;
index++;
}
void remove(int num){ // first num that we found
// FIND the num
for (int i = 0; i < index; i++) {
if (num == *(array + i)) { // we have found the num
for (int j = i; j < index - 1; j++) {
*(array + j) = *(array + j + 1);
}
index--;
return;
}
}
}
int get(int pos){
return *(array + pos - 1);
}
int size(){
return index;
}
void print() {
cout << "[";
for (int i = 0; i < index; i++) {
cout << *(array + i);
if (i != index - 1) {
cout << ", ";
}
}
cout << "]";
}
void addAt(int num, int pos) {
//CODE HERE
}
void removeAt(int pos) {
//CODE HERE
}
void removeAll(int num) {
//CODE HERE
}
void flip() {
//CODE HERE
}
void plus(int num) {
//CODE HERE
}
int summation() {
//CODE HERE
return 0;
}
};
Step by step
Solved in 3 steps with 7 images