Language:c++ choice b and c dont display the file rearranged why is that and how can i...

90.2K

Verified Solution

Question

Programming

Language:c++

choice b and c dont display the file rearranged why is that andhow can i fix it?

#include
#include
using namespace std;
#define size 10000

// Displays the current Inventory Data
void Display(int box_nums[], int nboxes[], double ppBox[], int n)// Prints Box number , number of boxes and price per box.
{
   cout << \"Box number Number of boxes in stockPrice per box\" << \"\n\";
   cout <<\"-------------------------------------------------------------\n\";
   for (int i = 0; i < n; i++)
   {
       cout << box_nums[i] <<\" \" << nboxes[i] << \" \" << ppBox[i] <<\"\n\";
   }
}

// sort's the inventory data according to the price per box fromlow to high
void sortByPrice(int box_nums[], int nboxes[], double priceBox[],int n)
{
   int i, j, min_idx, temp2;
   double temp;
   for (i = 0; i < n - 1; i++)
   {
       min_idx = i; // min_idx is used tostore data in ascending order
       for (j = i + 1; j < n; j++) //used selection sort to sort the data based on price per box
       {
           if (priceBox[j]< priceBox[min_idx])
              min_idx = j;
       }
       temp = priceBox[min_idx];
       priceBox[min_idx] = priceBox[i]; //temp is a variable to swap data
       priceBox[i] = temp;

       temp2 = nboxes[min_idx];
       nboxes[min_idx] = nboxes[i]; //temp2 is a variable to swap data
       nboxes[i] = temp2;

       temp2 = box_nums[min_idx];
       box_nums[min_idx] =box_nums[i];
       box_nums[i] = temp2;

   }
}

// sort's the inventory data according to Box number from low tohigh
void sortByBoxNumber(int box_nums[], int nboxes[], doublepriceBox[], int n)
{
   int i, j, min_idx, temp2;
   double temp;
   for (i = 0; i < n - 1; i++)
   {
       min_idx = i; // min_idx is used tostore data in ascending order
       for (j = i + 1; j < n;j++)
       {
           if (box_nums[j]< box_nums[min_idx]) // used selection sort to sort the databased on price per box
              min_idx = j;
       }

       temp2 = box_nums[min_idx];
       box_nums[min_idx] =box_nums[i];
       box_nums[i] = temp2;

       temp = priceBox[min_idx];
       priceBox[min_idx] =priceBox[i];
       priceBox[i] = temp;

       temp2 = nboxes[min_idx];
       nboxes[min_idx] = nboxes[i];
       nboxes[i] = temp2;
   }
}

// Searches for the price per box of the corresponding boxnumber entered by user.
double lookUpByBoxNumber(int boxNumber, int box_nums[], doublepriceBox[], int n)
{
   int low = 0, high = n - 1;
   int mid;
   while (low <= high) // used binary search to searchfor the corresponding box number and its price-per-box
   {
       mid = low + (high - low) / 2;

       if (box_nums[mid] >boxNumber)
       {
           high = mid -1;
       }
       else if (box_nums[mid] ==boxNumber)
       {
           returnpriceBox[mid];
       }
       else
       {
           low = mid +1;
       }

   }
   return -1;
}

//Reordered the data whose number of boxes are less than100
void reorderReport(int box_nums[], int nboxes[], int n)
{
   int* reorderBoxNums = new int[n];
   int* reorderBoxes = new int[n];
   int k = 0;
   for (int i = 0; i < n; i++)
   {
       if (nboxes[i] < 100)
       {
          reorderBoxNums[k] = box_nums[i];
           reorderBoxes[k]= nboxes[i];
           k++;
       }
   }
   int i, j, max_idx, temp2;
   for (i = 0; i < k - 1; i++) // sorts the data inreordered data according to number of boxes in inventory from lowto high
   {
       max_idx = i;
       for (j = i + 1; j < k;j++)
       {
           if(reorderBoxes[j] > reorderBoxes[max_idx])
              max_idx = j;
       }

       temp2 =reorderBoxes[max_idx];
       reorderBoxes[max_idx] =reorderBoxes[i];
       reorderBoxes[i] = temp2;

       temp2 =reorderBoxNums[max_idx];
       reorderBoxNums[max_idx] =reorderBoxNums[i];
       reorderBoxNums[i] = temp2;
   }
   cout << \"REORDERED REPORT IS: \n\";
   cout << \"The boxes in invetory whose stock areless than 100 are: \n\";
   cout << \"Box number Number of boxes in stock\"<< \"\n\";
   cout <<\"------------------------------------------\" << \"\n\";
   for (int i = 0; i < k; i++)
   {
       cout << reorderBoxNums[i]<< \" \" << reorderBoxes[i] << \"\n\";
   }
}
int main()
{

   std::fstream myfile(\"inventory.txt\");
   int box_number[size], numberOfBoxes[size];
   double pricePerBox[size], sp;
   int bn, nb, i = 0;
   while (myfile >> bn >> nb >> sp) //fetch data from file inventory.txt
   {
       box_number[i] = bn;
       numberOfBoxes[i] = nb;
       pricePerBox[i] = sp;
       i++;
   }

   int n = i, bnumber; // n stores number of recordsin file , bnumber is the box number which is to be searched forprice-per-box by user
   double val; // val is variable used for value storedfrom lookup by box-number
   char option;
   bool exit = true; // exit variable to exit the whileloop

   // Menu for the user
   cout << \"\nChoose a option in the Menua/b/c/d/e/f :\" << \"\n\";
   cout << \"a. Display the data\" <<\"\n\";
   cout << \"b. Sort data by price, low to high\"<< \"\n\";
   cout << \"c. Sort data by box number, low tohigh\" << \"\n\";
   cout << \"d. Look up the Price of the box giventhe box number\" << \"\n\";
   cout << \"e. Generate Reorder Report\" <<\"\n\";
   cout << \"f. Exit\" << \"\n\";

   while (exit)
   {
       cout << \"Enter your choicea/b/c/d/e/f : \";
       cin >> option;

       switch (option)
       {
       case 'a':
          Display(box_number, numberOfBoxes, pricePerBox, n);
           break;
       case 'b':
          sortByPrice(box_number, numberOfBoxes, pricePerBox, n);
           cout <<\"Data has been Successfully sorted by price\" << \"\n\";
           cout <<\"Please, choose option 'a' to display sorted data\" <<\"\n\";
           break;
       case 'c':
          sortByBoxNumber(box_number, numberOfBoxes, pricePerBox, n);
           cout <<\"Data has been Successfully sorted by Box Number\" <<\"\n\";
           cout <<\"Please, choose option 'a' to display sorted data\" <<\"\n\";
           break;
       case 'd':
          sortByBoxNumber(box_number, numberOfBoxes, pricePerBox, n);
           cout <<\"Enter the box number for which you want to search the price :\";
           cin >>bnumber;
           val =lookUpByBoxNumber(bnumber, box_number, pricePerBox, n);
           if (val <0)
           {
              cout << \"There is no price of the box forthe box number you are searching for\n\";
           }
           else
           {
              cout << \"The price-per-box of theBox-Number you searched is \" << val << \"\n\";
           }
           break;
       case 'e':
          reorderReport(box_number, numberOfBoxes, n);
           break;
       case 'f':
           exit =false;
           break;
       default:
           cout <<\"Invalid options , enter a valid option\" << \"\n\";
           break;

       }

   }
   return 0;
}

Answer & Explanation Solved by verified expert
4.0 Ratings (826 Votes)
The solution has been written in C Changes 1 changed the argument    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students