At a school’s bazaar, activities were divided into ‘stalls’. At the close of the bazaar, the manager of each stall submitted information to the Principal consisting of the name of the stall, the income earned and its expenses. Some sample data were:
Games 2300.00 1000.00
Sweets 900.00 1000.00
Using a structure to hold a stall’s data, write a program to read the data and print a report consisting of stall name and net income (income - expenses), in order of increasing net income using selection sort.
1
Expert's answer
2018-06-11T09:31:33-0400
#include <iostream> #include <string> using namespace std;
struct stall{ string name; double income; double expenses; double net_income(){ return (income - expenses); } }; void selection_sort(stall array[], int size) { for (int i = 0; i < size-1; i++){ int l = i; for (int j = i + 1; j < size; j++) { if(array[j].net_income() < array[l].net_income()){ l = j; } } swap(array[i], array[l]); } } int main(){ int n; cout << "Enter the amount of stalls: " << endl; cin >> n; stall *array = new stall[n]; string tmp_name; double tmp_income; double tmp_expenses; for (int i = 0; i < n; i++) { cout << "Enter stall's "<< i+1<<" name" << endl; cin >> tmp_name; cout << "Enter stall's "<< i+1<<" income" << endl; cin >> tmp_income; cout << "Enter stall's "<< i+1<<" expenses" << endl; cin >> tmp_expenses; array[i].name = tmp_name; array[i].income = tmp_income; array[i].expenses = tmp_expenses; } selection_sort(array, n); cout << "\t\tNet income" << endl; for (int i = 0; i < n; i++) { cout << "Stall #" << i + 1; cout <<"\t "<< array[i].net_income() << endl; } }
Comments
Leave a comment