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

#ifndef ARRAY_H_INCLUDED #define ARRAY_H_INCLUDED #include <iostream> #include <

ID: 3730670 • Letter: #

Question

#ifndef ARRAY_H_INCLUDED
#define ARRAY_H_INCLUDED

#include <iostream>
#include <string>

using namespace std;

/* error codes
0 -- no error
1 -- non-positive size for constructor
2 -- invalid index was used
4 -- non-postitive newSize for changeSize
*/

template <class T>
class Array {
public:
Array(int size);
~Array();
Array(const Array<T> & a);
Array<T> & operator = (const Array<T> & a);

T & operator [] (int index);
void changeSize (int newSize);
int length();
string err();

private:
T * els;
int capacity;
T dud;
int errorCode;
void deepCopy(const Array<T> & a);
};

template <class T>
Array<T>::Array(int size) {
cout << "constructor" << endl;
if (size < 1) { size = 1; errorCode = 1; }
else { errorCode = 0; }
els = new T[size];
capacity = size;
}

template <class T>
Array<T>::~Array() {
delete [] els;
}

template <class T>
Array<T>::Array(const Array<T> & a) {
deepCopy(a);
}

template <class T>
Array<T> & Array<T>::operator = (const Array<T> & a) {
if (this == & a) return *this;
delete [] els;
deepCopy (a);
return * this;
}

template <class T>
void Array<T>::deepCopy (const Array<T> & a) {
capacity = a.capacity;
errorCode = a.errorCode;
els = new T[capacity];
for (int i = 0; i < capacity; i++) {
els[i] = a.els[i];
}
}

template <class T>
T & Array<T>::operator [](int index) {
if (index < 0 || index >= capacity) {
errorCode |= 2; return dud;
}
return els[index];
}

template <class T>
void Array<T>::changeSize(int newSize) {
if (newSize < 0) { errorCode |= 4; return; }
int numEls = (capacity > newSize ? newSize : capacity);
T * temp = new T [newSize];
for (int i = 0; i < numEls; i++) {
temp[i] = els[i];
}
delete [] els;
els = temp;
capacity = newSize;
}

template <class T>
int Array<T>::length() { return capacity; }

template <class T>
string Array<T>::err() {
if (errorCode == 0) return "No error. ";
string s = "";
if (errorCode & 1) s = s + "non-positive size for constructor. Size set to 1. ";
if (errorCode & 2) s = s + "index out of range. ";
if (errorCode & 4) s = s + "non-positive newSize for changeSize. Size not changed. ";
return s;
}

#endif // ARRAY_H_INCLUDED

#ifndef TWITTERARRAYHEADER_H_INCLUDED
#define TWITTERARRAYHEADER_H_INCLUDED

using namespace std;

template <class DT>
class TwitterArray {
public:
TwitterArray(DT u);
void AddFollower(DT p);
void RemoveFollower(DT p);
void PrintFollowers();
private:
DT user;
int numFollowers;
//int capacity;
TwitterArray <DT> followers;
};

template <class DT>
TwitterArray<DT>::TwitterArray(DT u):followers(2) {
user = u;
numFollowers = 0;
//capacity = 2;
followers = new DT[2];
}

template <class DT>
void TwitterArray<DT>::AddFollower(DT p) {
if (followers.Length() <= numFollowers) {
followers.changeSize(followers.Length() * 2);
return;
}
}
template <class DT>

void TwitterArray<DT>::RemoveFollower(DT p) {

int index = -1;
for (int i = 0; i < numFollowers; i++) {
if (followers[i] == p) {
index = i;
}
}
for (int i = index; i < numFollowers - 1; i++) {
followers[i] = followers[i + 1];
}
numFollowers --;
}

template <class DT>
void TwitterArray<DT>::PrintFollowers() {
for (int i = 0; i < numFollowers; i++) {
cout << followers[i] << ", ";
}
cout << endl;
}

#endif // TWITTERARRAYHEADER_H_INCLUDED

#include <iostream>
#include <string>
#include "TwitterArrayHeader.h"
#include "Array.h"

using namespace std;

struct Profile {
string userName;
int age;
string state;

bool operator == (Profile p2) {
if (p2.userName == userName) return true;
else return false;
}
};

ostream& operator << (ostream & output, Profile p) {
output << p.userName;
return output;
}

int main()
{
TwitterArray<string> user1 ("P1");
user1.AddFollower("P2");
user1.PrintFollowers();
user1.AddFollower("P3");
user1.PrintFollowers();
user1.AddFollower("P4");
user1.PrintFollowers();
user1.RemoveFollower("P3");
user1.PrintFollowers();

Profile up; up.userName = "UP1";
TwitterArray<Profile> user2 (up);
up.userName = "UP2";
user2.AddFollower(up);
user2.PrintFollowers();
up.userName = "UP3";
user2.AddFollower(up);
user2.PrintFollowers();
up.userName = "UP4";
user2.AddFollower(up);
user2.PrintFollowers();
up.userName = "UP3";
user2.RemoveFollower(up);
user2.PrintFollowers();
return 0;
}

Trying to modify the AddFollower, RemoveFollower and PrintFollowers of the TwitterArrayHeader to use the ArrayHeader and run the functions, but keep getting an error. Any help would be greatly appreciated.

Explanation / Answer

Given below is the fixed code for the question.
Please do rate the answer if it was helpful. Thank you


TwitterArrayHeader.h


#ifndef TWITTERARRAYHEADER_H_INCLUDED
#define TWITTERARRAYHEADER_H_INCLUDED

#include "Array.h"
using namespace std;

template <class DT>
class TwitterArray {
public:
TwitterArray(DT u);
void AddFollower(DT p);
void RemoveFollower(DT p);
void PrintFollowers();
private:
DT user;
int numFollowers;
Array<DT> followers;
};

template <class DT>
TwitterArray<DT>::TwitterArray(DT u):followers(Array<DT>(2)){
user = u;
numFollowers = 0;
}

template <class DT>
void TwitterArray<DT>::AddFollower(DT p) {
if (followers.length() <= numFollowers) {
followers.changeSize(followers.length() * 2);
return;
}
followers[numFollowers++] = p;
}

template <class DT>
void TwitterArray<DT>::RemoveFollower(DT p) {

int index = -1;
for (int i = 0; i < numFollowers; i++) {
if (followers[i] == p) {
index = i;
}
}
if(index != -1)
{
for (int i = index; i < numFollowers - 1; i++) {
followers[i] = followers[i + 1];
}
numFollowers --;
}
}

template <class DT>
void TwitterArray<DT>::PrintFollowers() {
for (int i = 0; i < numFollowers; i++) {
cout << followers[i] << ", ";
}
cout << endl;
}

#endif // TWITTERARRAYHEADER_H_INCLUDED


output
constructor
P2,
P2, P3,
P2, P3,
P2,
constructor
UP2,
UP2, UP3,
UP2, UP3,
UP2,