Share Pastebin
Guest
Public paste!

question 4

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 5.36 KB | Hits: 74 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Assignment_3_Question_5
  7. {
  8.     class Program
  9.     {
  10.         //creating public arrays for racers and time
  11.         static string[] racers = new string[3];
  12.         static int[] rtime = new int[3];
  13.         //main method
  14.         static void Main(string[] args)
  15.         {
  16.             //delcaring final ordered racer and time variables
  17.             string racer1 = "";
  18.             string racer2 = "";
  19.             string racer3 = "";
  20.             int rtime1 = 0;
  21.             int rtime2 = 0;
  22.             int rtime3 = 0;
  23.             //title
  24.            
  25.             //do loop for program to continue until user wants to exit
  26.            
  27.            
  28.                 //do loop to check for errors in inputted data
  29.                 int count = 0;
  30.                 do
  31.                 {
  32.                     //asks user for input
  33.                     Console.WriteLine("Please Enter Racer {0}", (count + 1));
  34.                     racers[count] = Console.ReadLine();//gets user input
  35.                     do
  36.                     {
  37.                         rtime[count] = GetRacerTime(count);//sets racer time array entry at what ever the counters at from the return value from the getracertime method
  38.                     } while (rtime[count] < 0 || rtime[count] >= 181);//continues until data is within the 0-180 data range thats excepted
  39.                     count++;
  40.                 } while (count <= 2);//while statement for do loop
  41.                 //calling setradius method from circle class/object to set inputted radius from user
  42.                 OrderRacers(ref racer1, ref racer2, ref racer3, ref rtime1, ref rtime2, ref rtime3);//caled orderracers method and passes the reference for variables so method modifies the original variables and not there own
  43.                 Console.WriteLine("");
  44.                 Console.WriteLine("-----------------------------------------");
  45.                 Console.WriteLine("|  Fastest             Time (in minutes)      ");
  46.                 Console.WriteLine("| {0}                   {1}                   ", racer1, rtime1);
  47.                 Console.WriteLine("| {0}                   {1}                   ", racer2, rtime2);//displays output of the racers from fastest to slowest thats calculated from orderracers method
  48.                 Console.WriteLine("| {0}                   {1}                   ", racer3, rtime3);
  49.                 Console.WriteLine("|  Slowest");
  50.                 Console.WriteLine("-----------------------------------------");
  51.                 Console.WriteLine("");
  52.                 Console.WriteLine("");
  53.         }
  54.     }
  55.  
  56. public static void OrderRacers(ref string racer1, ref string racer2, ref string racer3, ref int rtime1, ref int rtime2, ref int rtime3)
  57.         {
  58.             //sets up counter and temp storage vars
  59.             string temp = null;
  60.             int tempn = 0;
  61.             int count = 0;
  62.             //continues in a loop to find the slowest racer
  63.             while (count < 3)
  64.             {
  65.                 if (rtime[count] > tempn)//compares new entry with the last slowest person found
  66.                 {
  67.                     temp = racers[count]; //if new slowest time is found its set to temp variable
  68.                     tempn = rtime[count]; //with the corresponding time
  69.                 }
  70.                 count++; //ups the counter
  71.             }
  72.             racer3 = temp;//unloads temp variables with slowest time into finalized variables that where passed on as reference
  73.             rtime3 = tempn;
  74.             //resets variables and functions like the last loop
  75.             tempn = 0;
  76.             count = 0;
  77.             while (count < 3)
  78.             {
  79.                 if (rtime[count] > tempn && rtime[count] != rtime3)
  80.                 {
  81.                     temp = racers[count];
  82.                     tempn = rtime[count];
  83.                 }
  84.                 count++;
  85.             }
  86.             racer2 = temp;
  87.             rtime2 = tempn;
  88.             //functions like last loop except finds the last remaining racer that was not added to the other entries making it the fastest entry (smallest number)
  89.             tempn = 0;
  90.             count = 0;
  91.             while (count < 3)
  92.             {
  93.  
  94.                 if (rtime[count] != rtime2 && rtime[count] != rtime3)
  95.                 {
  96.                     racer1 = racers[count];
  97.                     rtime1 = rtime[count];
  98.                 }
  99.                 count++;
  100.             }
  101.  
  102.         }
  103.  
  104. public static int GetRacerTime(int count)
  105.         {
  106.             /*Temporary Variable to store output*/
  107.             string temp;
  108.             int output = -1;
  109.             Console.WriteLine("Please Enter Time for Racer {0}", (count + 1));
  110.             temp = Console.ReadLine();
  111.             /*Try statement to attempt the variable data change*/
  112.             try
  113.             {
  114.                 /*converting a string to an int*/
  115.                 output = Convert.ToInt32(temp);
  116.                 if (output < 0 || output >= 181)
  117.                 //if statement to show error message if data is invalid
  118.                 {
  119.                     Console.WriteLine("----------Error-------");
  120.                     Console.WriteLine("|Error Invalid Number|");
  121.                     Console.WriteLine("----------------------");
  122.                 }
  123.                 else
  124. {
  125.     count1++;
  126. }
  127. }
  128. }
  129. }