Foundations of Algorithms Workshop 1
===
###### tags: `comp10002` `workshop` `c`
---
### Austen
School
`Bcom 2015-2018` + `MSc 2019-2021`
Languages I like to use
`python` `java` `golang` `javascript`
`c` `cpp` `rust`
---
### Icebreakers
Introduce yourself to your group - name, major - then once everyone's gone you can start sharing ideas on the icebreaker question
---
### Icebreakers
> Calculate given an array $A$ of $n$ daily stock prices $s_i$, calculate the maximum profit $P$, if you can buy and sell one stock at a time. $1 \leq n \leq 100$, $1 \leq s_i \leq 8$.
```
A = [7,1,5,3,6,4] = 7
A = [1,2,3,4,5] = 4
A = [7,6,4,3,1] = 0
```
https://codeshare.io/new
---
### Lecture Recap
---
### #define
What do we use the following for?
```cpp=
#define
```
---
### #define
Defining something that isn't going to change in our program.
Example:
```cpp=
#define FIRST_NAME "Austen"
```
---
### #define
We can also define functions, these are called **Macros** e.g.
```cpp=
#define MIN(a,b) ((a) < (b) ? (a) : (b))
```
However this isn't necessary for this course
---
### Variables, Assignment and Types
```cpp=
int main(int argc, char **argv)
{
// declaration
int k;
double m;
// assignment
k = 2;
m = 2.0;
// evaluation
printf("%?\n", k/m)
return 0;
}
```
---
### Variables, Assignment and Types
```cpp
1.000000
```
---
### MinGW & GEdit
**Min**imalist
**G**NU for
**W**indows
Setup Instructions
---
### Code Editors
Jedit Is the only supported editor for this class. You can however use any editor so long as you use one!
---
### Numbers In & Out
How do I read in an single integer?
---
### Numbers In & Out
```cpp
int n;
scanf("%d", &n);
```
---
### Numbers In & Out
How do I read in an single float / double?
---
### Numbers In & Out
```cpp
double n;
float h;
scanf("%lf", &n);
scanf("%f", &n);
```
---
### Control Statements
> What's the output?
```cpp=
#include<stdio.h>
int main(int argc, char *argv[]) {
int i, j;
i = 3; j = 4;
if (i<j && j<6) {
i = i+j;
} else {
j = i+j;
}
printf("i=%d, j=%d", i, j);
return 0;
}
```
---
### Control Statements
> What's the ouput?
```cpp=
#include<stdio.h>
int main(int argc, char *argv[]) {
int i, j, k;
i = 3; j = 4; k = 7;
if ((i<j || j < k) && j<i) {
i = i+1;
if (i*i>k) {
k = k+1;
}
} else {
j = j+1;
if (i*i>k) {
k = k+2;
}
}
printf("i=%d, j=%d, k=%d\n", i, j, k);
return 0;
}
```
---
### Useful snippets
printing and taking input
```
#include <stdio.h>
```
compiling your program
```
gcc -Wall <filename.c> -o <outputfilename>
```
running your program
```
./<outputfilename>
```
standard program template
```cpp
int main(int argc, char *argv[]) {
return 0;
}
```
---
### Exercise 1.2
>Find out how to use the editor on your computer to create a file, and type in the "Hello World" program. Compile it and execute it.
---
```cpp
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello World!\n");
return 0;
}
```
---
### Exercise 2.8
>To convert from degrees Farenheit to degrees Celsius, you must first subtract 32, then multiply by 5/9.
>Write a program that undertakes this covnersion. Confirm that 212 Farenheit maps to 100 Celsius, and that 82 Farenheit maps to a little under 28 degrees Celsius.
---
```cpp
#include <stdio.h>
int main(int argc, char*argv[])
{
double celsius, farenheit;
scanf("%lf", &celsius);
farenheit = (celsius - 32) * (5.0/9.0)
printf("Converted value from %lf Celsius is: %lf", celsius, farenheit;
}
```
---
### Exercise 2.4
>Write a simple program that has `#include <limits.h>` added at the top, and then print out the values of the following constants: `INT_MAX`, `INT_MIN`, `FLT_MIN`, `FLT_MAX`, `DBL_MIN` and `DBL_MAX`.
---
```cpp
#include <limits.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("INT_MAX: %d, INT_MIN: %d\n", INT_MAX, INT_MIN);
printf("FLT_MAX: %f, FLT_MIN: %f\n", __FLT_MAX__, __FLT_MIN__);
printf("DBL_MAX: %f, DBL_MIN: %f\n", __DBL_MAX__, __DBL_MIN__);
return 0;
}
```
{"metaMigratedAt":"2023-06-15T11:29:16.572Z","metaMigratedFrom":"Content","title":"Foundations of Algorithms Workshop 1","breaks":true,"contributors":"[{\"id\":\"097a8b2e-1817-41aa-b11f-65c49c54dbaf\",\"add\":4294,\"del\":68}]"}