Answer to Question #198794 in C++ for Ali Raza Butt

Question #198794

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.


1
Expert's answer
2021-05-27T01:57:18-0400
#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;
	}
	
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog