1 / 19

C++ crash course

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?

kaiser
Download Presentation

C++ crash course

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ crash course Class 8 statements, sort, flight times program

  2. 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

  3. 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

  4. 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

  5. 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

  6. switch / case How would we write the program with if / else statements?

  7. 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?

  8. 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

  9. 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:

  10. 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)

  11. 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

  12. 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

  13. 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

  14. 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?

  15. 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

  16. 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

  17. 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!

  18. Flight Times Program • Given the input: 1030 1415 ...how are we going to parse that?

  19. 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

More Related