Engineering Computation Workshop 1 === ###### tags: `comp20005` `workshop` `c` --- ### Austen School `Bcom 2015-2018` + `MSc 2019-2021` Languages I like to use `python` `java` `golang` `javascript` `c` `cpp` --- ### Icebreakers :face_palm: Name, Major & What you did over the summer :sun_with_face: --- ### 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 --- ### Numbers In & Out How do I read in a 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); ``` --- ### 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-15T04:32:42.008Z","metaMigratedFrom":"Content","title":"Engineering Computation Workshop 1","breaks":true,"contributors":"[{\"id\":\"097a8b2e-1817-41aa-b11f-65c49c54dbaf\",\"add\":3104,\"del\":191}]"}
    310 views