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

C++ Array Insertion question. I have a tester header and one main.cpp file. I ca

ID: 3787673 • Letter: C

Question

C++ Array Insertion question. I have a tester header and one main.cpp file. I can't seem to find the problem with this insertion. Can someone help please. The tester should automatically run, but I think it's inserting incorrectly

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

#include <iostream>
using namespace std;

const int CAPACITY = 10;

// Declaration function insertAtIndex
int* insertAtIndex(int* origArr, int numElem, int elemToInsert, int index);

#include "Testing.hxx"

int main( )
{
   testCases();

   cout << endl;
   system("Pause");
return 0;
}

// Definition function insertAtIndex

int* insertAtIndex(int* origArr, int numOfElem, int elemToInsert, int index) {

   int arrSize;
   arrSize = sizeof(origArr)/ sizeof(origArr[0]);

   if (index > arrSize) {
       cerr << "Array is full. Cannot insert another element." << endl;
   }
  

   int* newArr = new int[arrSize + 1];

   for (int i = 0; i <= arrSize; i++) {
       if (i < index) {
           newArr[i] = origArr[i];
       }
       if (i == index) {
           newArr[i] = elemToInsert;
       }
       if (i > index) {
           newArr[i] = origArr[i - 1];
       }
   }

   return newArr;
}

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

#ifndef TESTING_H
#define TESTING_H

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

vector< vector<int>> v = { { },
{ 1 },
{ 3 },
{ 5, 3 },
{ 7, 4 },
{ 5, 3, 1, 7 },
{ 4, 2, 7, 4 },
{ 8, 4, 2, 6, 7, 8, 2 },
{ 9, 8, 5, 6, 3, 2, 1, 4 },
{ 1, 6, 4, 8, 9, 0, 7, 5, 2, 3 },
{ 4, 6, 2},
{ 0, 1, 2, 3, 4, 5, 6 ,7 ,8, 9 } };

int indices[] = { 0, 0, 1, 1, 2, 4, 5, 7, 8, 10, 20, 5 };

void printArray(const int a[], int numOfElements)
{
   if (numOfElements == 0)
       cout << "No elements in the array.";
   else
       for (int i = 0; i < numOfElements; ++i)
           cout << a[i] << " ";
}

void testing(int i)
{
   int a[CAPACITY];
   int numOfElem = static_cast<int>(v[i].size());
   for (int j = 0; j < numOfElem; ++j)
       a[j] = v[i].at(j);
   cout << "Initial Array: ";
   printArray(a, numOfElem);
   cout << endl;
   int elem = (i + 1) * 10;
   cout << "Insert " << elem << " at idx " << indices[i] << "... Modified array: ";
   insertAtIndex(a, numOfElem, elem, indices[i]);  
   printArray(a, numOfElem);
   cout << " -------------------------------------------------------- ";
}
void testCases()
{
   int vectorSize = static_cast<int>(v.size());

   for (int i = 0; i < vectorSize; ++i)
   {
       testing(i);
   }
}

#endif

Explanation / Answer

check over these statements::

int* insertAtIndex(int* origArr, int numOfElem, int elemToInsert, int index) {

and

#ifndef TESTING_H
#define TESTING_H

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

vector< vector<int>> v = { { },
{ 1 },
{ 3 } .....

and

insertAtIndex(a, numOfElem, elem, indices[i]);