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

explain each line and what it does #include <iostream> #include <string> using n

ID: 3746559 • Letter: E

Question

explain each line and what it does

#include <iostream>
#include <string>

using namespace std;

int counter = 0;

//PRINT ELEMENTS FUNCTION

void printElements(int arr[])
{
for (int i = 0; i < counter; i++)
{
  if (i != (counter - 1))
  {
   cout << arr[i] << ", ";
  }
  else
  {
   cout << arr[i];
  }
}
cout << endl;
}

//EXPAND SIZE FUNCTION

void expandArray(int* &arr, int &size)
{
int *tempPtr = nullptr;

if (counter == size)
{
  tempPtr = new int[size * 2];

  for (int i = 0; i < counter; i++)
  {
   tempPtr[i] = arr[i];
  }

  delete[] arr;
  arr = nullptr;

  arr = tempPtr;
  size = size * 2;

  tempPtr = nullptr;


}
}

//ADD ELEMENTS FUNCTION
void addElement(int* &arr, int &size)
{
int userAddElement;
int temp;
bool Added = false;
bool exp = false;
if (counter == size)
{
  expandArray(arr, size);
  exp = true;
}

for (int i = 0; i < counter - 1; i++)
{
  for (int j = 0; j < counter - i - 1; j++)
  {
   if (arr[j] > arr[j + 1])
   {
    temp = arr[j];
    arr[j] = arr[j + 1];
    arr[j + 1] = temp;
   }
  }
}

cout << "Add element: ";
cin >> userAddElement;
if (exp)
{
  cout << "Array Expanded" << endl;
}
cout << endl;

if (counter == 0)
{
  arr[0] = userAddElement;
  counter++;
}
else
{
  for (int i = 0; i < counter; i++)
  {
   if (userAddElement < arr[i])
   {
    for (int j = counter - 1; j >= i; j--)
    {
     arr[j + 1] = arr[j];
    }

    arr[i] = userAddElement;
    Added = true;
    counter++;

    break;
   }

  }

  if (Added == false)
  {
   arr[counter] = userAddElement;
   counter++;
  }
}


}

//SHRINK SIZE FUNCTION

void shrinkArray(int* &arr, int &size)
{
bool shrunk = false;

int *tempPtr = nullptr;

while (counter < (size / 2))
{
  tempPtr = new int[size / 2];

  for (int i = 0; i < counter; i++)
  {
   tempPtr[i] = arr[i];
  }

  delete[] arr;
  arr = nullptr;

  arr = tempPtr;
  size = size / 2;
  shrunk = true;

  tempPtr = nullptr;
}

if (shrunk == true)
{
  cout << "Array size Shrunk to: " << size << endl << endl;
}
}

//DELETE ELEMENTS FUNCTION

void deleteElement(int* &arr, int &size)
{
int userDeleteElement;
bool Found = false;

cout << "Delete element: ";
cin >> userDeleteElement;
cout << endl << endl;

if (counter != 0)
{
  for (int i = 0; i < counter; i++)
  {
   if (arr[i] == userDeleteElement)
   {
    Found = true;

    for (int j = i; j < counter; j++)
    {
     arr[j] = arr[j + 1];
    }

    i = -1;
    counter--;
   }
  }

  if (Found == false)
  {
   cout << "Error: " << userDeleteElement << " was not found in the array." << endl << endl;
  }
}
else
{
  cout << "ERROR: There are no elements in the array to delete. " << endl << endl;
}

shrinkArray(arr, size);
}

//RETURN SIZE FUNCTION

void returnSize(int size)
{
cout << "S: " << size << ", E: " << counter << endl;

}

//THE MAIN FUNCTION

int main()
{
int size = 2;
int *mainArray = new int[size];
string userInput;
string trash;
do
{
  cout << "p) Print Elements" << endl;
  cout << "a) Add Element" << endl;
  cout << "d) Delete Element" << endl;
  cout << "r) Return Size" << endl;
  cout << "e) Exit" << endl << endl;

  cout << " Enter Option: ";
  getline(cin, userInput);
  cout << endl << endl;

  if (userInput == "p")
  {
   printElements(mainArray);
  }
  else if (userInput == "a")
  {
   addElement(mainArray, size);
  }
  else if (userInput == "d")
  {
   deleteElement(mainArray, size);
  }
  else if (userInput == "r")
  {
   returnSize(size);
  }
  else if (userInput == "e")
  {
   break;
  }
  else
  {
   cout << "Your input is not an option on the menu." << endl << endl;
  }
  getline(cin, trash);
} while (userInput != "e");

delete[] mainArray;
mainArray = nullptr;

return 0;
}

Explanation / Answer

please give thumbs up, thanks

#include <iostream>

#include <string>

#define nullptr 0

using namespace std;

//used to know how many elemnets are currently in array/List

int counter = 0;

//PRINT ELEMENTS FUNCTION

void printElements(int arr[])

{

//from 0 to number of elements-1 display arrays content

for (int i = 0; i < counter; i++)

{

//if current index contains element, then add , so all data will dislayed in formated manner

if (i != (counter - 1))

{

cout << arr[i] << ", ";

}

else

{

cout << arr[i];

}

}

cout << endl;

}

//EXPAND SIZE FUNCTION

void expandArray(int* &arr, int &size)

