0% found this document useful (0 votes)
3 views

Lecture-7 - Assignment

lecture slides
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture-7 - Assignment

lecture slides
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Programming-1

Lecture 7: Review

Instructor: Cuong Tuan NGUYEN


Review
• Use of IDEs, build, debug
• Flow controls
• Character type
• Branching, looping

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

• The debug toolbar

Continue Step into Restart


Step over Step out Stop
Programming-1, WS 2023 13
Pointer: referencing, dereferencing
• Pointer reference to a int
{
*evenOddBit(int n, int *returnSize)

variable (using &) int *out = malloc(2 * sizeof(int));


*returnSize = 2;
int * int binSize;
int *bin = toBinary(n, &binSize);
p = &binSize …
}
binSize

int int *toBinary(int n, int *returnSize)


{
int size = (int)floor(log2(n)) + 1; //
Note: n > 0 as in leetcode description
int *out = malloc(size * sizeof(int));
*returnSize = size;
Programming-1, WS 2023 14
Structure review: accessing
elements
• Using ‘.’ to access the elements of struct

struct fraction {
int numerator;
int denominator;
};

struct fraction f1;


f1.numerator = 22;
f1.denominator = 7;

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

• Allocate n-dimensional array


• Recursive implementation
• Return (void *)

Programming-1, WS 2023 19
Summary
• IDEs (vscode), debugging
• Pointer
• Multi-array

Programming-1, WS 2023 20

You might also like