Lecture-7 - Assignment
Lecture-7 - Assignment
Lecture 7: Review
Programming-1, WS 2023 2
IDEs
• Visual studio code (vscode)
• Compiler, debugger
• Linux: gcc, gdb
• Windows: Cygwin, WSL
• Mac: clang, llvm
• Vscode on the web with docker (from cs50
course)
• https://cs50.dev/
Programming-1, WS 2023 3
Programming with vscode
• Open folder
• From menu “File” > “Open
folder …”
• Extensions
• Tab “Extensions”: find/manage
Vscode’s
extensions configuration
folder
• C/C++ Extension Pack
Source code
folders
Programming-1, WS 2023 4
Build a program
• Configure build task
• From menu “Terminal” > “Configure Tasks”
• Run build task
• From menu “Terminal” > “Run task”
Programming-1, WS 2023 5
Run and debug a program
• Configure debug
• From menu “Run” > “Open Configurations”
• Add (gdb) launch debugger
• Run debug
• From menu “Run” > “Start debugging”
Programming-1, WS 2023 6
References for configurations
• Linux
• https://code.visualstudio.com/docs/cpp/config-linux
• Windows
• MinGW:
https://code.visualstudio.com/docs/cpp/config-mingw
• WSL: https://code.visualstudio.com/docs/cpp/config-wsl
• MSVC:
https://code.visualstudio.com/docs/cpp/config-msvc
• Mac
• https://code.visualstudio.com/docs/cpp/config-clang-mac
Programming-1, WS 2023 7
Code formatter
• Open View > Command palette (Ctrl + Shift + P)
• Find “format” Select “Format document”
Programming-1, WS 2023 8
Flow controls
• Loop
• Break
• Continue
• Switch
• Break
Programming-1, WS 2023 9
If statement
if (<expression>) <statement> // simple form with no {}'s or else clause
if (<expression>)
{
<statement>
}
else if (<expression>)
{
<statement>
}
else
{
<statement>
}
Programming-1, WS 2023 10
Switch statement
• Break switch (<expression>) {
case <const-expression-1>:
• Combine case <statement>
break;
case <const-expression-2>:
<statement>
break;
case <const-expression-3>: // here we combine case 3
and 4
case <const-expression-4>:
<statement>
break;
default: // optional
<statement>
}
Programming-1, WS 2023 11
Break and continue in Loops
• Break: exit the loop
while (<expression>)
• Continue: continue {
<statement>
the loop if (<condition>)
break;
<statement>
if (<condition>)
continue;
<statement>
}
Programming-1, WS 2023 12
Debug using breakpoint
• The program will Executed
pause whenever it
hits the breakpoint Click to set a
Not executed
breakpoint
struct fraction {
int numerator;
int denominator;
};
Programming-1, WS 2023 15
Pointer to a structure: dereferencing
struct fraction {
int numerator;
int denominator;
};
• Using “*”
struct fraction *f1_p;
(*f1_p).numerator = 22;
(*f1_p).denominator = 7;
• Using “->”
struct fraction *f1_p;
f1_p->numerator = 22;
f1_p->denominator = 7;
Programming-1, WS 2023 16
Check solution
• Linked list
• Binary tree
Programming-1, WS 2023 17
Deallocate memory
• Linked list
• Binary tree
Programming-1, WS 2023 18
Multi array
• Allocate 2d array using int ** array pointer
Programming-1, WS 2023 19
Summary
• IDEs (vscode), debugging
• Pointer
• Multi-array
Programming-1, WS 2023 20