{

//creating integer point to set to null

int *tempPtr = nullptr;

//if list is full, then expand its size to double

if (counter == size)

{

//then create a new array with multiple of size 2

tempPtr = new int[size * 2];

//copy each element to array to newly created array

for (int i = 0; i < counter; i++)

{

tempPtr[i] = arr[i];

}

//delete old array

delete[] arr;

//make old array to point null i.e nothing

arr = nullptr;

//pass address of newly created array is pointer arr

arr = tempPtr;

//increase size variable, beacuase size is doubled now

size = size * 2;

//as new array is now pointed by arr, so make pointer tempptr to null

tempPtr = nullptr;

}

}

//ADD ELEMENTS FUNCTION

void addElement(int* &arr, int &size)

{

int userAddElement;

int temp;

bool Added = false;

bool exp = false;

//is array is full then exapnd array to double by calling function exapandArray and make flag exp to true

if (counter == size)

{

expandArray(arr, size);

exp = true;

}

//sort content to array

for (int i = 0; i < counter - 1; i++)

{

for (int j = 0; j < counter - i - 1; j++)

{

if (arr[j] > arr[j + 1])

{

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}

}

}

//prompt user and ask for input

cout << "Add element: ";

//take input int inputelement

cin >> userAddElement;

//if array is doubled then display message

if (exp)

{

cout << "Array Expanded" << endl;

}

cout << endl;

//if array has no element then add to first index, and increase counter so indicate that new element is added

if (counter == 0)

{

arr[0] = userAddElement;

counter++;

}

//if list has aready one or more element

else

{

//add element as depend on your condition

for (int i = 0; i < counter; i++)

{

if (userAddElement < arr[i])

{

//if input is less then element pointed by index i move each element to right , i.e right shift and make added flag to true

for (int j = counter - 1; j >= i; j--)

{

arr[j + 1] = arr[j];

}

arr[i] = userAddElement;

Added = true;

counter++;

break;

}

}

//if element is not added then add at last index, and increment counter

if (Added == false)

{

arr[counter] = userAddElement;

counter++;

}

}

}

//SHRINK SIZE FUNCTION

void shrinkArray(int* &arr, int &size)

{

bool shrunk = false;

int *tempPtr = nullptr;

//while current number of elements is less than half of size of array

while (counter < (size / 2))

{

//create new array is half size

tempPtr = new int[size / 2];

copy each element to new array

for (int i = 0; i < counter; i++)

{

tempPtr[i] = arr[i];

}

//delete privious array

delete[] arr;

arr = nullptr;

//ad address of new array to pointer arr which hold the address of actuall array

arr = tempPtr;

size = size / 2;

shrunk = true;

tempPtr = nullptr;

}

//if array is made half show message that memry is shrunkedd

if (shrunk == true)

{

cout << "Array size Shrunk to: " << size << endl << endl;

}

}

//DELETE ELEMENTS FUNCTION

void deleteElement(int* &arr, int &size)

{

int userDeleteElement;

bool Found = false;

//take input from element to delete that from array

cout << "Delete element: ";

cin >> userDeleteElement;

cout << endl << endl;

//if arrat contains element

if (counter != 0)

{

//for each element

for (int i = 0; i < counter; i++)

{

//if ith position contains element that we need to delete

if (arr[i] == userDeleteElement)

{

//then set found flag to true

Found = true;

//move all right element from i index to left shift

for (int j = i; j < counter; j++)

{

arr[j] = arr[j + 1];

}

i = -1;

counter--;

}

}

//if element not found then show error

if (Found == false)

{

cout << "Error: " << userDeleteElement << " was not found in the array." << endl << endl;

}

}

//if not contains an element then display error

else

{

cout << "ERROR: There are no elements in the array to delete. " << endl << endl;

}

shrinkArray(arr, size);

}

//RETURN SIZE FUNCTION

void returnSize(int size)

{

cout << "S: " << size << ", E: " << counter << endl;

}

//THE MAIN FUNCTION

int main()

{

//creating a variable to store array size

int size = 2;

//creating a integer pointer which hold address to an integer array of size "size"

int *mainArray = new int[size];

//variable to store user input

string userInput;

string trash;

do

{

//printing menu

cout << "p) Print Elements" << endl;

cout << "a) Add Element" << endl;

cout << "d) Delete Element" << endl;

cout << "r) Return Size" << endl;

cout << "e) Exit" << endl << endl;

cout << " Enter Option: ";

//taking input in uerinput using function getline

getline(cin, userInput);

cout << endl << endl;

//if user enters p then call printElement function to display elements in array

if (userInput == "p")

{

printElements(mainArray);

}

//if user enters s then call addelement function which will add take data from use and insert to array

else if (userInput == "a")

{

addElement(mainArray, size);

}

//if user enter d then delete

else if (userInput == "d")

{

deleteElement(mainArray, size);

}

//if user input r then display array's size and how many elements it contains

else if (userInput == "r")

{

returnSize(size);

}

//if user enter e then exit while loop

else if (userInput == "e")

{

break;

}

//if user enter anything else, then show error

else

{

cout << "Your input is not an option on the menu." << endl << endl;

}

getline(cin, trash);

} while (userInput != "e"); //continue while loop while user not enters e

//then deleter array, to memory can be free

delete[] mainArray;

//make pointer to null

mainArray = nullptr;

return 0;

}