Reputation: 33
I have just started learning C# coding and my latest assignment question on arrays requires the output shown in example picture, I will put in the code I have written so far. My problem is that when I run the program I have spaces between the lines and the 2 tables don't quite line up, does any one have any idea how I can re position to look like sample pic? Thanks!
This is sample output
int[] hrs = { 8, 24, 9, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8, 8, 9, 7, 9, 15, 6, 1, 7, 6, 12, 10, 11, 23, 1, 2, 9, 8 };
decimal fee;
const decimal HOURLY_RATE = 2.5m, MAX_FEE = 20;
decimal avg = 0;
decimal total = 0;
Console.WriteLine("Hours Parking Fee");
for (int count = 0; count < hrs.Length; count++)
{
Console.WriteLine("{0, 3}", hrs[count]);
fee = hrs[count] * HOURLY_RATE;
if (fee > MAX_FEE)
{
fee = MAX_FEE;
}
Console.WriteLine("{0, 13}", fee.ToString("C"));
// calculate average fee paid
{
total = total + fee;
}
}
avg = total / 30; //average = total / 30;
Console.WriteLine("average parking fee: " + avg.ToString("C"));
Console.ReadKey();
Console.ReadKey();
Upvotes: 2
Views: 303
Reputation: 426
You are using WriteLine
which will add an entry in new line, it's better if you combine your output and print:
for (int count = 0; count < hrs.Length; count++)
{
//Console.WriteLine("{0,3}", hrs[count]);
fee = hrs[count] * HOURLY_RATE;
if (fee > MAX_FEE)
{
fee = MAX_FEE;
}
//Console.Write("{0,13}", fee.ToString("C"));
Console.WriteLine("{0,3} {1,13}", hrs[count], fee.ToString("C"));
// calculate average fee paid
{
total = total + fee;
}
}
Upvotes: 3