hey guys, two things (my code is below, and should work,. just need stuff added.
ID: 3811385 • Letter: H
Question
hey guys, two things (my code is below, and should work,. just need stuff added.)
1.) I need help with my code, allowing it to store any friends that are made on facebook to be stored in an Array Object, which can be a dynamic array created by Array class. Anything initilized can be initilized with the constructor, which the size can be set to 2.
2.) I also need the size of the array to change if it's full, or if only a small amount is used. Meaning that the changeSize() which is inside the addFriend() and removeFriend() functions.
I believe if I have a code with changeSize() to double the size of the array if its full, and it should be called to cut the array in half if it's less then 25% of the array.
Here is my code, it can just be altered to fix those two prompts.
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
template <class Type>
class Facebook
{
public:
Facebook();
~Facebook();
void AddFriend(Type item);
void RemoveFriend(Type item);
void PrintFriend();
private:
Type *A;
Type word;
int count;
};
template <class Type>
Facebook<Type>::Facebook()// constructor initilize the default value
{
count = 0;
A = new Type[SIZE];
}
template <class Type>
Facebook<Type>::~Facebook() // desctructor
{
delete[] A;
count = 0;
A = 0;
}
template <class Type>
void Facebook<Type>::AddFriend(Type item) // add element to array and increament the array size
{
if (count<SIZE)
{
A[count++] = item;
}
else
{
cout << "The array is full. ";
}
}
template <class Type>
void Facebook<Type>::RemoveFriend(Type item)
{
int i;
word = item;
for (i = 0; i < count; i++)
{
if (item == A[i]) // find the matching name and remove with element shifting
{
for(int j=i; j<count; j++)
A[j]= A[i+1];
count--; // decreament the array size
}
}
}
template <class Type>
void Facebook<Type>::PrintFriend()
{
int i;
for (i = 0; i<count; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
}
int main()
{
Facebook<string> FriendsList;
FriendsList.AddFriend("Jack");
FriendsList.AddFriend("Mark");
FriendsList.AddFriend("Abcd");
FriendsList.PrintFriend();
cout << endl;
FriendsList.RemoveFriend("Abcd");
FriendsList.PrintFriend();
return 0;
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Thanks for any help, will thumbs up if its good help!
Explanation / Answer
// FB.cpp : Defines the entry point for the console application.
//i have included a global variable s which keeps track of size of array.
#include <iostream>
#include <string>
#include <array>
using namespace std;
const int SIZE = 5;
int s = 5;// keeps track of size of array
template <class Type>
class Facebook
{
public:
Facebook();
~Facebook();
void AddFriend(Type item);
void RemoveFriend(Type item);
void PrintFriend();
void changeSize();
private:
Type *A;
Type word;
Type *B;
int count;
};
template <class Type>
Facebook<Type>::Facebook()// constructor initilize the default value
{
count = 0;
A = new Type[SIZE];
}
template <class Type>
Facebook<Type>::~Facebook() // desctructor
{
delete[] A;
count = 0;
A = 0;
}
template <class Type>
void Facebook<Type>::AddFriend(Type item) // add element to array and increament the array size
{
if (count<s)
{
A[count] = item;
count++;
}
else
{
changeSize();
A[count] = item;
count++;
}
}
template <class Type>
void Facebook<Type>::RemoveFriend(Type item)
{
int i;
word = item;
for (i = 0; i < count; i++)
{
if (item == A[i]) // find the matching name and remove with element shifting
{
for (int j = i; j < count - 1; j++)
{
A[j] = A[j + 1];
}
count--; // decreament the array size
}
}
changeSize();
}
template <class Type>
void Facebook<Type>::PrintFriend()
{
int i;
for (i = 0; i<count; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
}
template <class Type>
void Facebook<Type>::changeSize()
{
if (count < 0.25*s)
{
//cut the array in half
B = new Type[s / 2];
for (int i = 0; i < count; i++)
B[i] = A[i];
delete[] A;
A = B;
s = s / 2;
}
if (count == s)
{
//array is full make a new array
B = new Type[s * 2];
for (int i = 0; i < count; i++)
B[i] = A[i];
delete[] A;
A = B;
s = s * 2;
}
}
int main()
{
Facebook<string> FriendsList;
FriendsList.AddFriend("a");
FriendsList.AddFriend("b");
FriendsList.PrintFriend();
cout << endl;
FriendsList.RemoveFriend("a");
FriendsList.PrintFriend();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.