/    Sign up×
Community /Pin to ProfileBookmark

How to to calculate Splitting the Bill in C#

How to calculate Splitting the Bill in C# from a text file (expenses.txt) and write output to a new text file (expenses.txt.out)?
A number of friends go camping every year at provincial parks.

The group agrees in advance to share expenses equally, but it is not practical to have them share every expense as it occurs. So individuals in the group pay for particular things, like food, drinks, supplies, the camp site, parking, etc. After the camping trip, each person’s expenses are tallied and money is exchanged so that the net cost to each is the same. Read from a list of expenses, the amount of money that each person must pay or be paid.

Sample Input:
3 (# of people)
2 (# of bills for the 1st person)
10.00 (Amount paid by 1st person)
20.00 (Amount paid by 1st person)
4 (# of bills for the 2nd person)
15.00 (Amount paid by 2nd person)
15.01 (Amount paid by 2nd person)
3.00 (Amount paid by 2nd person)
3.01(Amount for the 2nd person)
3 (# of bills for the 3rd person)
5.00 (Amount paid by 3rd person)
9.00 (Amount paid by 3rd person)
4.00 (Amount paid by 3rd person)

2 (# of people)
2 (# of bills for the 1st person)
8.00 (Amount paid by 1st person)
6.00 (Amount paid by 1st person)
2 (# of bills for the 2nd person)
9.20 (Amount paid by 2nd person)
6.75 (Amount paid by 2nd person)
0 End of program

Output for Sample Input
($1.99)
($8.01)
$10.01

$0.98
($0.98)

This is what I tried So far-. But how would I distinguish people, bills and amount in this case?

[CODE]using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.IO;

namespace SplittingTheBill
{
class Program
{
static void Main(string[] args)
{
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader(“C:\Users\makhtar\Desktop\P S\SplittingTheBill\SplittingTheBill\expenses.txt”);

String line;
ArrayList BillArray = new ArrayList();

//Read the first line of text
line = sr.ReadLine();

//Continue to read until you reach end of file
while (line != null)
{
BillArray.Add(line);
//write the lie to console window
//Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}

//close the file
sr.Close();

foreach (var item in BillArray)
{
Console.WriteLine(item);
}

Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine(“Exception: ” + e.Message);
}
finally
{
Console.WriteLine(“Executing finally block.”);
}
}
}[/CODE]

to post a comment

6 Comments(s)

Copy linkTweet thisAlerts:
@acumenApr 25.2017 — Interesting.. have you already find the solution?
Copy linkTweet thisAlerts:
@Waleed123May 19.2018 — Have you found any solution then kindly share
Copy linkTweet thisAlerts:
@VITSUSAMay 19.2018 — This is really easy concept to calculate Splitting the Bill in C#.
Copy linkTweet thisAlerts:
@rootMay 19.2018 — This is obviously homework of some kind...

This is a math problem, go back to basics.

Answer this problem as if it were a math paper, then write the functions and routine based on that and when your program runs, if the output is not the same, you have to work out which is wrong, the code or your working out!
Copy linkTweet thisAlerts:
@NogDogMay 19.2018 — It's also a year old...closing.
Copy linkTweet thisAlerts:
@NogDogMay 19.2018 — {"locked":true}
×

Success!

Help @asifakhtar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...