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

Please Help!!! Write a code that will: resize an array insert: this will be foll

ID: 3624919 • Letter: P

Question

Please Help!!!

Write a code that will: resize an array

insert: this will be followed by an int. We will simply insert this int into the end of the array. When the array fills up, we will use a doubling to grow our array.

size: this will not be followed by anything and it will print what the current size of the array is. This is one of the two commands that produce output. It will simply put Size: on a line, where is the current size of the list.

Input.txt:
insert 7
insert 10
insert 1
insert 3
insert 3
insert 4
insert 8
insert 5
insert 10
insert 3
size
insert 7
size
print
remove

Output.txt:
Size: 10
Size: 20
Elements
--------
7
10
1
3
3
4
8
5
10
3
7

Explanation / Answer

//header section

#include < iostream >

#include < malloc.h >

#include < fstream >

#include < string >

using namespace std;

//main function

int main()

{

       //declaration

       int *numbers=new int[10];

       int arraysize=10;

       string command;

       int index=0,num;

       //files opening

       fstream infile("input.txt",ios::in);

       fstream outfile("output.txt",ios::out);

       if(infile.fail())

              cout<<"file not found";

       else

       {

       while(!infile.eof())

       {

              infile>>command;

              if(command=="insert")

              {

                     if(index==arraysize)

                     {

                           arraysize *=2;

                           //resize an array

                           numbers=(int*)realloc(numbers,arraysize);

                          

                     }

                     infile>>num;

                     //placing number into an array

                     numbers[index]=num;

                     cout<<numbers[index];

                     index++;

              }

              else if(command=="size")

                     outfile<<"Size: "<<index<<endl;//place size in file

              //remove last element from array

              else if(command.compare("remove"))

              {

                     index--;

                     if(index<(arraysize/2))

                     {

                           arraysize /=2;

                           //shrink the array

                           numbers=(int*)realloc(numbers,arraysize);

                     }

              }

              else if(command=="print")

              {

                     //print data to file

                     outfile<<"Elements"<<endl;

                     outfile<<"--------"<<endl;

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

                     {

                           outfile<<numbers[i]<<endl;

                     }

              }

       }

       }

       system("pause");

}//end of main