Th e Whippet Bus Company charges prices for tickets based
on distance traveled, as shown in the accompanying table.
Distance (miles) Ticket Price ($)
0 – 99 25.00
100 – 299 40.00
300 – 499 55.00
500 and farther 70.00
Write a program that allows a user to enter a trip distance.
Th e output is the ticket price
My question is how do i represent distance? cause it has number range so i am not really sure how to use array here
Loading
Posted Jan 7, 2012, 3:46 PM
public class WhippetBus
{
public static void Main()
{
double[] distance = { 0, 100, 300, 500 };
int[] tPrice = { 25, 40, 55, 70 };
int x = 3;
Console.Write("Enter Distance in miles: ");
double d = Convert.ToDouble(Console.ReadLine());
while (x >= 0 && d < distance[x])
--x;
Console.WriteLine("Ticket Price for {0} mile is {1}", d, tPrice[x].ToString("c"));
Console.ReadKey();
}
}
Ankit ShivnakrPosted Jan 10, 2012, 8:03 AM
Please Find Attachment and run it.
you will find you accepted result
Posted Jan 10, 2012, 7:45 AM
Zuber KaziPosted Jan 10, 2012, 7:21 AM
{
static void Main(string[] args)
{
Console.WriteLine("enter the distance");
int distance = Convert.ToInt32(Console.ReadLine());
if (distance > 0 && distance < 100)
Console.WriteLine("25$");
else if (distance > 99 && distance < 300)
Console.WriteLine("40$");
else if (distance > 299 && distance <499 )
Console.WriteLine("55$");
else if (distance > 500)
Console.WriteLine("70$");
Console.ReadLine();
}
Posted Jan 8, 2012, 5:18 AM
Now we say we input 1000 miles. So inside the while condition x is true (because initial value of x is 3) and the distance is false (because initial value of distance[x] is 500 which is less than 1000 (which we input)) therefore condition is false - loop will end and the execution will go to next line. Here is very important, which is the next line? Not the decrement value of x (that means –-x) because loop ended but the line which is telling ticket price.
Now we say our input is 100. So inside the while condition x is true (because initial value of x is 3) and the distance is true (because initial value of distance[x] is 500 which is greater than 100 (which we input)) therefore condition is true. Therefore here the loop will not end because condition is true. Execution will go to next line which is next line that is decrement of x value –-x.
Now x=2 and distance[x] = 300 (which is greater than input value that is 100) condition is true loop will continue.
Now x=1 and distance[x] = 100 (which is equal to input value that is 100) condition is false loop will end and the execution will go to next line which will tell the ticket price.
Concept is loop must end when the value of the input distance is greater or equal to distance[x] but at the same time it must show the relevant price.
Anyone can explain better than this is highly appreciated.
Prime bPosted Jan 8, 2012, 1:16 AM
Prime bPosted Jan 7, 2012, 9:59 PM
This is what i have done
int[] distance = new int[] {0, 100, 300, 500 };
int[] ticketPrice = new int[] { 25, 40, 55, 70 };
int distanceInput;
double ticketPrice1 = 0;
bool found = false;
Console.WriteLine("Please enter the distance you traveled");
distanceInput = Convert.ToInt32(Console.ReadLine());
for (int x = 0; x < distance.Length; ++x)
{
if(distanceInput == distance[x])
{
found = true;
ticketPrice1 = ticketPrice[x];
}
}
if(found)
Console.WriteLine("It will cost you {0}", ticketPrice1);
else
Console.WriteLine("Too bad");
VulpesPosted Jan 7, 2012, 3:14 PM
You can do this because you know that the upper bound will be one less than the lower bound of the next element in the array, apart of course from the final element.