Plz solve with one dimintion array: DATA.txt chair 5 2 9 low round_table1 10 4 9 medium square_table 5 3 9 low stand_lamp1 6 1 17 medium pillow_cover1 9 2 15 low BLUE_chair 8 2.7 15 high X_chair 5 2 9 low round_table2 10 4 4 medium square_table 5 2 9 low stand_lamp2 6 1 17 medium pillow_cover2 9 4 15 low round_lamp 8 3 13 high
Plz solve with one dimintion array:
DATA.txt
chair 5 2 9 low
round_table1 10 4 9 medium
square_table 5 3 9 low
stand_lamp1 6 1 17 medium
pillow_cover1 9 2 15 low
BLUE_chair 8 2.7 15 high
X_chair 5 2 9 low
round_table2 10 4 4 medium
square_table 5 2 9 low
stand_lamp2 6 1 17 medium
pillow_cover2 9 4 15 low
round_lamp 8 3 13 high
Store the text in data.txt
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string line;
ifstream file("data.txt");
string item_name[20];
int price_perunit[20];
int profit[20];
int months_of_stocking[20];
string customer_reviews[20];
int i = 0;
while (getline(file, line)) {
stringstream linestream(line);
string data;
int j = 0;
while (getline(linestream, data, '\t')) {
switch (j) {
case 0:
item_name[i] = data;
break;
case 1:
price_perunit[i] = stoi(data);
break;
case 2:
profit[i] = stoi(data);
break;
case 3:
months_of_stocking[i] = stoi(data);
break;
case 4:
customer_reviews[i] = data;
break;
default:
break;
}
j++;
}
i++;
}
int array_size = i;
string items_on_sale[array_size];
int percentage_of_sale[array_size];
string items_without_sale[array_size];
int total_profit = 0;
for (int i = 0; i < array_size; i++) {
total_profit += profit[i];
}
int avg_profit = total_profit / array_size;
int ios_index = 0;
int pos_index = 0;
int iws_index = 0;
for (int i = 0; i < array_size; i++) {
if (months_of_stocking[i] > 8 && profit[i] > avg_profit) {
if (customer_reviews[i] == "low") {
items_on_sale[ios_index++] = item_name[i];
percentage_of_sale[pos_index++] = 50;
} else if (customer_reviews[i] == "medium") {
items_on_sale[ios_index++] = item_name[i];
percentage_of_sale[pos_index++] = 30;
} else {
items_on_sale[ios_index++] = item_name[i];
percentage_of_sale[pos_index++] = 15;
}
} else if ((profit[i] * 100) / price_perunit[i] > 8) {
if (months_of_stocking[i] > 12) {
items_on_sale[ios_index++] = item_name[i];
percentage_of_sale[pos_index++] = 40;
} else {
items_without_sale[iws_index++] = item_name[i];
}
} else {
items_without_sale[iws_index++] = item_name[i];
}
}
cout << "\tSale items\t\tpercentage" << endl;
for (int i = 0; i < ios_index; i++) {
cout << "\t" << items_on_sale[i] << "\t\t" << percentage_of_sale[i] << endl;
}
cout << endl;
cout << "\n\t\tNon sale items" << endl;
for (int i = 0; i < iws_index; i++) {
cout << "\t\t" << items_without_sale[i] << endl;
}
return 0;
}
Step by step
Solved in 3 steps with 2 images