Note: I posted this question earlier and recieved an incorrect solution. Please
ID: 3674949 • Letter: N
Question
Note: I posted this question earlier and recieved an incorrect solution. Please read the following paragraph before replying with an answer.
I finished the following assignment but my professor wants me to change some of the logic of it to match what she wants. My code is below the instructions to the assignment. She says my code isn't right because the body of my sort loop doesn't execute, and it if did, it still can't work because a sort requires two nested loops. Finally, totalItems ends up one too high because its incremented before the end of file is detected. To reiterate, all I need is:
* To know how to redo A. My sort loop so it uses 2 nested loops and executes, and B. How to prevent the totalItems int from ending up one too high.
I'm not asking for a whole new program, just corrections.
Write a C++ program that reads sales data from a file called sales.txt. Each line of the file contains four data items; 1. A product number which is an integer 2. A product name which is a string no longer than 12 characters and which does not contain spaces 3. A unit price which is a double 4. A number of units sold which is an integer The program will output the two products which generated the highest total revenue (unit price * number sold). Your program must: 1. Read each line of the file storing the four items into the members of a dynamically allocated structure. 2. Store pointers to the structures in an array. 3. Sort the array of pointers by total revenue 4. Display the product name and total revenue of the top two products 5. The program must work for sales.txt files with anywhere from 2 to 100 products
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
//Create structure for items
struct Item {
int productNumber;
string productName;
double unitPrice;
int unitsSold;
};
int main() {
struct Item itemArray[100];
int i=0;
int j=0;
int totalItems=0;
double rev1=0;
double rev2=0;
ifstream infile;
infile.open("sales.txt");
//Read data from file
while (!infile.eof()) {
infile >> itemArray[i].productNumber;
infile >> itemArray[i].productName;
infile >> itemArray[i].unitPrice;
infile >> itemArray[i].unitsSold;
i++;
}
totalItems = i;
//Sort items by revenue. "i" is used as the total items, while "j" is used for the second top item
for (j = i + 1; j < totalItems; j++) {
if (itemArray[i].unitPrice * itemArray[i].unitsSold > itemArray[j].unitPrice
* itemArray[j].unitsSold) {
int tempNumber = itemArray[i].productNumber;
itemArray[i].productNumber = itemArray[j].productNumber;
itemArray[j].productNumber = tempNumber;
string tempName = itemArray[i].productName;
itemArray[i].productName = itemArray[j].productName;
itemArray[j].productName = tempName;
double tempPrice = itemArray[i].unitPrice;
itemArray[i].unitPrice = itemArray[j].unitPrice;
itemArray[j].unitPrice = tempPrice;
int tempUnits = itemArray[i].unitsSold;
itemArray[i].unitsSold = itemArray[j].unitsSold;
itemArray[j].unitsSold = tempUnits;
}
}
//Show top two items by revenue
rev1 = itemArray[totalItems - 1].unitPrice*itemArray[totalItems - 1].unitsSold;
rev2 = itemArray[totalItems - 2].unitPrice*itemArray[totalItems - 2].unitsSold;
cout << "The top selling product is: " << itemArray[totalItems - 1].productName << " with total sales of $" << rev1 << endl;
cout << "The second top selling product is: " << itemArray[totalItems - 2].productName << " with total sales of $" << rev2 << endl;
return 0;
}
Explanation / Answer
I have implemented Bubble Sort. Test and give feedback.
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
//Create structure for items
struct Item {
int productNumber;
string productName;
double unitPrice;
int unitsSold;
};
int main() {
struct Item itemArray[100];
int i=0;
int j=0;
int totalItems=0;
double rev1=0;
double rev2=0;
ifstream infile;
infile.open("sales.txt");
//Read data from file
while (!infile.eof()) {
infile >> itemArray[i].productNumber;
infile >> itemArray[i].productName;
infile >> itemArray[i].unitPrice;
infile >> itemArray[i].unitsSold;
i++;
}
totalItems = i;
//Sort items by revenue: Bubble Sort
int min_pos;
for(int i=0; i< totalItems-1; i++){
min_pos = i;
for (j = 0; j < totalItems-i-1; j++) {
if (itemArray[j].unitPrice * itemArray[j].unitsSold > itemArray[j+1].unitPrice
* itemArray[j+1].unitsSold) {
int tempNumber = itemArray[j].productNumber;
itemArray[j].productNumber = itemArray[j+1].productNumber;
itemArray[j+1].productNumber = tempNumber;
string tempName = itemArray[j].productName;
itemArray[j].productName = itemArray[j+1].productName;
itemArray[j+1].productName = tempName;
double tempPrice = itemArray[j].unitPrice;
itemArray[j].unitPrice = itemArray[j+1].unitPrice;
itemArray[j+1
int tempUnits = itemArray[j].unitsSold;
itemArray[j].unitsSold = itemArray[j+1].unitsSold;
itemArray[j+1].unitsSold = tempUnits;
}
}
}
//Show top two items by revenue
rev1 = itemArray[totalItems - 1].unitPrice*itemArray[totalItems - 1].unitsSold;
rev2 = itemArray[totalItems - 2].unitPrice*itemArray[totalItems - 2].unitsSold;
cout << "The top selling product is: " << itemArray[totalItems - 1].productName << " with total sales of $" << rev1 << endl;
cout << "The second top selling product is: " << itemArray[totalItems - 2].productName << " with total sales of $" << rev2 << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.