CSS 133 Computer Programming for Engineers II

Introduction

Yusuf Pisan

1

YP Setting up MSYS2

Instructions at https://github.com/pisanorg/w/wiki/setting-up-your-development-environment

Class Instruction: 1. Copy msys2.zip from USB to your computer a. Give USB to another student 2. Unzip msys2.zip a. Right click on file and use 7-zip to extract 3. Move the directory, so it is installed at C:\msys64\ 4. Install CLion from https://www.jetbrains.com/clion/

Create a repl.it account and join repl.it class using invitation https://repl.it/classroom/invite/jji2dXX (jay-jay-eye-two-d-capital-ex-capital-ex)

2

YP CSS 133 - Building on CSS 132

● Input/Output Operators ● Basic data types (int, char, bool, float, double) ● Control structures (if, if-else, switch, ?) ● Iteration (for, while) ● (function calling itself) C++ PROGRAMMING ● Operators (&&, ||, +, +=, ==, !=, ++, ...) ● Functions (pass by value, reference, pointer, void or return value) ● Classes, objects (our own data types) ● string (data type from STL) ● Memory ● built in array ● vector from STL COMPUTATIONAL ● Reading data from a file ● assert (for debugging) THINKING ● Algorithms: linear search, binary search, selection sort

3

YP 133 Topics

● C -- 4 weeks ● Ch 1-8 ● Memory, Arrays and strings, I/O, Heap, Linked Lists ● Working with systems (CSS Linux Lab) ● Streams ● Inheritance ● Recursion ● STL Containers ● Stack ● Queue ● Search & Sort ● Tree ● Algorithms ● Complexity analysis

4

YP Grading

Quizzes and Exercises: 10% (can miss one without any penalty) Assignments: 20% (can submit one and only one 24 hours late) Exam-1: 20% Exam-2: 20% Final: 30%

Readings: Zybooks & "Programming for Engineers" IDE: CLion, CodeBlocks, VisualStudio, repl.it or any other

5

YP

Dennis Ritchie Ken Thompson Brian Kernighan Bell Labs Ritchie created the C programming language at in 1970s. Kernighan wrote the first C tutorial Ritchie & Thompson designed and implemented the original Unix system with significant contributions from Kernighan K & R Style (Kernighan and Richie) style indentation Different indentation styles at: https://en.wikipedia.org/wiki/Indentation_style

6

YP

Richard Stallman (RMS) Linus Trovalds Stallman launched the GNU Project in 1983 to create a Unix-like computer operating system composed entirely of free software. “GNU” is a recursive for “GNU's Not Unix.” Trovalds was the principal developer for the Linux kernel - First public version of Linux release in 1991 Free Software Foundation continues to refer to linux as GNU/Linux - Linux relies on g++, gcc and all other gnu tools

7

YP Hello World

#include All system libraries have .h

int main() { printf("Hello, World!\n"); return 0; printf and "\n" } rather than cout

gcc main.c (rather than g++)

8

YP C vs C++

● Standards K&R → C89 → C95 → C99 → C11 ● Variables declared anywhere in function ● Using // for comments ● stdbool.h so you can now use "bool bx = true;" ● const variables so you do not have to use #define

● printf int anumber = 10; printf("the variable anumber is %d\n", anumber);

char *cstr = "css133"; printf("Class name is %s\n", cstr);

double mypi = 3.1415; printf("mypi is %f\n", mypi);

printf("class %s, mypi is %f and anumber is %d\n", cstr, mypi, anumber); 9

YP C vs C++ (2)

● No reference variables or passing by reference, use pointers

● fgets char *cptr; char buffer[256]; printf("Enter stuff:\n"); cptr = fgets(buffer, 256, stdin); if(cptr != NULL) { printf("You typed : %s\n", cptr); // same as above line, puts for cstrings only // puts adds \n automatically puts(cptr); } buffer holds up to 255 characters or up to newline character add terminating null character ('\0') to value returned ● atoi (convert char* to int) ● atof (convert char* to float, actually type double),

10

YP C vs C++ (3)

● No classes in C, use struct instead (usually with typedef) struct point { int x; int y; }; struct point p; p.x = 0; p.y = 0;

typedef struct point3 { int x; int y; int z; } point3;

point3 p3; p3.x = 0; ...

11

YP Bitwise Operators

● Compression: Do you need all these variables? A 32-bit int can be used to store 32 Boolean variables. Normally, the minimum size for one Boolean variable is one byte. All types in C must have sizes that are multiples of bytes.

● Set operations: You can also use bits to represent elements of a (small) set. If a bit is 1, then element i is in the set, otherwise it's not. You can use bitwise AND to implement set intersection, bitwise OR to implement set union.

● Encryption: Swapping the bits of a string for e.g. according to a predefined shared key will create an encrypted string.

12

YP & (bitwise and)

| (bitwise or)

^ (bitwise exclusive or)

13

YP XOR Cipher

● Given a string, XOR with a "secret password" gives you encrypted string

● Given the "encrypted string", XOR with a "secret password" gives you the string back

● String "Wiki" is encrypted and decrypted with "11110011"

14

YP 0010 1101 << 1 becomes 0101 1010 << (left shift) Number is doubled 45(1+4+8+32) became 90 (2+8+16+64)

0010 1101 >> 2 becomes >> (right shift) 0000 1011 Number is halved twice 45(1+4+8+32) became 11 (1+2+8)

~0010 1101 becomes ~1101 0010 0's become 1's ~ (bitwise complement) 1's become 0's 15

YP Example int m = 45; int mleft = (m << 1); int mright2 = (mleft >> 2); printf("m is %d, mleft is %d, mleft is %d\n", m, mleft, mright2);

int csize = sizeof(char); printf("char is made up of %d byte\n", csize); // a = 5(0000 0101) char a = 5; // b = 9(0000 1001) char b = 9; printf("a&b should be 1(0000 0001) = %d\n", a & b); printf("a|b should be 13(0000 1101) = %d\n", a | b); printf("a^b should be 12(0000 1100) = %d\n", a ^ b);

16

YP int csize = sizeof(char); printf("char is made up of %d byte\n", csize); // a = 5(0000 0101) char a = 5; // b = 9(0000 1001) char b = 9; printf("a&b should be 1(0000 0001) = %d\n", a & b); printf("a|b should be 13(0000 1101) = %d\n", a | b); printf("a^b should be 12(0000 1100) = %d\n", a ^ b);

17

YP Unix

● What do all of these have in common? ● AWS, Raspberry Pi, Smart TV, Kindle, Alexa, ...

https://github.com/pisanorg/w/wiki/css-linux-lab

https://github.com/pisanorg/w/wiki/unix

18

YP