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

I am finishing up a project for class and I\'m stuck on the last bit. I have the

ID: 3768012 • Letter: I

Question

I am finishing up a project for class and I'm stuck on the last bit. I have the function implemented to work via bubble sort, the function below WORKS. However, the teacher wants us to sort recursively and I'm trying to figure out where I'm going wrong turning my bubble sort into a recursive function.

BUBBLE SORT THAT WORKS BUT NOT RECURSIVE:

void sortInventory(vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
    vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
    vector<double>& sellingPrice, int size)
{
    string name, id;
    int pieces, instock, sold;
    double mprice, sprice;
    int iteration;
    int index;
    
    for (iteration = 1; iteration < size; iteration++)
    {
        for (index = 0; index < size - iteration; index++)
            if (itemName[index] > itemName[index +1])
        {

            name = itemName[index];
            itemName[index] = itemName[index + 1];
            itemName[index + 1] = name;

            id = itemID[index];
            itemID[index] = itemID[index + 1];
            itemID[index + 1] = id;

            pieces = pOrdered[index];
            pOrdered[index] = pOrdered[index + 1];
            pOrdered[index + 1] = pieces;

            instock = pInStore[index];
            pInStore[index] = pInStore[index + 1];
            pInStore[index + 1] = instock;

            sold = pSold[index];
            pSold[index] = pSold[index + 1];
            pSold[index + 1] = sold;

            mprice = manufPrice[index];
            manufPrice[index] = manufPrice[index + 1];
            manufPrice[index + 1] = mprice;

            sprice = sellingPrice[index];
            sellingPrice[index] = sellingPrice[index + 1];
            sellingPrice[index + 1] = sprice;
        }
    }
}

MY ATTEMPT AT TURNING THAT INTO A RECURSIVE FUNCTION:

void sortInventory(vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
   vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
   vector<double>& sellingPrice, int size)
{
   string name, id;
   int pieces, instock, sold;
   double mprice, sprice;
   int iteration;
   int index;
   if (size == 0)
       return;//exit sort
   else
   {
       for (index = 0; index < size; index++)
           if (itemName[index] > itemName[index + 1])
           {

               name = itemName[index];
               itemName[index] = itemName[index + 1];
               itemName[index + 1] = name;

               id = itemID[index];
               itemID[index] = itemID[index + 1];
               itemID[index + 1] = id;

               pieces = pOrdered[index];
               pOrdered[index] = pOrdered[index + 1];
               pOrdered[index + 1] = pieces;

               instock = pInStore[index];
               pInStore[index] = pInStore[index + 1];
               pInStore[index + 1] = instock;

               sold = pSold[index];
               pSold[index] = pSold[index + 1];
               pSold[index + 1] = sold;

               mprice = manufPrice[index];
               manufPrice[index] = manufPrice[index + 1];
               manufPrice[index + 1] = mprice;

               sprice = sellingPrice[index];
               sellingPrice[index] = sellingPrice[index + 1];
               sellingPrice[index + 1] = sprice;
           }
   }
   sortInventory(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice, size - 1);
}

I don't understand what is wrong with my function. I keep getting this error:

Program: C:WINDOWSSYSTEM32MSVCP140D.dll
File: c:program files (x86)microsoft visual studio 14.0 cinclude ector
Line: 1232

Expression: vector subscript out of range

I must have some logical error in the loop but I can't figure it out.

Explanation / Answer

void sortInventory(vector<string>& itemID, vector<string>& itemName, vector<int>& pOrdered,
vector<int>& pInStore, vector<int>& pSold, vector<double>& manufPrice,
vector<double>& sellingPrice, int size)
{
string name, id;
int pieces, instock, sold;
double mprice, sprice;
int iteration;
int index;
if (size == 0)
return;//exit sort
else
{
for (index = 0; index < size-1; index++)
if (itemName[index] > itemName[index + 1])
{
name = itemName[index];
itemName[index] = itemName[index + 1];
itemName[index + 1] = name;

id = itemID[index];
itemID[index] = itemID[index + 1];
itemID[index + 1] = id;

pieces = pOrdered[index];
pOrdered[index] = pOrdered[index + 1];
pOrdered[index + 1] = pieces;

instock = pInStore[index];
pInStore[index] = pInStore[index + 1];
pInStore[index + 1] = instock;

sold = pSold[index];
pSold[index] = pSold[index + 1];
pSold[index + 1] = sold;

mprice = manufPrice[index];
manufPrice[index] = manufPrice[index + 1];
manufPrice[index + 1] = mprice;

sprice = sellingPrice[index];
sellingPrice[index] = sellingPrice[index + 1];
sellingPrice[index + 1] = sprice;
}
}
sortInventory(itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellingPrice, size - 1);
}

Reason for error

when you are using

for (index = 0; index < size; index++)

for the last iteration when index=size-1

inside if condition you are trying to access element at index+1

itemName[index] = itemName[index + 1];

that means it is trying to get the element at index = size, where as the vector is from 0 to size-1

hence you are getting vector subscript out of range error