Question

Replace "#include <algorithm>" and "sort(stalls, stalls + count, comparator2);" with simple and ba...

Replace "#include <algorithm>" and "sort(stalls, stalls + count, comparator2);" with simple and basic C++

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

#define MAX 100

struct Stall
{
string stallName;
float income;
float expense;
};

// to sort in decreasung net income
bool comparator(Stall x, Stall y)
{
return (x.income - x.expense) > (y.income - y.expense);
}

// to sort in increasing profit
bool comparator2(Stall x, Stall y)
{
return ((x.income - x.expense) < (y.income - y.expense));
}

int main()
{
Stall stalls[MAX];
string name;
float income, expense;

string fileName = "stalls.txt";
fstream file;
file.open(fileName.c_str(), ios::in);
fstream outfile("output.txt", ios::out);

int i = 0;

while (true)
{
file >> name;

if (name.compare("XXXXXX") == 0 || name.compare("xxxxxx") == 0)
{
break;
}

file >> income;
file >> expense;

stalls[i].stallName = name;
stalls[i].income = income;
stalls[i].expense = expense;

i++;
}

int count = i;

sort(stalls, stalls + count, comparator);

int countProfit = 0;
long totalProfit = 0;

outfile << "Report in decreasing order of the income -" << endl;
for (int i = 0; i < count; i++)
{
outfile << stalls[i].stallName << " " << stalls[i].income - stalls[i].expense << endl;

if ((stalls[i].income - stalls[i].expense) > 0)
{
countProfit++;
totalProfit += stalls[i].income - stalls[i].expense;
}
}

outfile << endl << "Stats: -" << endl;
outfile << "Number of stalls: " << count << endl;
outfile << "Number of stalls which made the profit: " << countProfit << endl;
outfile << "Total profit from all the stalls: " << totalProfit << endl;

sort(stalls, stalls + count, comparator2);
outfile << "Stalls with profits: ";
for (int i = 0; i < count; i++)
{
if ((stalls[i].income - stalls[i].expense) > 0)
outfile << stalls[i].stallName << " ";
}
outfile << endl;

file.close();

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: This is the exact version of your existing program using simple c++ code instead of using algorithm.h and sort method. I have not changed any logic in the code; just changed the technique of sorting.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

#define MAX 100

struct Stall {

    string stallName;

    float income;

    float expense;

};

//method to sort the stalls in descending order of net income/profit

void sortByNetIncome(Stall *stalls, int size){

                //using bubble sort algoithm to sort the stalls

                for(int i=0;i<size;i++){

                                for(int j=0;j<size-1;j++){

                                               if((stalls[j].income-stalls[j].expense) < (stalls[j+1].income-stalls[j+1].expense)){

                                                               //swapping elements at indices j and j+1

                                                               Stall temp=stalls[j];

                                                               stalls[j]=stalls[j+1];

                                                               stalls[j+1]=temp;

                                               }

                                }

                }

}

//method to sort the stalls in ascending order of net income/profit

void sortByNetProfit(Stall *stalls, int size){

                for(int i=0;i<size;i++){

                                for(int j=0;j<size-1;j++){

                                               if((stalls[j].income-stalls[j].expense) > (stalls[j+1].income-stalls[j+1].expense)){

                                                               Stall temp=stalls[j];

                                                               stalls[j]=stalls[j+1];

                                                               stalls[j+1]=temp;

                                               }

                                }

                }

}

int main()

{

    Stall stalls[MAX];

    string name;

    float income, expense;

    string fileName = "stalls.txt";

    fstream file;

    file.open(fileName.c_str(), ios::in);

    fstream outfile("output.txt", ios::out);

    int i = 0;

    while (true) {

        file >> name;

        if (name.compare("XXXXXX") == 0 || name.compare("xxxxxx") == 0) {

            break;

        }

        file >> income;

        file >> expense;

        stalls[i].stallName = name;

        stalls[i].income = income;

        stalls[i].expense = expense;

        i++;

    }

    int count = i;

    //sorting in descending order of income/profit

                sortByNetIncome(stalls,count);

               

    int countProfit = 0;

    long totalProfit = 0;

    outfile << "Report in decreasing order of the income -" << endl;

    for (int i = 0; i < count; i++) {

        outfile << stalls[i].stallName << " " << stalls[i].income - stalls[i].expense << endl;

        if ((stalls[i].income - stalls[i].expense) > 0) {

            countProfit++;

            totalProfit += stalls[i].income - stalls[i].expense;

        }

    }

    outfile << endl

            << "Stats: -" << endl;

    outfile << "Number of stalls: " << count << endl;

    outfile << "Number of stalls which made the profit: " << countProfit << endl;

    outfile << "Total profit from all the stalls: " << totalProfit << endl;

                //sorting in ascending order of income/profit

    sortByNetProfit(stalls,count);

    outfile << "Stalls with profits: ";

    for (int i = 0; i < count; i++) {

        if ((stalls[i].income - stalls[i].expense) > 0)

            outfile << stalls[i].stallName << " ";

    }

    outfile << endl;

    file.close();

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
Replace "#include <algorithm>" and "sort(stalls, stalls + count, comparator2);" with simple and ba...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Fix the following program so that input is from a file “c:\comp\asg1\stalls.txt” and output is from...

    Fix the following program so that input is from a file “c:\comp\asg1\stalls.txt” and output is from a file “c:\comp\asg1\stalls_output.txt” #include <iostream> #include <fstream> using namespace std; struct Stall { string name; double income; double expenses; double net; }; int main() { double tprofit_loss = 0, most_profit; Stall tmp; int n = 0; Stall Stalls[100]; bool loop = true; ifstream f; f.open("stalls.txt"); ofstream of; of.open("output.txt"); while (loop) { f >> tmp.name; if (tmp.name == "XXXXXX" || tmp.name == "xxxxxx") { loop...

  • #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock...

    #include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • Write this program in c++:

    test.txtLab7.pdfHeres the main.cpp:#include "Widget.h"#include <vector>#include <iostream>#include <string>#include <iomanip>#include <fstream>using std::ifstream;using std::cout;using std::cin;using std::endl;using std::vector;using std::string;using std::setprecision;using std::setw;bool getWidget(ifstream& is, Widget& widget){ string name; int count; float unitCost; if (is.eof())  { return false; } is >> count; is >> unitCost; if (is.fail())  { return false; } is.ignore(); if (!getline(is, name))  { return false; } widget.setName(name); widget.setCount(count); widget.setUnitCost(unitCost); return true;}// place the definitions for other functions here// definition for function mainint main(){ // Declare the variables for main here  // Prompt the...

  • #include #include #include #include #include #include #include using namespace std; const int MAX = 26; string...

    #include #include #include #include #include #include #include using namespace std; const int MAX = 26; string addRandomString(int n) {    char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',    'h', 'i', 'j', 'k', 'l', 'm', 'n',    'o', 'p', 'q', 'r', 's', 't', 'u',    'v', 'w', 'x', 'y', 'z' };    string res = "";    for (int i = 0; i < n; i++)    res = res + alphabet[rand() % MAX];    return res;...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • The 4th deliverable is to create the program the makes the buy recommendation. It uses the...

    The 4th deliverable is to create the program the makes the buy recommendation. It uses the class Stock to store and retrieve the stock information. I need to make this program compatible with my stocks class. Program I need to change: #include <iostream> #include <cmath> #include <fstream> #include <iomanip> #include <string> #include <stdlib.h> using namespace std; // read the data file, store in arrays int openDatafile(ifstream&,string [], float[], float[], int[]); // opens the data file void readData(ifstream &, string[], float[],...

  • How would I add a line to the main function to sort the people vector by...

    How would I add a line to the main function to sort the people vector by Age using the STL sort function? #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; struct Person { string name; int age; string favoriteColor; }; bool sortByName(Person &lhs, Person &rhs) { return lhs.name < rhs.name; } bool sortByAge(Person &lhs, Person &rhs) { return lhs.age < rhs.age; } bool sortByColor(Person &lhs, Person &rhs) { return lhs.favoriteColor < rhs.favoriteColor; } int main() { vector<Person>...

  • #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int...

    #include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT