using c++ The file organization, the structure ADT, and the function arguments w
ID: 3595220 • Letter: U
Question
using c++
The file organization, the structure ADT, and the function arguments with structure. Reference: Sections 8.3, 11.5
A. Create a text file of 15+ products including product ID’s, unitprices, quantities sold, and sales of each product in your computer. Then input the file into an array of structure ADT to your program.
B. Sort the above data and adding the sales of each product. You may use either bubble sort or selection sort in ascending or descending order on sales. The arguments of call function must include arrays of structure
C. In the program algorithm, you must use “->” structure pointer operator.
D. Print out product ID’s, unit prices, quantities sold, and the sales of each product into a second text file. And print out the total units sold and the total sales also into the second text file. .
E. The file path in the desktop can be located by file property: “C:\Users\yourUsersName\Desktop\filename.txt”
Explanation / Answer
#include <iostream>
using namespace std;
#include<bits/stdc++.h>
#include <vector>
#include <fstream>
//define the structure here
struct Product
{
int productID;
int unitprices;
int quantities_sold;
int sales_of_each_product;
};
// comparator function to compare by sales_of_each_product
bool compareBySalesOfEachProduct(Product &a, Product &b)
{
return a.sales_of_each_product < b.sales_of_each_product;
}
void print_products(vector<Product> vec)
{
// print sorted products in a output file
string filename= "/Users/nilmadhab/Desktop/Chegg/output.txt";
ofstream out(filename);
for (int i = 0; i < vec.size(); ++i)
{
Product * product = &vec[i];
out << product->productID << " " << product->unitprices << " "
<< product->quantities_sold << " " << product->sales_of_each_product << endl;
;
}
out.close();
}
int main()
{
/* code */
string filename= "/Users/nilmadhab/Desktop/Chegg/input.txt";
std::ifstream infile(filename);
int productID, unitprices, quantities_sold, sales_of_each_product;
vector<Product> list;
// read products from input file
while (infile >> productID >> unitprices >> quantities_sold >> sales_of_each_product)
{
Product product;
product.productID = productID;
product.unitprices = unitprices;
product.quantities_sold = quantities_sold;
product.sales_of_each_product = unitprices*quantities_sold;
list.push_back(product);
}
// sort the vector by using comparator function
sort(list.begin(), list.end(), compareBySalesOfEachProduct);
print_products(list);
infile.close();
return 0;
}
INPUT file:
100 101 216 21816
24 103 835 86005
34 104 446 46384
44 102 278 28356
45 1044 799 834156
1543 10 992 9920
12 105 594 62370
123 104 266 27664
144 1033 991 1023703
1443 140 142 19880
147 410 321 131610
123 510 860 438600
156 510 157 80070
164 610 510 311100
143 616 331 203896
122 904 111 100344
OUTPUT FILE:-
1543 10 992 9920
1443 140 142 19880
100 101 216 21816
123 104 266 27664
44 102 278 28356
34 104 446 46384
12 105 594 62370
156 510 157 80070
24 103 835 86005
122 904 111 100344
147 410 321 131610
143 616 331 203896
164 610 510 311100
123 510 860 438600
45 1044 799 834156
144 1033 991 1023703
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.