Question

In: Computer Science

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

Language:c++

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

#include<iostream>
#include <fstream>
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 stock Price 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 from low 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 to store 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 to high
void sortByBoxNumber(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 to store 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 data based 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 box number entered by user.
double lookUpByBoxNumber(int boxNumber, int box_nums[], double priceBox[], int n)
{
   int low = 0, high = n - 1;
   int mid;
   while (low <= high) // used binary search to search for 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)
       {
           return priceBox[mid];
       }
       else
       {
           low = mid + 1;
       }

   }
   return -1;
}

//Reordered the data whose number of boxes are less than 100
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 in reordered data according to number of boxes in inventory from low to 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 are less 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 records in file , bnumber is the box number which is to be searched for price-per-box by user
   double val; // val is variable used for value stored from lookup by box-number
   char option;
   bool exit = true; // exit variable to exit the while loop

   // Menu for the user
   cout << "\nChoose a option in the Menu a/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 to high" << "\n";
   cout << "d. Look up the Price of the box given the box number" << "\n";
   cout << "e. Generate Reorder Report" << "\n";
   cout << "f. Exit" << "\n";

   while (exit)
   {
       cout << "Enter your choice a/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 for the box number you are searching for\n";
           }
           else
           {
               cout << "The price-per-box of the Box-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;
}

Solutions

Expert Solution

The solution has been written in C++;

Changes:

1. changed the argument type of the function "sortByPrice" and "sortByBoxNumber" from array to pointer.

#include<iostream>
#include <fstream>
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 stock Price 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 from low 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 to store 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 to high
void sortByBoxNumber(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 to store 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 data based 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 box number entered by user.
double lookUpByBoxNumber(int boxNumber, int box_nums[], double priceBox[], int n)
{
   int low = 0, high = n - 1;
   int mid;
   while (low <= high) // used binary search to search for 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)
       {
           return priceBox[mid];
       }
       else
       {
           low = mid + 1;
       }

   }
   return -1;
}

//Reordered the data whose number of boxes are less than 100
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 in reordered data according to number of boxes in inventory from low to 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 are less 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 records in file , bnumber is the box number which is to be searched for price-per-box by user
   double val; // val is variable used for value stored from lookup by box-number
   char option;
   bool exit = true; // exit variable to exit the while loop

   // Menu for the user
   cout << "\nChoose a option in the Menu a/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 to high" << "\n";
   cout << "d. Look up the Price of the box given the box number" << "\n";
   cout << "e. Generate Reorder Report" << "\n";
   cout << "f. Exit" << "\n";

   while (exit)
   {
       cout << "Enter your choice a/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 for the box number you are searching for\n";
           }
           else
           {
               cout << "The price-per-box of the Box-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;
}

Related Solutions

Need to create a program in C++ that can display/write into a file called marks.txt. I'm...
Need to create a program in C++ that can display/write into a file called marks.txt. I'm not too worried about the functions, but I don't know how to store the different marks into a arrays. Any help would be appreaciated. Here's some details about the assignment. Student marks are kept in a text file as a single column. Each student may have a different number of assessments and therefore scores. The data recorded in the file for each student start...
Write a C++ program that will display the top internet stories from the stories.txt file The...
Write a C++ program that will display the top internet stories from the stories.txt file The program must display the stories that have a score which the statistical "mode". The "mode" of a set of values is the value that occurs most often (with the greatest frequency) The data about the stories is in a file that contains the following: storyTitle    (a sequence of numbers and/or letters, may contain spaces in it) storyURL    (a regular URL, like http://www.twitter.com/story1,without spaces) score        (an integer number,...
Hi there Can someone tell me. If someone file a case against you and you dont...
Hi there Can someone tell me. If someone file a case against you and you dont have money to fight against it. Then what person should do in that situation.? If someone file a sexual harassment case?
Alcoholic quizz Can you please select A, B, C OR D in the following multiple choice...
Alcoholic quizz Can you please select A, B, C OR D in the following multiple choice question. Thank you so much . When are alcohol commercials mostly televised? Why Most alcohol commercials are televised during the afternoon, and to females because it is promoted as an escape to a lonely lifestyle    Most alcohol commercials are televised during prime time news hours (4pm-7pm), and to males because men drink the most alcohol after work    Most alcohol commercials are televised...
Assume that a donor offers a choice of contributions (a, b, or c) to a not...
Assume that a donor offers a choice of contributions (a, b, or c) to a not for profit organization. The organization may select only one of the choices. Which choice should the organization make to maximize its funds? Why? Choice (a) the donor will contribute $2,000 on the day after the donation is accepted to an account of the not for profit organization. The donor will similarly contribute $2,000 for each of the next nine anniversary dates of the original...
Which of the following files is a temporary file? a. transaction file b. master file c. reference file d. none of the above
Which of the following files is a temporary file? a. transaction fileb. master filec. reference filed. none of the above
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
In a seven-segment LED display, a square pattern can be created by enabling the a, b,...
In a seven-segment LED display, a square pattern can be created by enabling the a, b, f, and g segments or the c, d, e, and g segments. We want to design a circuit that circulates the square patterns in the four-digit seven-segment LED display. The circuit should have an input, en, which enables the circulation, and an input, cw, which specifies the direction (i.e., clockwise or counterclockwise) of the circulation. Write in verilog please.
how to do circular linked list in c++ (inset and delet and display)
how to do circular linked list in c++ (inset and delet and display)
(This is a multiple choice question, so the answer is either A, B, C, or D.)...
(This is a multiple choice question, so the answer is either A, B, C, or D.) Question: For probability density function of a random variable X, P(X < a) can also be described as: A.) The area under the curve to the right of a. B.) F(a), where F(X) is the cumulative distribution function. C.) The area under the curve between 0 and a. D.) 1- F(a) where F(X) is the cumulative distribution function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT