190 likes | 305 Views
C++ crash course. Class 8 statements, sort, flight times program. Agenda. Last week: operators, expressions This week: Finishing the basics - statements new control flow statements a simple sort - Insertion Flight times program (due Wednesday 10pm) What ’ s the best flight to take?
E N D
C++ crash course Class 8 statements, sort, flight times program
Agenda Last week: operators, expressions This week: • Finishing the basics - statements • new control flow statements • a simple sort - Insertion • Flight times program (due Wednesday 10pm) • What’s the best flight to take? • Mazes program (due Friday 10pm) • Given a maze, what’s the best path through it? • Exam on Friday • Much like the last one, covering all the material in the three weeks
Statements • A statement in C++ is really just a line of code, ending with a semicolon • We’ve gone over a lot of statements: • if • while • for • Let’s talk about a few others • switch / case • break, continue
switch / case • If / else statements aren’t always the cleanest way to change how a program acts • switch statements help deeply nested if / else logic
switch / case Writing a program: • Read each character until there are no more characters • Compare each character to the set of vowels • If the character matches one of the vowels, add 1 to that vowel’s count • Display the results
switch / case How would we write the program with if / else statements?
switch / case • It’s also possible to do this much more easily with a switch / case • syntax: switch (var) { case const_or_literal: // do something break; case const_or_literal2: ... } How can we change our character reading program to use switch / case?
switch / case • switch / case is a little tricky though, because once execution enters a certain point in a switch / case, it will continue until it encounters “break” • make sure you put a break at the end of every case unless you want to do strange things Let’s change our program: Use switch / case syntax to count ANY vowel
switch / case • Using the default label is like having an else: execution will go there if it doesn’t match any other case • Switch expressions can be anything, but will be converted to ints • Case labels must be constant integral types // bad case 3.14: case ival:
break, continue • Some loops you only want to continue until a certain thing happens • Searching a vector for a value, we don’t want to keep searching once we’ve found it • Adding a break in the middle of a loop will exit the loop • Adding a continue in the middle of a loop will go to the next execution of a loop (stops current execution and goes to the next round)
break, continue • break can only occur inside of switch statements or loops (while, for) • can occur inside of if statements when they are nested inside of switch statements or loops
break, continue How might we search an int vector using the break statement? We want to find an instance of value in the vector, and add 1 to it afterwards
insertion sort • There are a number of ways to sort a set of numbers, or other items • We’ll talk about insertion sort as a simple example How do we sort these numbers with insertion sort? 5 87 14 23 62 54 10 19
insertion sort • Sort of similar to how people sort cards • Take each element as it’s encountered, put it in the right place in the sorted list • Intuitive idea; how can we get a computer to do it?
Designing Programs • We have a whole lot of pieces now! • Let’s write something useful with it • Flight Times program • from a set of inputs, determine what the best (shortest), average, and worst (longest) flight times are from a set of inputs
Flight Times Program • Inputs how many flights? 5 enter flight times in military HHMM format, one per line 1030 1415 1535 1800 2523 2304 0530 0835 0605 1270 invalid flight times: 2523-2304 invalid flight times: 0605-1270 shortest flight: 1535-1800, elapsed time 2 hrs 25 mins longest flight: 1030-1415, elapsed time 3 hrs 45 mins average flight time: 3 hrs 5 mins
Flight Times Program • First work out the algorithm • What steps do we need to take to solve the problem? • Next work out the way to code the solution • What data structures do we need? • What functions should we write? • What classes do we need? • Try coding the solution!
Flight Times Program • Given the input: 1030 1415 ...how are we going to parse that?
Closing • FlightTimes spec and base code will go up before tomorrow • Tomorrow: we work through the rest of FlightTimes • also: short intro to basic debugging • You’ll get two more non-required homeworks, one due tomorrow and one due Thursday (for practice with the concepts) • FlightTimes due Wednesday 10pm