From 32b02c8dfba1c23caa759c06c583ca11c3eb30ac Mon Sep 17 00:00:00 2001 From: Trance-0 <60459821+Trance-0@users.noreply.github.com> Date: Sun, 9 Feb 2025 23:19:27 -0600 Subject: [PATCH] update --- pages/CSE332S/CSE332S_L7.md | 80 ++++++++++++++++++++++++++++++++++++- pages/CSE332S/CSE332S_L8.md | 6 ++- 2 files changed, 84 insertions(+), 2 deletions(-) diff --git a/pages/CSE332S/CSE332S_L7.md b/pages/CSE332S/CSE332S_L7.md index 9b385d4..7bf8da5 100644 --- a/pages/CSE332S/CSE332S_L7.md +++ b/pages/CSE332S/CSE332S_L7.md @@ -1 +1,79 @@ -# Lecture 7 \ No newline at end of file +# Lecture 7 + +## Debugging + +Debugger let’s us: + +1. Execute code incrementally + a. Line by line, function to function, breakpoint to breakpoint +2. Examine state of executing program + a. Examine program call stack + b. Examine variables + +When to debug: + +1. Trace how a program runs +2. Program crashes +3. Incorrect result + +### Basic debugging commands + +Set breakpoints +Run program - program stops on the first breakpoint it encounters +From there: + +- Execute one line at a time +- Step into (step out can be useful if you step into a function outside of your code) +- Step over + +- Execute until the next breakpoint (continue) + +While execution is stopped: + +- Examine the state of the program + - Call stack, variables, ... + +### Lots of power, but where to start? + +Stepping through the entire program is infeasible + +Think first!!! + +- What might be going wrong based on the output or crash message? +- How can I test my hypothesis? + - Can I narrow down the scope of my search? + - Can I recreate the bug in a simpler test case/simpler code? +- Set breakpoints in smart locations based on my hypothesis + +### Today’s program + +A simple lottery ticket game + +1. User runs the program with 5 arguments, all integers (1-100) +2. Program randomly generates 10 winning numbers +3. User wins if they match 3 or more numbers + +At least that’s how it should run, but you will have to find and fix a few issues first + +First, let’s look at some things in the code + +- Header guards/pragma once +- Block comments: Who wrote this code? and what does it do? +- Multiple files and including header files + - **Do not define functions in header files, declarations only** + - **Do not #include .cpp files** + - Function or data type must be declared before it can be used + +#### Header Guards + +```cpp +#pragma once // alternative to traditional header guards, don't need to do both. +#ifndef ALGORITHMS_H +#define ALGORITHMS_H +#include +void insertion_sort(std::vector & v); +bool binary_search(const std::vector & v, int value); +#endif // ALGORITHMS_H +``` + +The header guard is used to prevent the header file from being included multiple times in the same file. diff --git a/pages/CSE332S/CSE332S_L8.md b/pages/CSE332S/CSE332S_L8.md index ea02e1a..1154860 100644 --- a/pages/CSE332S/CSE332S_L8.md +++ b/pages/CSE332S/CSE332S_L8.md @@ -1 +1,5 @@ -# Lecture 8 \ No newline at end of file +# Lecture 8 + +## From procedural to object-oriented programming + +