Create a program to help a generous person who wants to distribute money among needy people. He wants to serve those persons first whose bank balance is least but if there are two persons with same balance then he wants to check the number of dependents of both persons. Use the best-suited data structure to form a fair money distribution system.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Money{
public:
int balance;
int dependents;
int index;
void input()
{
cout<<"Enter Bank Balance : ";
cin>>balance;
cout<<"Enter number of Dependents : ";
cin>>dependents;
}
void display()
{
cout<<"Bank Balance : "<<balance<<endl;
cout<<"Number of dependents : "<<dependents<<endl;
}
};
bool compare(Money a, Money b)
{
if(a.balance == b.balance)
{
return a.dependents > b.dependents;
}
return a.balance < b.balance;
}
int main()
{
cout<<"Enter number of persons : ";
int n;
cin>>n;
Money m[n];
cout<<endl;
for(int i=0; i<n; i++)
{
cout<<"Person"<<i+1<<endl;
m[i].input();
m[i].index = i+1;
cout<<endl;
}
sort(m, m+n, compare);
cout<<endl<<endl<<"Displaying the order of Money Distribution to people : "<<endl;
cout<<"--------------------------------------------------------------"<<endl;
for(int i=0; i<n; i++)
{
cout<<"Person"<<m[i].index<<endl;
m[i].display();
cout<<endl;
}
}
Comments
Leave a comment