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

C++ Question: Using Structs of Arrays I am trying to write a code to count the s

ID: 3850760 • Letter: C

Question

C++ Question: Using Structs of Arrays

I am trying to write a code to count the size of an array used from an input files using structs.Tricky thing is the initial size of the array is 5 , The array should double in size after it is full and the next addition comes in. So at 5 it is full but doesn't grow. If a 6 element is attempted to be added, then it grows.

It will also show actual data used for. Not sure how this logic works. Can someone explain with a code so I can run and check? Thanks for your time!

inputfile:

outputfile:

Explanation / Answer

//By using pointer to struct type we can change the size of array of struct ,

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
   //declare array of struct
   struct data
   {
       int num;
   };
   //declare object of type ifstream for reading input file
   ifstream in;
   //open input file
   in.open("inputfile.txt");
   //check if input file is open
   if (!in)
   {
       cout << "Not able to open input file " << endl;
       return -1;
   }
   //count number of elements
   int count = 0,num,size;
   struct data *Data;
   //initialize array of struct to size 5 in the beginning
   Data = new struct data[5];
   //initial array size is 5
   size = 5;
   while (!in.eof())
   {
       in >> num;
       if (count >= 5 && !(count % 5))
       {
           struct data *tmp;
           //allocate tmp memory to copy elelements of original array
           tmp = new struct data[count];
           for (int i = 0; i < count; i++)
           {
               tmp[i].num = Data[i].num;
           }
           //allocate new memory doble its initial size to hold more than its initial size
           Data = new struct data[count * 2];
           //now copy from tmp to Data
           for (int i = 0; i < count; i++)
           {
               Data[i] = tmp[i];
           }
           size = count * 2;
       }
       Data[count].num = num;
       count++;
       //checck array elements more than 5
      
   }
   //display the result
   //open outputfile
   ofstream out;
   out.open("outputfile.txt");
   if (!out)
   {
       cout<< "Unable to open outputfile" << endl;
       return -1;
   }
   out << "Size: " << size << endl;
   out << "Count: " << count << endl;
   out << "Data: " << endl;
   for (int i = 0; i < count; i++)
   {
       out<< Data[i].num << endl;
   }
}

-----------------------------------------------------------------------------------------------------------------------------------------------------

change inputfile.txt file to check various output given, and check outputfile for the outpt results

//output1

outputfile.txt has following data

Size: 5
Count: 5
Data:
6
3
5
4
7

output2

Size: 10
Count: 6
Data:
6
3
5
4
7
10

output3

Size: 20
Count: 11
Data:
6
3
5
4
7
10
23
11
9
40
12

output4

Size: 40
Count: 22
Data:
6
3
5
4
7
10
23
11
9
40
12
6
3
5
4
7
10
23
11
9
40
12

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote