Saturday, 15 August 2015

arrays - I cannot solve this simple exercise in Deitel Visual C# 2010 -



arrays - I cannot solve this simple exercise in Deitel Visual C# 2010 -

i've been studying deitel's visual c# 2010. i'm stuck @ exercise in chapter arrays. it's been bugging me days now, , i've written code several times , each times goes wrong.

the exercise asking develop new reservation scheme little airline company. capacity of aeroplane 10 seats. inquire client input 1 first class , 2 economy. seats 1 5 first class. seats 6 10 economy.

i must utilize one-dimensional array of type bool represent seating char of plane. initialize elements of array false represent vacant seats (luckily bool initializes false anyway, because not know how initialize array). each seat assigned, set corresponding element in plane true.

the app should never assign seat that's been seated. when economic scheme class full, app should inquire client if wants fly in first class (and vice versa). if client responds yes, assign him in economic scheme class (if there's empty seat there). if there no empty seats in economic scheme or client refuses fly in first class, display him "the next flight after 3 hours!).

i'm self-studying book. not assignment or homework. not wish post code i've written because want fresh way solve problem, i'm pretty sure asked code, here is

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace craps_game { class airlinereservation { private static bool[] seats = new bool[10]; private static void firstclass(ref bool[] seats) { for(int = 0; < 5; i++) { if (seats[i] == false) { seats[i] = true; console.writeline("your seat number " + i); break; } else { console.writeline("first class full. fly economy?"); if (console.readline().tolower() == "y") economy(ref seats); else console.writeline("the next flight in 3 hours!. bye"); } } } private static void economy(ref bool[] seats) { (int = 5; < 10; i++) if (seats[i] == false) seats[i] = true; else { console.writeline("economy class full. fly first class"); if (console.readline().tolower() == "y") firstclass(ref seats); else console.writeline("the next flight in 3 hours!. bye"); } } public static void reservation() { { console.writeline("enter 1 fly first class"); console.writeline("enter 2 fly economic scheme class"); if (convert.toint32(console.readline()) == 1) firstclass(ref seats); else economy(ref seats); } while (true); } }

}

bear in mind prefer different way of solving problem, instead of fixing 1 :)

"i must utilize one-dimensional array of type bool represent seating char of plane"

"i want fresh way solve problem"

"bear in mind prefer different way of solving problem, instead of fixing one"

so it! others have given advice already, here's "fresh" way it.

using system; namespace funnyconsoleapplication { public class programme { static void main(string[] args) { aeroplane plane = new airplane(); bool reserve = true; while (reserve) { console.clear(); console.writeline("enter [1] fly first class ({0} vacant)", plane.vacantfirstclassseats()); console.writeline("enter [2] fly economic scheme class ({0} vacant)", plane.vacanteconomyseats()); string input = console.readline(); switch (input) { case "1": if (plane.hasfirstclassseats) { plane.reservefirstclassseat(); } else if (plane.haseconomyseats) { if (isok("no vacancy, come in [y] fly economic scheme instead?")) plane.reserveeconomyseat(); else reserve = false; } else { reserve = false; } break; case "2": if (plane.haseconomyseats) { plane.reserveeconomyseat(); } else if (plane.hasfirstclassseats) { if (isok("no vacancy, come in [y] fly first class instead?")) plane.reservefirstclassseat(); else reserve = false; } else { reserve = false; } break; } console.writeline(); } console.writeline("no can do, bye!"); console.readline(); } private static bool isok(string question) { console.writeline(question); homecoming string.compare(console.readline(), "y", stringcomparison.ordinalignorecase) == 0; } } public class aeroplane { private readonly bool[] _seats = new bool[10]; public bool hasfirstclassseats { { homecoming hasseats(0); } } public bool haseconomyseats { { homecoming hasseats(5); } } public int vacantfirstclassseats() { homecoming getvacant(0); } public int vacanteconomyseats() { homecoming getvacant(5); } public void reservefirstclassseat() { reserve(0); } public void reserveeconomyseat() { reserve(5); } private bool hasseats(int index) { (int = index; < index + 5; i++) { if (!_seats[i]) homecoming true; } homecoming false; } private int getvacant(int index) { int count = 0; (int = index; < index + 5; i++) { if (!_seats[i]) count++; } homecoming count; } private void reserve(int index) { (int = index; < index + 5; i++) { if (!_seats[i]) { _seats[i] = true; break; } } } } }

which gives

c# arrays

No comments:

Post a Comment