Skip to content

New to arrays

herbie

Asked Apr 13, 2007, 7:08 AM EDT3 answers
Share
LinkedInX
I am learning arrays and have a program that needs some help.  What I am trying to do is enter the miles of a trip then get a ticket price.  For example 0-99 miles would be $25, 100-299...$40,...300-499,...$55 farther than 500 miles is $70.  Here is what I have any pointers would be appericated.  I know I need to account for all the miles 0-500 Thanks

using System;
public class Miles

{
    public static string GetMiles()
    {
        string Miles;
        Console.WriteLine("Enter miles ");
        Miles = Console.ReadLine();
        return Miles;
    }
    public static void Main()
    {
        string Miles;
        Miles = GetMiles();
        int[] distanceMiles = {0, 100, 300, 500};
        double[] price = {25.00, 40.00, 55.00, 70.00};
        double customerMiles;
        int sub = 4;
        while(sub >= 0 && Convert.ToInt32(Miles) < distanceMiles[sub])--sub;
        customerMiles = price[sub];
    }
}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    using System;

    public class Miles

    {

    public static void Main()

    {

    int[] distanceMiles = {0, 100, 300, 500};

    double[] price = {25.00, 40.00, 55.00, 70.00};

    string Miles;

    double customerMiles, ticketPrice;

    int sub = 3;

    Console.Write("Enter miles ");

    Miles = Console.ReadLine();

    customerMiles = Convert.ToDouble(Miles);

    while(sub >= 0 && customerMiles < distanceMiles[sub])

    --sub;

    ticketPrice = price[sub];

    Console.WriteLine("Ticket Price : " +ticketPrice.ToString("c"));

    }

    }

    Important point to note: In C#, any array’s elements are numbered beginning with 0, therefore sub = 3 not 4

    0
  • 0
    C#
    using System;
    public class Miles
    {
    public static string GetMiles()
    {
    string Miles;
    Console.WriteLine("Enter miles ");
    Miles = Console.ReadLine();
    return Miles;
    }
    public static void Main()
    {
    string Miles;
    Miles = GetMiles();
    int[] distanceMiles = {0, 100, 300, 500};
    double[] price = {25.00, 40.00, 55.00, 70.00};
    double customerMiles, ticketPrice;
    int sub = 3;
    while(sub >= 0 && Convert.ToInt32(Miles) < distanceMiles[sub])
    --sub;
    ticketPrice = price[sub];
    Console.WriteLine("Ticket Price : " +ticketPrice.ToString("c"));
    }
    }
    0
  • 1 short reply
    • 0
      Thanks for the help...
      0