1

Ok so im completely lost i have never done any kind of c# coding and am trying to learn.

The task I have to do:

Calculate the parking fee for each customer, and put that out together with the hours parked.

There is a maximum parking fee of $20.00.

Calculate and output the average of the parking fees.

enter image description here

Overall Tasks: Read the data file ‘hours.txt’ into an array of data type integer Calculate the parking fee for each customer and output the hours parked and the parking fee.

Calculate the average parking fee Output the average parking fee (formatted to 2 decimal places) for now, declare your array and assign the numbers as shown below.

This is what i have come up with so far. And plaese remember that like i said im very NEW to this and I´m looking for some guidence.

        int[] hours;
        hours = new int[30];
        const decimal HOURLY_RATE = 2.50m;
        const decimal MAX_FEE = 20.00m; //Capped at S20.00
        decimal parkFee;
        decimal parkingCost = HOURLY_RATE;

        int[] hoursArray = { 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 };

        Console.WriteLine("Hours " + "Parking Fee ");

        int total = 0;
        double average = 0;
        for (int index = 0; index < hours.Length; index++)

        if (parkingCost > MAX_FEE)
            {
                parkFee = parkingCost * MAX_FEE;
                Console.WriteLine("Hours " + "Parking Fee ");
            }


        average = (double)total / hours.Length;
        Console.WriteLine("Average = " + average.ToString("N2"));
        Console.ReadKey();`
Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30
  • 2
    The issue you described, has nothing to do with C#. It's programming structure and affects many languages. You need to calculate the `parkFee = HOURLY_RATE * hoursArray[i];` and use that in the `if (parkFee > MAX_FEE) parkFee = MAX_FEE`. Use the debugger to step thru the code to see what values are in what variables. Giving a complete solution, won't help you. You need to grasp it. – Jeroen van Langen Apr 06 '17 at 08:45
  • 1
    And what is your question? Where specifically are you stuck? What step do you have problems with? Does your code do what it should? Or do you get any errors/unexpected results? – MakePeaceGreatAgain Apr 06 '17 at 08:49
  • 2
    Most of us cannot be bothered to read through code and figure out for ourselves what your issue is. Either tell us, or chances are, your question will be ignored. – ThePerplexedOne Apr 06 '17 at 08:50

2 Answers2

1

You never calculate your fee before you check if it's bigger than the maximum rate. Therefore you don't see any results, since your fee is never calculated (always 0). Also, you miss some braces for the for loop, so it will not loop through your code under the for loop.

A quick fix:

int[] hours = new int[30]{ 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 };
const decimal HOURLY_RATE = 2.50m;
const decimal MAX_FEE = 20.00m; //Capped at S20.00

Console.WriteLine("Hours and fee's parked per person");

decimal total = 0;
for (int index = 0; index < hours.Length; index++)
{
    decimal parkFee = HOURLY_RATE * hours[index];
    if (parkFee > MAX_FEE)
        parkFee = MAX_FEE;

    total += parkFee;
    Console.WriteLine("Hours: {0}. Parking Fee: {1}", hours[index], parkFee);      
}

decimal average = total / hours.Length;
Console.WriteLine("Average = {0}", average.ToString("N2"));
Console.ReadKey();

I also cleaned up some variables that are double and moved some variable declarations to the place where they are calculated. I also made a decimal value out of your total variable. You were using int which will only give you whole numbers which will lead to an inaccurate total amount of fee's. By using decimal you solve this problem. Lastly, I also changed you average value to a decimal, which seems to make more sense in this case since you use decimal for all variables so far.

EDIT: since you specifically ask for reading the array from a text file. This is how you create your hours array from your text file instead of declaring it in code:

const decimal HOURLY_RATE = 2.50m;
const decimal MAX_FEE = 20.00m; //Capped at S20.00

// See http://stackoverflow.com/a/556142/7397065 why you should use @ here
string path = @"C:\your\path\here.txt";
string stringFromFile = File.ReadAllText(path);
// Since your array is delimited on whitespace, use null as parameter
// You also need int's instead of string. So directly convert the results to int.
// We now make a List(), which also gives you more flexibility.
// Want to change your delimiter? See https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx
List<int> hours = stringFromFile.Split(null).Select(int.Parse).ToList();

Console.WriteLine("Hours and fee's parked per person");

decimal total = 0;
// Let's change this to a foreach. That's clearer to work with.
foreach (int parkingTime in hours)
{
    decimal parkFee = HOURLY_RATE * parkingTime;
    if (parkFee > MAX_FEE)
        parkFee = MAX_FEE;

    total += parkFee;
    Console.WriteLine("Hours: {0}. Parking Fee: {1}", parkingTime, parkFee);
}

// We have to use .Count now, since we use List()
decimal average = total / hours.Count;
Console.WriteLine("Average = {0}", average.ToString("N2"));
Console.ReadKey();
Jurjen
  • 1,376
  • 12
  • 19
  • You need to add a cast to `total += parkfee` You define total as int but then try adding an a decimal to it. –  Apr 06 '17 at 09:05
  • Thanks @John, I overlooked that! I just changed the `total` variable to `decimal` now. That seemed to make more sense, otherwise you won't get a proper average result. – Jurjen Apr 06 '17 at 09:14
  • Thanks heaps that gives me a a bit better of an idea is there a simple solution to reading from hours text file? Text file just reads 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 – Steve Adams Apr 06 '17 at 10:15
  • You can achieve that as follows: `string stringFromFile = File.ReadAllText(path);` where path is the location of your text file. As your delimiter is a white space, you should split the string on that: `string[] hours = stringFromFile.Split(null);`. You now have your `hours` array like you had at the beginning of your question. BTW, if this answer helped solve your question, don't forget to mark it as answer. – Jurjen Apr 06 '17 at 10:25
  • @SteveAdams I updated my answer according to your updated question. I hope it's more clear now! – Jurjen Apr 06 '17 at 12:18
  • Yeah that makes it clear but i think we have to do it by StreamReader fileSR = new StreamReader("hours.txt"); and read from the bin>debug folder really appreciate your help this is giving me a real good understanding – Steve Adams Apr 06 '17 at 14:07
0

Easiest solution would be to just iterate through hoursArray and check for the lowest value between each park period times rate and maximum fee. Then just add this to your total fees and after all divide this by your array's length :

for ( int i = 0; i < hoursArray.Length; ++i )
{
    // get the lowest value
    parkFee = Math.Min(hoursArray[i] * HOURLY_RATE, MAX_FEE);
    // add to total fees
    total += parkFee;
    // print current iteration state
    Console.WriteLine("Hours : {0}, Parking fee : {1}", hoursArray[i], parkFee);
}

// calculate the average
average = total / (double)hoursArray.Length;
// print results
Console.WriteLine("Average : {0}", average.ToString("N2"));
mrogal.ski
  • 5,828
  • 1
  • 21
  • 30