Goal is to read in a file and sort it using sturct, array , and functions must u
ID: 3551292 • Letter: G
Question
Goal is to read in a file and sort it using sturct, array , and functions
must use getline for initial read in
A.) Add a function to sort the products in the array, by price in ascending order (lowest to highest). If two functions have the exact same price, then those two items can be in any order.**MUST USE BUBBLE SORT***
B.) Add a function to sort the products in the array, by price in ascending order (lowest to highest). If two functions have the exact same price, then those two items can be in any order.***MUST USE SELECTION SORT***
final out put should look like this:
$ ./a.out
Popular tablets:
$ 493.78 4.4 stars: Apple iPad Air MD785LL/A (16GB, Wi-Fi, Black with Space Gray) NEWEST VERSION
$ 610.00 4.4 stars: Apple iPad Air MD789LL/A (32 GB, Wi-Fi, White with Silver)
$ 239.00 4.2 stars: Google Nexus 7 Tablet (7-Inch, 32GB, Black) by ASUS (2013)
$ 382.00 4.1 stars: Google Nexus 10 (Wi-Fi only, 32 GB)
$ 139.00 4.3 stars: Kindle Fire 7" Tablet, Now in HD
$ 379.00 4.3 stars: Kindle Fire HDX 8.9" Tablet
$ 179.00 4.2 stars: Samsung Galaxy Tab 3 (7-Inch, White)
$ 229.00 4.0 stars: Dell Venue 8 Pro 32 GB Tablet (Windows 8.1)
$ 625.00 4.2 stars: Microsoft Surface Pro Tablet (128 GB Hard Drive, 4 GB RAM, Windows 8 Pro)
$ 994.95 4.1 stars: Microsoft - Surface Pro 2 with 128GB - Dark Titanium
Sort by price ('p'), review ('r'), or quit ('q')? p
Sorted by price:
$ 139.00 4.3 stars: Kindle Fire 7" Tablet, Now in HD
$ 179.00 4.2 stars: Samsung Galaxy Tab 3 (7-Inch, White)
$ 229.00 4.0 stars: Dell Venue 8 Pro 32 GB Tablet (Windows 8.1)
$ 239.00 4.2 stars: Google Nexus 7 Tablet (7-Inch, 32GB, Black) by ASUS (2013)
$ 379.00 4.3 stars: Kindle Fire HDX 8.9" Tablet
$ 382.00 4.1 stars: Google Nexus 10 (Wi-Fi only, 32 GB)
$ 493.78 4.4 stars: Apple iPad Air MD785LL/A (16GB, Wi-Fi, Black with Space Gray) NEWEST VERSION
$ 610.00 4.4 stars: Apple iPad Air MD789LL/A (32 GB, Wi-Fi, White with Silver)
$ 625.00 4.2 stars: Microsoft Surface Pro Tablet (128 GB Hard Drive, 4 GB RAM, Windows 8 Pro)
$ 994.95 4.1 stars: Microsoft - Surface Pro 2 with 128GB - Dark Titanium
Sort by price ('p'), review ('r'), or quit ('q')? r
Sorted by review:
$ 493.78 4.4 stars: Apple iPad Air MD785LL/A (16GB, Wi-Fi, Black with Space Gray) NEWEST VERSION
$ 610.00 4.4 stars: Apple iPad Air MD789LL/A (32 GB, Wi-Fi, White with Silver)
$ 139.00 4.3 stars: Kindle Fire 7" Tablet, Now in HD
$ 379.00 4.3 stars: Kindle Fire HDX 8.9" Tablet
$ 179.00 4.2 stars: Samsung Galaxy Tab 3 (7-Inch, White)
$ 239.00 4.2 stars: Google Nexus 7 Tablet (7-Inch, 32GB, Black) by ASUS (2013)
$ 625.00 4.2 stars: Microsoft Surface Pro Tablet (128 GB Hard Drive, 4 GB RAM, Windows 8 Pro)
$ 382.00 4.1 stars: Google Nexus 10 (Wi-Fi only, 32 GB)
$ 994.95 4.1 stars: Microsoft - Surface Pro 2 with 128GB - Dark Titanium
$ 229.00 4.0 stars: Dell Venue 8 Pro 32 GB Tablet (Windows 8.1)
Sort by price ('p'), review ('r'), or quit ('q')? t
Please enter 'p', 'r', or 'q':
q
not sure how to save the info in one array and then how to access just a part of that array here is what i ihave so far
also this is a copy of products.txt that we are manipulating:
10
Apple iPad Air MD785LL/A (16GB, Wi-Fi, Black with Space Gray) NEWEST VERSION
493.78
4.4
Apple iPad Air MD789LL/A (32 GB, Wi-Fi, White with Silver)
610.00
4.4
Google Nexus 7 Tablet (7-Inch, 32GB, Black) by ASUS (2013)
239.00
4.2
Google Nexus 10 (Wi-Fi only, 32 GB)
382.00
4.1
Kindle Fire 7" Tablet, Now in HD
139.00
4.3
Kindle Fire HDX 8.9" Tablet
379.00
4.3
Samsung Galaxy Tab 3 (7-Inch, White)
179.00
4.2
Dell Venue 8 Pro 32 GB Tablet (Windows 8.1)
229.00
4.0
Microsoft Surface Pro Tablet (128 GB Hard Drive, 4 GB RAM, Windows 8 Pro)
625.00
4.2
Microsoft - Surface Pro 2 with 128GB - Dark Titanium
994.95
4.1
1 #include<iostream>
2 #include <string>
3 #include <cstdlib>
4 #include <fstream>
5 #include <iomanip>
6 using namespace std;
7
8 struct Product {
9 string name;
10 float price;
11 float reviews;
12 };
13
14 void bubbleSort(int arr[]) {
15 bool sorted = false;
16 int maxIdx = 10-1;
17 while (!sorted) {
18 sorted = true;
19
20 for (int i = 0; i<10;i++) {
21 if(arr[i]>arr[i+1]) {
22 sorted = false;
23
24 int tmp = arr[i];
25 arr[i] = arr[i+1];
26 arr[i+1] = tmp;
27 }
28 }
29 }
30 }
31
32 void printProduct(Product & myProduct) {
33 cout<<"$ "<<setprecision(2)<<fixed<<myProduct.price<<" ";
34 cout<<setprecision(1)<<fixed<<myProduct.reviews<<" stars: ";
35 cout<<myProduct.name<<endl;
36 }
37 int main() {
38 ifstream inFile;
39 inFile.open("products.txt");
40
41 Product myProducts[10];
42 Product myProduct;
43 myProduct.name = "Nexus 7";
44 myProduct.price = 239.99;
45 myProduct.reviews= 4.2;
46
47
48 string line;
49 getline(inFile, line);
50 int numRecords = atoi(line.c_str());
51
52 for (int i = 0; i < numRecords; i++) {
53 getline(inFile, line);
54 myProduct.name = line;
55 getline(inFile, line);
56 myProduct.price = atof(line.c_str());
57 getline(inFile, line);
58 myProduct.reviews = atof(line.c_str());
59 cout<<"$ "<<setprecision(2)<<fixed<<myProduct.price<<" ";
60 cout<<setprecision(1)<<fixed<<myProduct.reviews<<" stars: ";
61 cout<<myProduct.name<<endl;
62 }
63
64 for (int i = 0; i < 10; i++) {
65 printProduct(myProducts[i]);
66 }
67 return 0;
68 }
Explanation / Answer
Make a string array, with the index variable x which would hold a certain line. After you use the getline function in increase x by one and keep iterating until you have reached the final line.
So something like this---
string words
string array [100000]
int x=1
words= getline();
string array [x] =words;
x = x+1;
repeat until the last line is reached.
You will require a "for" loop
for (x=1 ; x<last_line ; x++)
{
words= getline();
string array [x] =words;
}
last_line should be a integer type and you will need to specify the last line number with some function in a C++ library.
Hope this helps, good luck.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.