├── .gitignore ├── README.md ├── Week_01 ├── README.md └── figures │ ├── 1569345823541.png │ ├── 1569348346330.png │ ├── 1569348386213.png │ ├── 1569348433566.png │ ├── 1569348477633.png │ ├── 1569348487515.png │ ├── 1569348513992.png │ ├── 1569348537826.png │ ├── 1569348574235.png │ ├── 1569348604330.png │ └── 1569351507071.png ├── Week_02 ├── Q1_triple_sum.c ├── Q2_rectangle.c ├── Q3_circle.c ├── Q4_average.c ├── Q5_odd_enough.c ├── README.md └── figures │ └── 1569961731136.png ├── Week_03 ├── Q1_time_flies.c ├── Q2_number_letter.c ├── Q3_bigger_better.c ├── Q4_characteristic_numbers.c ├── README.md └── kahoot_winners.png ├── Week_04 ├── Conditional_Statements_Margaret_Reid-Miller.pdf ├── Q1_whats_next.c ├── Q2_smol.c ├── Q3_positivity.c ├── Q4_divisible.c ├── Q5_what_am_i.c ├── Q6_calculator.c ├── Q6_calculator_switch.c └── README.md ├── Week_05 ├── Q1_chars_from_hell.c ├── Q2_staaaars.c ├── Q3_numbeeeers.c ├── Q4_hooping_numbers.c ├── Q5_even_numbeeeers.c ├── Q6_boom_boom_pow.c └── README.md ├── Week_06 ├── Q1_digiiiits.c ├── Q2_pma.c ├── Q3_noBig.c ├── Q4_rectangle.c ├── Q5_trihard.c ├── Q6_prime.c └── README.md ├── Week_07 ├── Q1_pyramid.c ├── Q2_minimum.c ├── Q3_word_up.c ├── Q4_reverse.c ├── Q5_perfect.c ├── Q6_prime_one.c ├── Q7_prime_two.c ├── Q8_binary.c ├── Q9_fibonacci.c └── README.md ├── Week_08 ├── Q1_alter.c ├── Q2_divide_remain.c ├── Q3_powered_sums.c ├── Q4_complex_power.c ├── README.md └── figures │ ├── W08-ptr1.png │ ├── W08-ptr2.png │ ├── W08-ptr3.png │ ├── W08-swap_ptr1.png │ ├── W08-swap_ptr2.png │ ├── W08-swap_ptr3.png │ ├── W08-swap_ptr4.png │ ├── W08-swap_ptr5.png │ ├── W08-swap_val1.png │ ├── W08-swap_val2.png │ ├── W08-swap_val3.png │ ├── W08-swap_val4.png │ ├── W08-swap_val5.png │ └── q_complex.png ├── Week_09 ├── Q1_large.c ├── Q2_statistics.c ├── Q3_encryption.c ├── Q4_reverse.c ├── Q5_lets_count.c ├── Q6_inception.c ├── Q7_pairs.c ├── Q8_fix34.c ├── Q9_bubble_sort.c ├── README.md └── figures │ ├── array_fig1.png │ ├── array_fig2.png │ ├── array_fig3.png │ ├── array_fig4.png │ ├── array_fig5.png │ ├── array_fig6.png │ └── variance.png ├── Week_10 ├── Q1_pairs.c ├── Q2_fix34.c ├── Q3_bubble_sort.c ├── Q4_transpose.c ├── Q5_2d_functions.c ├── Q6_matrix_add.c ├── Q7_diagonal.c ├── README.md └── figures │ ├── W11_01.png │ └── W11_02.png ├── Week_11 ├── Q1_rook_pawns.c ├── Q2_mystrcmp.c ├── Q3_getmeout.c ├── Q4_find_me.c ├── README.md └── figures │ └── array_pointer.png ├── Week_12 ├── Q1_put_my_words.c ├── Q2_sorted_sentence.c ├── Q3_freq_chars.c ├── Q4_largerst_sub_array.c ├── Q5_shift_array.c ├── Q6_shift_matrix.c ├── Q7_interstellar.c └── README.md └── Week_13 ├── Q1_distance.c ├── Q2_extreme_points.c ├── Q3_moving_rectangles.c ├── Q4_letter_grades.c ├── Q5_flights.c ├── Q6_book_prices.c ├── Q7_sbfs.c └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CmpE 150 2 | 3 | Bogazici University CmpE 150 Introduction to Computing (C programming language) Lab Notes 4 | 5 | **Email:** burak.suyunu@boun.edu.tr 6 | 7 | ### Notes From Other Labs 8 | 9 | * https://github.com/gokceuludogan/cmpe150 10 | * https://github.com/melsener/cmpe150 11 | * https://github.com/zeynepyirmibes/cmpe150-spring20 12 | * https://github.com/sb-b/cmpe150-spring2020 13 | * https://github.com/hkmztrk/CMPE150 14 | 15 | ## Weeks 16 | 17 | ### [Week 1](Week_01/) 18 | 19 | * Introduction 20 | * printf(), scanf() 21 | 22 | #### Star Items 23 | 24 | - Indentation 25 | - Read Errors and Warnings 26 | 27 | ### [Week 2](Week_02/) 28 | 29 | * Data types 30 | * int, float 31 | 32 | #### Star Items 33 | 34 | - Always use the correct format specifier (%d %f ..) 35 | - Type Casting 36 | 37 | ### [Week 3](Week_03/) 38 | 39 | * Operators 40 | * char 41 | 42 | #### Star Items 43 | 44 | - Zero => False, Nonzero => True 45 | - Letters and digits are consecutive in ASCII table 46 | 47 | ### [Week 4](Week_04/) 48 | 49 | * Conditional Statements 50 | 51 | #### Star Items 52 | 53 | - Indentation saves lives (and codes) 54 | - No boolean expression after else 55 | 56 | ### [Week 5](Week_05/) 57 | 58 | * Loops 59 | 60 | ### [Week 6](Week_06/) 61 | 62 | * Loops 63 | * Nested Loops 64 | 65 | ### [Week 7](Week_07/) 66 | 67 | * Functions 68 | 69 | ### [Week 8](Week_08/) 70 | 71 | * Functions - Call by Reference 72 | * Pointers 73 | 74 | ### [Week 9](Week_09/) 75 | 76 | * Arrays 77 | 78 | ### [Week 10](Week_10/) 79 | 80 | * Arrays to Functions 81 | * Multidimensional Arrays 82 | 83 | ### [Week 11](Week_11/) 84 | 85 | * Strings 86 | 87 | ### [Week 12](Week_12/) 88 | 89 | * Practice (Array - 2D Array - String) 90 | 91 | ### [Week 13](Week_13/) 92 | 93 | * Structures -------------------------------------------------------------------------------- /Week_01/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to CMPE150 Lab Session! -- Week 1 2 | 3 | ## Programming Languages 4 | 5 | ![Image result for popular programming languages](https://www.ubuntupit.com/wp-content/uploads/2018/11/Most-Popular-Programming-Languages.jpg) 6 | 7 | Figure from: https://www.ubuntupit.com/wp-content/uploads/2018/11/Most-Popular-Programming-Languages.jpg 8 | 9 | ![1569351507071](figures/1569351507071.png) 10 | 11 | 12 | 13 | ## Installation 14 | 15 | The installation consists of four stages. The steps you should follow for these stages are OS dependent (Windows, Linux, MacOS). 16 | 17 | * **Stage #0:** https://programming.cmpe.boun.edu.tr/ 18 | * **Stage #1:** *Install Java 8u181 JDK* 19 | * **Stage #2:** *Installing the C Compiler* 20 | * **Stage #3:** *Download and Install the ZIP File* 21 | * **Stage #4:** *Run Eclipse and Set Your Workspace* 22 | 23 | 24 | 25 | ## Eclipse IDE 26 | 27 | ### What's an IDE? 28 | 29 | An integrated development environment (**IDE**) is a software application that provides comprehensive facilities to computer programmers for software development. An **IDE** normally consists of at least a source code editor, build automation tools, and a debugger. (Definition from https://en.wikipedia.org/wiki/Integrated_development_environment) 30 | 31 | ### Eclipse is an open source integrated development environment. 32 | 33 | It provides tools for *writing code, compiling and building the program and debugging.* 34 | 35 | 36 | 37 | ## User Interface 38 | 39 | ![1569348487515](figures/1569348487515.png) 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | * Project Explorer 48 | 49 | ![1569348386213](figures/1569348386213.png) 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | * Code Editor 60 | 61 | ![1569348513992](figures/1569348513992.png) 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | * Teaching Codes Plug-in 70 | 71 | ![1569348537826](figures/1569348537826.png) 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | * Console 80 | 81 | ![1569348574235](figures/1569348574235.png) 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | * Toolbar 90 | 91 | ![1569348604330](figures/1569348604330.png) 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | **If you closed any of these views accidentally, you can open them following *Window>Show View> (View Name) (i.e. Console, Project Explorer, Other for Teaching Codes)* ** 100 | 101 | 102 | 103 | ### Creating a New Project and Running 104 | 105 | * Click **New** Under **File**. 106 | * Choose **C/C++ Project**. 107 | * Choose **C Managed Builds**. 108 | * Give your project a *name* and click **Finish**. 109 | * To build and run your project for the first time, you should right click on the project on *Project Explorer*. 110 | * Click **Build Project** from the options. 111 | * After you see *Build Finished* message on the *Console*, right click on the project again. 112 | * Click **Run as Local C/C++ Application**. 113 | * After first building and running, you could use the **build** and **run** buttons on the *Toolbar*. 114 | 115 | ## Main Function 116 | 117 | * This function is "called" by the operating system when the program runs. Thus it is the first code executed when a program starts. 118 | * Statements are executed in sequential order. 119 | 120 | * **return 0;** terminates the main() function and returns the value 0 meaning that the program runned successfully. 121 | 122 | ## Comments 123 | 124 | * Multiline comments: /* */ 125 | 126 | * Single line comments: // 127 | 128 | ## Code Block 129 | 130 | * The set of statements between a pair of braces is called a *block*. 131 | 132 | * Each statement must end with a semicolon (;) 133 | 134 | ## Variables 135 | 136 | * Storing value for later use 137 | * Variable names: only letters (English), digits, and underscore ( _ ). cannot start with a digit. Case sensitive. 138 | 139 | ## Declarations 140 | 141 | * In C, every variable needs to be *declared* to be a specific type before use. (i.e. int sum;) 142 | * Several variables of the same type can be declared in a single line. Names separated with comma. (int num1, num2, num3;) 143 | 144 | ## Assignments (=) 145 | 146 | * Stores a value at the memory location reserved for the variable. **Not mathematical equality.** 147 | 148 | ## Operations 149 | 150 | * Sum, Subtraction, Multiplication, Division, Modulus. 151 | 152 | ## Conventions 153 | 154 | * Write one statement per line 155 | * Indent the code inside a block (press Tab). 156 | * Blocks inside blocks should be further indented. 157 | * Use spaces on both sides of operators. 158 | * Use a lot of comments. 159 | * Use meaningful variable names. 160 | 161 | ## Output 162 | 163 | * The #include directive copies the contents of a “header file” into source code. 164 | * stdio.h: The header file for the standard input-output library. 165 | * Required for the printf() function. 166 | * #include 167 | 168 | ### printf() 169 | 170 | * printf(“Result= %d\n”, number_3); 171 | * Takes a “format string”, and any number of expressions. Expression: Anything that returns a value. 172 | * %d: “Format specifier”, used for int. 173 | * \n: Newline character (one of the “escape” characters) 174 | * printf(“%d + %d = %d\n”, number_1, number_2, number_3);** 175 | 176 | 177 | --- 178 | page credit to https://github.com/gokceuludogan/cmpe150-fall19 179 | 180 | 181 | --- 182 | 183 | ## Examples 184 | 185 | ### Example 1 - Hello World 186 | 187 |
188 | Code 1 189 | 190 |
191 | 192 | ```c 193 | #include 194 | 195 | int main() 196 | { 197 | printf("Hello World"); 198 | 199 | return 0; 200 | } 201 | ``` 202 |
203 | 204 |
205 | 206 | ### Example 2 - Let's TryAngle 207 | 208 |
209 | Code 2 210 | 211 |
212 | 213 | ```c 214 | #include 215 | 216 | int main() 217 | { 218 | printf("*\n"); 219 | printf("**\n"); 220 | printf("***\n"); 221 | printf("****\n"); 222 | 223 | return 0; 224 | } 225 | ``` 226 | 227 |
228 | 229 |
230 | 231 | ### Example 3 - Numbers and Characters 232 | 233 |
234 | Code 3 235 | 236 |
237 | 238 | ```c 239 | #include 240 | 241 | int main() 242 | { 243 | printf("2 + 3 = %d", 2 + 3); 244 | 245 | return 0; 246 | } 247 | ``` 248 | 249 |
250 | 251 |
252 | 253 | ### Example 4 - Hold Me Variable 254 | 255 |
256 | Code 4 257 | 258 |
259 | 260 | ```c 261 | #include 262 | 263 | int main() 264 | { 265 | int number; 266 | 267 | number = 10; 268 | 269 | printf("number = %d", number); 270 | 271 | return 0; 272 | } 273 | ``` 274 | 275 |
276 | 277 | 278 |
279 | 280 | ### Example 5 - Read Me Variable 281 | 282 |
283 | Code 5 284 | 285 |
286 | 287 | ```c 288 | #include 289 | 290 | int main() 291 | { 292 | int number; 293 | 294 | scanf("%d", &number); 295 | 296 | printf("number = %d", number); 297 | 298 | return 0; 299 | } 300 | ``` 301 | 302 |
303 | 304 |
305 | 306 | ### Example 6 - Calculate Us Variables 307 | 308 |
309 | Code 6 310 | 311 |
312 | 313 | ```c 314 | #include 315 | 316 | int main() 317 | { 318 | int number1, number2; 319 | 320 | scanf("%d %d", &number1, &number2); 321 | 322 | printf("%d %d %d", number1+number2, number1*number2, number1/number2); 323 | 324 | return 0; 325 | } 326 | 327 | ``` 328 | 329 |
-------------------------------------------------------------------------------- /Week_01/figures/1569345823541.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569345823541.png -------------------------------------------------------------------------------- /Week_01/figures/1569348346330.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348346330.png -------------------------------------------------------------------------------- /Week_01/figures/1569348386213.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348386213.png -------------------------------------------------------------------------------- /Week_01/figures/1569348433566.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348433566.png -------------------------------------------------------------------------------- /Week_01/figures/1569348477633.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348477633.png -------------------------------------------------------------------------------- /Week_01/figures/1569348487515.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348487515.png -------------------------------------------------------------------------------- /Week_01/figures/1569348513992.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348513992.png -------------------------------------------------------------------------------- /Week_01/figures/1569348537826.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348537826.png -------------------------------------------------------------------------------- /Week_01/figures/1569348574235.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348574235.png -------------------------------------------------------------------------------- /Week_01/figures/1569348604330.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569348604330.png -------------------------------------------------------------------------------- /Week_01/figures/1569351507071.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_01/figures/1569351507071.png -------------------------------------------------------------------------------- /Week_02/Q1_triple_sum.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Q1 - Rectangle 3 | */ 4 | 5 | #include 6 | 7 | int main() { 8 | int n1, n2, n3; 9 | scanf("%d %d %d", &n1, &n2, &n3); 10 | printf("%d", n1+n2+n3); 11 | 12 | return 0; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /Week_02/Q2_rectangle.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Q1 - Rectangle 3 | */ 4 | 5 | #include 6 | 7 | int main() { 8 | int r1, r2; 9 | int perimeter, area; 10 | 11 | scanf("%d %d", &r1, &r2); 12 | 13 | perimeter = 2 * (r1+r2); 14 | area = r1*r2; 15 | 16 | printf("%d %d", perimeter, area); 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Week_02/Q3_circle.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Q2 - Circle 3 | */ 4 | 5 | #include 6 | 7 | int main() { 8 | float r; 9 | scanf("%f", &r); 10 | 11 | printf("%f %f", 2*3.14*r, 3.14*r*r); 12 | 13 | return 0; 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Week_02/Q4_average.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Q3 - Average 3 | */ 4 | 5 | #include 6 | 7 | int main() { 8 | int n1, n2, n3; 9 | scanf("%d %d %d", &n1, &n2, &n3); 10 | 11 | float average; 12 | average = (n1+n2+n3)/3.0; 13 | 14 | printf("%.2f", average); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Week_02/Q5_odd_enough.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int a, b, c; 5 | scanf("%d %d %d", &a, &b, &c); 6 | printf("%d", a*(a%2)+b*(b%2)+c*(c%2)); 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /Week_02/README.md: -------------------------------------------------------------------------------- 1 | # Week 2 Notes 2 | 3 | 4 | ## Star Items of the Last Week 5 |
6 | Star Item 1 7 | *** Indentation *** 8 |
9 | 10 |
11 | Star Item 2 12 | *** Read Errors and Warnings *** 13 |
14 | 15 | --- 16 | 17 | ## Last Week Refresher 18 | 19 | 20 | ### Output 21 | 22 | ```c 23 | #include 24 | int main() 25 | { 26 | printf("Hello world!"); 27 | return 0; 28 | } 29 | ``` 30 | 31 |
32 | 33 | ### Integer variables 34 | 35 | ```c 36 | #include 37 | int main() 38 | { 39 | int number_1, number_2, sum; 40 | number_1 = 3; 41 | number_2 = 5; 42 | sum = number_1 + number_2; 43 | printf("Number1: %d Number2: %d Sum: %d \n", number_1, number_2, sum); 44 | return 0; 45 | } 46 | ``` 47 | 48 |
49 | 50 | ### Variable names 51 | 52 | * It can contain alphanumeric characters and underscore(_) 53 | * It cannot start with number. 54 | * Case-sensitive. 55 | 56 | 57 |
58 | 59 | ### Getting Input: Scanf() 60 | 61 | * Format: ```scanf("%d", &number);``` *the variable **number** must be declared before.* 62 | * Requires *stdio.h*: ```#include ``` 63 | 64 | ```c 65 | #include 66 | int main(){ 67 | // Declare variables 68 | int number_1, number_2, product; 69 | 70 | //Read first value 71 | scanf("%d", &number_1); 72 | 73 | //Read second value 74 | scanf("%d", &number_2); 75 | product = number_1 * number_2; 76 | 77 | // Display output 78 | printf("The product of %d and %d is %d\n", number_1, number_2, product); 79 | return 0; 80 | } 81 | ``` 82 | 83 |
84 | 85 | --- 86 | 87 | 88 | ## Warm-Up 89 | 90 | ### Question 1 - Triple Sum 91 | 92 | Write a program that takes 3 integers from the user and then prints the sum of the numbers. 93 | 94 | | INPUT | OUTPUT | 95 | |:-------:|:-------:| 96 | | 3 4 5 | 12 | 97 | | 2 2 4 | 8 | 98 | | 1 9 15 | 25 | 99 | 100 |
101 | 102 | --- 103 | 104 | 105 | 106 | 107 | # Data Types 108 | 109 | ### Bit vs Byte 110 | 111 | * **Bit:** the smallest unit, *0 or 1*. 112 | * **Byte:** a collection of bits: *8 bits.* *(i.e. 01010111)* 113 | 114 |
115 | 116 | ### Fundamental Data Types 117 | 118 | ![img](https://qph.fs.quoracdn.net/main-qimg-7852cd4b11648b9ec8029a5df338bc2b) 119 | 120 | 121 | 122 | | Type | Size (bytes) | Format Specifier | 123 | | :----------------------- | :---------------------------- | :--------------: | 124 | | `int` | at least 2, usually 4 | `%d` | 125 | | `char` | 1 | `%c` | 126 | | `float` | 4 | `%f` | 127 | | `double` | 8 | `%lf` | 128 | | `short int` | 2 usually | `%hd` | 129 | | `unsigned int` | at least 2, usually 4 | `%u` | 130 | | `long int` | at least 4, usually 8 | `%li` | 131 | | `long long int` | at least 8 | `%lli` | 132 | | `unsigned long int` | at least 4 | `%lu` | 133 | | `unsigned long long int` | at least 8 | `%llu` | 134 | | `signed char` | 1 | `%c` | 135 | | `unsigned char` | 1 | `%c` | 136 | | `long double` | at least 10, usually 12 or 16 | `%Lf` | 137 | 138 | ### Integer Representation 139 | 140 | Image result for two's complement 141 | 142 |
143 | 144 | 145 | #### Two's Complement 146 | 147 | * Leftmost bit represents the sign (0 for positive, 1 for negative). 148 | * If positive, value is the numeric value of the remaining binary pattern. 149 | * If negative, value is the numeric value of the complement(\*) of the remaining binary pattern, plus one. (*) complement: Switch 0's to 1's, and vice versa. 150 | * Example: 0 100 0001 151 | * Sign bit is 0 ⇒ positive. 152 | * Absolute value is 100 0001B = 65D (D: Decimal; B: Binary) 153 | * The represented value is +65D 154 | * Example: 1 000 0001 155 | * Sign bit is 1 ⇒ negative 156 | * Absolute value is the complement of 000 0001B plus 1, i.e., 111 1110B + 1B = 127D 157 | * Hence, the represented value is -127D 158 | 159 | ------ 160 | 161 | * If an integer type is stored in N bits 162 | * Signed range: -2^(N-1) and 2^(N-1) -1 163 | * Unsigned range: 0 to 2^N - 1. 164 | * Example: int: 4 bytes = 32 bits 165 | * int: - 2^(31) and 2^(31) -1. 166 | * unsigned int: 0 and 2^(32) - 1. 167 | * The sizeof() function returns the number of bytes occupied by each type. Returns *long*, so the identifier: *%lu* 168 | 169 |
170 | 171 | #### Overflow: Outside of the range. 172 | 173 | ```c 174 | #include 175 | int main() 176 | { 177 | // the sizes of data types 178 | printf("size of int = %lu\n", sizeof(int)); 179 | printf("size of long int = %lu\n", sizeof(long)); 180 | printf("size of unsigned int = %lu\n", sizeof(unsigned int)); 181 | printf("size of char = %lu\n", sizeof(char)); 182 | int a; // declaration 183 | a = 2147483647; // assignment of largest int 184 | printf("%d\n", a+1); 185 | return 0; 186 | } 187 | ``` 188 | 189 |
190 | 191 | ### Floating Point Representation 192 | 193 | Stores nonintegers. 194 | 195 | | Type | Storage size | Value range | Precision | 196 | | :---------: | :----------: | :--------------------: | :---------------: | 197 | | float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places | 198 | | double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places | 199 | | long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places | 200 | 201 |
202 | 203 | --- 204 | 205 | # Questions 206 | 207 | ## Q2 - Rectangle 208 | 209 | Write a program that reads two integers as the side lengths of a rectangle. Then outputs the perimeter and the area of the rectangle. 210 | 211 | | INPUT | OUTPUT | 212 | |:-------:|:-------:| 213 | | 3 5 | 16 15 | 214 | 215 |
216 | 217 | ## Q3 - Circle 218 | 219 | Write a program that reads a float as the radius of a circle. Then outputs the circumference and the area of the circle. (pi = 3.14) 220 | 221 | | INPUT | OUTPUT | 222 | |:-------:|:-------:| 223 | | 4.5 | 28.260 63.585 | 224 | 225 |
226 | 227 | ## Q4 - Average 228 | 229 | Write a program that reads three integers from the user. Then outputs the average of these three numbers. (Hint: Type Casting) 230 | 231 | | INPUT | OUTPUT | 232 | |:-------:|:-------:| 233 | | 2 3 5 | 3.33 | 234 | 235 |
236 | 237 | ## Question 5 - Odd Enough 238 | 239 | Write a program that takes 3 integer from the user and then pronts the sum of odd numbers. 240 | 241 | | INPUT | OUTPUT | 242 | |:-------:|:-------:| 243 | | 3 4 5 | 8 | 244 | | 2 2 4 | 0 | 245 | | 1 9 15 | 25 | 246 | 247 | -------------------------------------------------------------------------------- /Week_02/figures/1569961731136.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_02/figures/1569961731136.png -------------------------------------------------------------------------------- /Week_03/Q1_time_flies.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int total_seconds; 5 | scanf("%d", &total_seconds); 6 | 7 | int hours = total_seconds/3600; 8 | int minutes = (total_seconds - hours*3600)/60; 9 | int seconds = (total_seconds - hours*3600 - minutes*60); 10 | 11 | printf("%d %d %d", hours, minutes, seconds); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Week_03/Q2_number_letter.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char ch; 5 | scanf("%c", &ch); 6 | 7 | printf("%d", ch-'A'+1); 8 | 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /Week_03/Q3_bigger_better.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char ch; 5 | scanf("%c", &ch); 6 | 7 | printf("%c", ch+('A'-'a')); 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Week_03/Q4_characteristic_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char a, b; 5 | scanf("%c %c", &a, &b); 6 | 7 | int result; 8 | 9 | result = (a-'0') * (b-'0'); 10 | 11 | printf("%d", result); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Week_03/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Week 3 Notes 3 | 4 | ## Last Week Refresher 5 | 6 | ### Bit vs Byte 7 | 8 | * **Bit:** the smallest unit, *0 or 1*. 9 | * **Byte:** a collection of bits: *8 bits.* *(i.e. 01010111)* 10 | 11 | 12 | ### Fundamental Data Types 13 | 14 | ![img](https://qph.fs.quoracdn.net/main-qimg-7852cd4b11648b9ec8029a5df338bc2b) 15 | 16 | 17 | 18 | | Type | Size (bytes) | Format Specifier | 19 | | :----------------------- | :---------------------------- | :--------------: | 20 | | `int` | at least 2, usually 4 | `%d` | 21 | | `char` | 1 | `%c` | 22 | | `float` | 4 | `%f` | 23 | | `double` | 8 | `%lf` | 24 | | `short int` | 2 usually | `%hd` | 25 | | `unsigned int` | at least 2, usually 4 | `%u` | 26 | | `long int` | at least 4, usually 8 | `%li` | 27 | | `long long int` | at least 8 | `%lli` | 28 | | `unsigned long int` | at least 4 | `%lu` | 29 | | `unsigned long long int` | at least 8 | `%llu` | 30 | | `signed char` | 1 | `%c` | 31 | | `unsigned char` | 1 | `%c` | 32 | | `long double` | at least 10, usually 12 or 16 | `%Lf` | 33 | 34 | ## Star Items of the Last Week 35 |
36 | Star Item 1 37 | *** Always use the correct format specifier (%d %f ..)*** 38 |
39 | 40 |
41 | Star Item 2 42 | *** Type Casting *** 43 |
44 | 45 | 46 | ## Warm-Up 47 | 48 | ### Question 1 - Time Flies 49 | 50 | Write a program to convert a given integer (in seconds) to hours, minutes and seconds. 51 | 52 | | INPUT | OUTPUT | 53 | |:-------:|:-------:| 54 | | 25300 | 7 1 40 | 55 | | 18000 | 5 0 0 | 56 | | 155 | 0 2 35 | 57 | 58 |
59 | 60 | --- 61 | 62 | ### Constant 63 | 64 | ```c 65 | #define constant_name constant_value 66 | ``` 67 | 68 | ## Operators 69 | 70 | ### Assignment Operators (=, +=, -=, *=, /=) 71 | 72 | * \ = \ 73 | * Assign the value of the expression to the variable 74 | * Right to left associative: 75 | * ```a = b = c = 10;``` ``` a = (b = (c = 10));``` 76 | * Initialization: 77 | * int a = 3, b = 4; 78 | float x = 2.75; 79 | * Compound assignment operator: 80 | * a = a + b; 81 | a += b; 82 | 83 | 84 | #### Compound assignment (+=, -=, *=, /=) 85 | 86 | ```c 87 | #include 88 | 89 | int main(){ 90 | int x=5, y=3; 91 | x +=y /* same as x = x + y */ 92 | printf("x:%d\n",x); 93 | 94 | y *= x /* same as y = x * y */ 95 | printf("y:%d x:%d\n",y,x); 96 | 97 | return 0; 98 | } 99 | ``` 100 | 101 |
102 | 103 | #### Type Conversion 104 | 105 | * Narrow to broader (i.e. int to float, 3 => 3.0): no loss of information. 106 | 107 | * Broad to narrower (i.e. float to int, 3.5 => 3): lost info. 108 | 109 |
110 | 111 | ### Arithmetic Operators (+, -, *, /, %) 112 | 113 | * The results of an operator expression is of the "wider" type (int < float < double) 114 | 115 | * Integer division vs Float division 116 | 117 | * Float division of integers: Multiply with 1.0 or cast into float. 118 | 119 | * Left to right associative: ```a/b/c``` <=>```(a/b)/c``` 120 | 121 | * Variable updates: 122 | 123 | * x = x +1, 124 | 125 | * ++, --: pre/post 126 | 127 | #### Increment-Decrement (++, --) 128 | 129 | ```c 130 | #include 131 | 132 | int main(){ 133 | int n = 5, x; 134 | n++; /* n = 6 */ 135 | printf("n:%d\n",n); 136 | // n = n+1 OR n += 1 OR n++ 137 | 138 | x=n++; 139 | printf("x:%d n:%d\n",x,n); 140 | /* equals to following two commands: 141 | * x = n; 142 | * n++; 143 | */ 144 | 145 | x=n--; 146 | printf("x:%d n:%d\n",x,n); 147 | /* equals to following two commands: 148 | * x = n; 149 | * n--; 150 | */ 151 | 152 | x=++n; 153 | printf("x:%d n:%d\n",x,n); 154 | /* equals to following two commands: 155 | * n++; 156 | * x = n; 157 | */ 158 | 159 | x=--n; 160 | printf("x:%d n:%d\n",x,n); 161 | /* equals to following two commands: 162 | * n--; 163 | * x = n; 164 | */ 165 | 166 | 167 | return 0; 168 | } 169 | ``` 170 | 171 |
172 | 173 | 174 | ### Logical Operators 175 | 176 | * Boolean operations (true vs false) 177 | * Zero => False 178 | * Nonzero => True 179 | * Operators: 180 | * "&&" logical-AND 181 | * "||" logical-OR 182 | * "!" negation 183 | 184 | ![](https://camo.githubusercontent.com/8f75228b5905e61ee55f141a940b3bb8a836d15a/68747470733a2f2f76696367726f75742e66696c65732e776f726470726573732e636f6d2f323031342f30372f74727574687461626c65732e676966) 185 | 186 | #### Short Circuit 187 | 188 | ##### OR 189 | 190 | ```c 191 | #include 192 | int main() 193 | { 194 | int a = 2, b = 5; 195 | printf("%d %d %d\n", a++ || ++b, a, b); 196 | printf("%d %d", a, b); 197 | 198 | return 0; 199 | } 200 | ``` 201 |
202 | 203 | ##### AND 204 | 205 | ```c 206 | #include 207 | int main() 208 | { 209 | int a = -1, b = 5; 210 | printf("%d %d %d\n", ++a && ++b, a, b); 211 | printf("%d %d", a, b); 212 | 213 | return 0; 214 | } 215 | ``` 216 | 217 |
218 | 219 | ### Relational Operators 220 | 221 | * Value comparisons: 0 if False, otherwise True 222 | 223 | | **Operators** | **Example/Description** | 224 | | ------------- | ---------------------------------------- | 225 | | > | x > y (x is greater than y) | 226 | | < | x < y (x is less than y) | 227 | | >= | x >= y (x is greater than or equal to y) | 228 | | <= | x <= y (x is less than or equal to y) | 229 | | == | x == y (x is equal to y) | 230 | | != | x != y (x is not equal to y) | 231 | 232 |
233 | 234 | ### Expressions and precedence 235 | 236 | - Highest precedence: Parentheses 237 | - *, /, % are evaluated before +, - 238 | - **Arithmetic Operators > Comparison Operators > Logical Operators > Assignment Operators** 239 | - Associativity is used when there are two or more operators of same precedence 240 | 241 | 242 | 243 |
244 | 245 | --- 246 | 247 | ## Char 248 | 249 | - Represent characters (letters, digits, signs) 250 | 251 | - 1 byte = 8 bits = 256 values. (-128 to 127) 252 | 253 | - **Encoding:** Mapping characters to integer values 254 | 255 | - ASCII table: maps 127 characters. 256 | 257 | ![](http://www.asciitable.com/index/asciifull.gif) 258 | 259 | ## Only Thing You Should Remember About ASCII Table: Letters and digits are consecutive 260 | 261 | - Variable stores corresponding integer value of character. The translation is done by i/o functions. 262 | 263 | | Syntax | Meaning | 264 | | ------ | ------------- | 265 | | a | variable name | 266 | | 'a' | character | 267 | | "a" | string | 268 | 269 | 270 | ## Char is special!! It is a character and an integer at the same time! 271 | 272 | 273 |
274 | 275 | --- 276 | 277 | # Questions 278 | 279 | ### Question 2 - Number the Letter 280 | 281 | Write a program which reads an upper-case char (character) from the user, then prints its rank in the alphabet. 282 | 283 | | INPUT | OUTPUT | 284 | |:-------:|:-------:| 285 | | D | 4 | 286 | | G | 7 | 287 | 288 |
289 | 290 | ### Question 3 - Bigger is Better (Lab2 Q2) 291 | Write a program which reads a lower-case char (character) from the user, then prints its upper-case version on the screen. 292 | 293 | *Hint: Note that in the ASCII table, the distance between a lower-case letter and its corresponding upper-case version is the same for all letters.* 294 | 295 | | INPUT | OUTPUT | 296 | |:-------:|:-------:| 297 | | d | D | 298 | | t | T | 299 | 300 |
301 | 302 | ### Question 4 - Characteristic Numbers 303 | 304 | Write a program that reads two numeric **characters** and prints their multiplication. 305 | 306 | | INPUT | OUTPUT | 307 | | --------- |:---------:| 308 | | 9 8 | 72| 309 | | 1 3 | 3| 310 | 311 | 312 | ```c 313 | #include 314 | 315 | int main(){ 316 | 317 | char ch1, ch2; 318 | scanf("%c %c", &ch1, &ch2); 319 | 320 | /* 321 | * YOUR SOLUTION HERE 322 | * 323 | */ 324 | 325 | return 0; 326 | } 327 | ``` 328 | -------------------------------------------------------------------------------- /Week_03/kahoot_winners.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_03/kahoot_winners.png -------------------------------------------------------------------------------- /Week_04/Conditional_Statements_Margaret_Reid-Miller.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_04/Conditional_Statements_Margaret_Reid-Miller.pdf -------------------------------------------------------------------------------- /Week_04/Q1_whats_next.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | 7 | scanf("%c", &ch); 8 | 9 | printf("%c", ch+3); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Week_04/Q2_smol.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | 7 | scanf("%c", &ch); 8 | 9 | printf("%c", ch+('A'-'a')); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Week_04/Q3_positivity.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int x; 5 | scanf("%d", &x); 6 | 7 | if(x > 0){ 8 | printf("positive\n"); 9 | if(x % 2 == 0){ 10 | printf("even\n"); 11 | } 12 | else{ 13 | printf("odd\n"); 14 | } 15 | } 16 | else if(x < 0){ 17 | printf("negative\n"); 18 | printf("%d\n", -x); 19 | } 20 | else{ 21 | printf("zero"); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Week_04/Q4_divisible.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | 7 | if(a <= 0 || b <= 0){ 8 | printf("Incorrect Input\n"); 9 | } 10 | else{ 11 | if(a % b == 0){ 12 | printf("it is divisible\n"); 13 | } 14 | else{ 15 | printf("it is not divisible\n"); 16 | } 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Week_04/Q5_what_am_i.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | 7 | scanf("%c", &ch); 8 | 9 | if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) 10 | printf("Lettuce"); 11 | else if(ch >= '0' && ch <= '9') 12 | printf("Durian"); 13 | else 14 | printf("Nani!?"); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Week_04/Q6_calculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | float x, y; 5 | char c; 6 | 7 | scanf("%f %c %f", &x, &c, &y); 8 | 9 | if(c == '+'){ 10 | printf("%.2f", x+y); 11 | } 12 | else if(c == '-'){ 13 | printf("%.2f", x-y); 14 | } 15 | else if(c == '*'){ 16 | printf("%.2f", x*y); 17 | } 18 | else if(c == '/'){ 19 | printf("%.2f", x/y); 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Week_04/Q6_calculator_switch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | float x, y; 5 | char c; 6 | 7 | scanf("%f %c %f", &x, &c, &y); 8 | 9 | switch(c){ 10 | case '+': 11 | printf("%.2f\n", x+y); 12 | break; 13 | case '-': 14 | printf("%.2f\n", x-y); 15 | break; 16 | case '*': 17 | printf("%.2f\n", x*y); 18 | break; 19 | case '/': 20 | printf("%.2f\n", x/y); 21 | break; 22 | default: 23 | printf("Wrong Operator\n"); 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Week_04/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Week 4 Notes 3 | 4 | --- 5 | 6 | ### QUIZ: 15:30 - 15:50 7 | 8 | --- 9 | 10 | ## Last Week Refresher 11 | 12 | ## Star Items of the Last Week 13 |
14 | Star Item 1 15 | *** Zero => False, Nonzero => True *** 16 |
17 | 18 |
19 | Star Item 2 20 | *** Letters and digits are consecutive in ASCII table *** 21 |
22 | 23 | ### Operators 24 | 25 | * Assignment Operators (=, +=, -=, \*=, /=) 26 | * Arithmetic Operators (+, -, \*, /, %) 27 | * Increment-Decrement (++, --) 28 | * Logical Operators (&&, ||, !) 29 | * Zero => False 30 | * Nonzero => True 31 | * Relational Operators (>, <, >=, <=, ==, !=) 32 | * Precedence: Parentheses > Arithmetic Operators > Comparison Operators > Logical Operators > Assignment Operators 33 | 34 | ### Char 35 | 36 | - Represent characters (letters, digits, signs) 37 | 38 | - 1 byte = 8 bits = 256 values. 39 | 40 | - **Encoding:** Mapping characters to integer values 41 | 42 | - ASCII table: maps 256 characters. 43 | 44 | ![](http://www.asciitable.com/index/asciifull.gif) 45 | 46 | ### Only Thing You Should Remember About ASCII Table: Letters and digits are consecutive 47 | 48 | - Variable stores corresponding integer value of character. The translation is done by i/o functions. 49 | 50 | | Syntax | Meaning | 51 | | ------ | ------------- | 52 | | a | variable name | 53 | | 'a' | character | 54 | | "a" | string | 55 | 56 | 57 | ### Char is a character and an integer at the same time! 58 | 59 | --- 60 | 61 | ## Warm-Up 62 | 63 | ### Question 1 - What's Next? 64 | Write a program which reads a character from the user. Print the 3rd character that comes after the input character. 65 | 66 | | INPUT | OUTPUT | 67 | |:-------:|:-------:| 68 | | c | f | 69 | | M | P | 70 | | ! | $ | 71 | 72 | 73 | ### Question 2 - Smol 74 | Write a program which reads a lower-case char (character) from the user, then prints its upper-case version on the screen. 75 | 76 | *Hint: Note that in the ASCII table, the distance between a lower-case letter and its corresponding upper-case version is the same for all letters.* 77 | 78 | | INPUT | OUTPUT | 79 | |:-------:|:-------:| 80 | | d | D | 81 | | t | T | 82 | 83 | 84 |
85 | 86 | 87 | ## Conditional Statements 88 | 89 | The conditional statements **if**, **if-else**, and **switch** allow us to choose which statement will be executed next. 90 | 91 | ### The *if* statement 92 | 93 |
94 | if Example 1 95 |
96 | 97 | ```c 98 | int a = 5; 99 | if(a > 0) 100 | printf("Positive\n"); 101 | printf("a: %d", a); 102 | ``` 103 | 104 |
105 | 106 |
107 | 108 |
109 | if Example 2 110 |
111 | 112 | ```c 113 | int a = 5; 114 | if(a > 0) 115 | printf("Positive\n"); 116 | printf("Not Negative\n"); 117 | printf("a: %d", a); 118 | ``` 119 | 120 |
121 | 122 |
123 | 124 |
125 | if Example 3 126 |
127 | 128 | ```c 129 | int a = 5; 130 | if(a > 0){ 131 | printf("Positive\n"); 132 | printf("Not Negative\n"); 133 | } 134 | printf("a: %d", a); 135 | ``` 136 | 137 |
138 | 139 |
140 | 141 | 142 | ### Switch 143 | 144 |
145 | switch Example 1 146 |
147 | 148 | ```c 149 | #include 150 | 151 | int main() 152 | { 153 | char card = 'D'; 154 | switch (card) { 155 | case 'C': 156 | printf("Clubs\n"); 157 | case 'D': 158 | printf("Diamonds\n"); 159 | case 'H': 160 | printf("Hearts\n"); 161 | case 'S': 162 | printf("Spades\n"); 163 | } 164 | 165 | return 0; 166 | } 167 | ``` 168 | 169 |
170 | 171 |
172 | 173 |
174 | switch Example 2 175 |
176 | 177 | ```c 178 | #include 179 | 180 | int main() 181 | { 182 | char card = 'D'; 183 | switch (card) { 184 | case 'C': 185 | printf("Clubs\n"); 186 | break; 187 | case 'D': 188 | printf("Diamonds\n"); 189 | break; 190 | case 'H': 191 | printf("Hearts\n"); 192 | break; 193 | case 'S': 194 | printf("Spades\n"); 195 | break; 196 | default: 197 | printf("WUT\n"); 198 | } 199 | 200 | return 0; 201 | } 202 | ``` 203 | 204 |
205 | 206 |
207 | 208 |
209 | switch Example 3 210 |
211 | 212 | 213 | ```c 214 | #include 215 | 216 | int main() 217 | { 218 | char day = 'W'; 219 | int d; 220 | switch (day) { 221 | case 'M': 222 | printf("Monday\n"); 223 | d = 1; 224 | break; 225 | case 'T': 226 | printf("Tuesday\n"); 227 | d = 2; 228 | break; 229 | case 'W': 230 | printf("Wednesday\n"); 231 | d = 3; 232 | break; 233 | case 't': 234 | printf("Thursday\n"); 235 | d = 4; 236 | break; 237 | case 'F': 238 | printf("Friday\n"); 239 | d = 5; 240 | break; 241 | case 'S': 242 | printf("Saturday\n"); 243 | d = 6; 244 | break; 245 | case 's': 246 | printf("Sunday\n"); 247 | d = 7; 248 | break; 249 | default: 250 | printf("Noday\n"); 251 | d = 0; 252 | } 253 | printf("%d", d); 254 | 255 | return 0; 256 | } 257 | ``` 258 | 259 |
260 | 261 |
262 | 263 | ### Question 3 - Positivity 264 | 265 | #### Question 3a 266 | Write a program that reads an integer. If the integer is positive then print "positive", if it is not then print "not positive". 267 | 268 | | INPUT | OUTPUT | 269 | |:-------:|:-------:| 270 | | 4 | positive | 271 | | -5 | not positive | 272 | | 0 | not positive | 273 | 274 |
275 | 276 | #### Question 3b 277 | Write a program that reads an integer. If the integer is positive then print "positive", if it is negative then print "negative", if it is zero then print "zero". 278 | 279 | | INPUT | OUTPUT | 280 | |:-------:|:-------:| 281 | | 4 | positive | 282 | | -5 | negative | 283 | | 0 | zero | 284 | 285 |
286 | 287 | #### Question 3c 288 | Write a program that reads an integer. If the integer is positive then check if the number is odd or even and then print "odd" or "even", if it is negative then print the absolute value of the number, if it is zero then print "zero". 289 | 290 | | INPUT | OUTPUT | 291 | |:-------:|:-------:| 292 | | 4 | even | 293 | | -5 | 5 | 294 | | 0 | zero | 295 | 296 | 297 |
298 | 299 | ### Question 4 - Divisible (Lab3 Q5) 300 | 301 | Write a program that will take 2 numbers from the user and if the first number is divisible by the second number, your code should print "it is divisible", if it is not divisible, then print "it is not divisible". If any of your numbers are nonpositive, print "Incorrect Input" 302 | 303 | 304 | | INPUT | OUTPUT | 305 | |:-------:|:-------:| 306 | | 45 5 | it is divisible | 307 | | 45 6 | it is not divisible | 308 | | -34 17 | Incorrect Input | 309 | 310 | 311 |
312 | 313 | 314 | ### Question 5 - What am I to You 315 | 316 | Write a program that will take 1 char from the user. If the char is a letter print "Lettuce", if the char is a digit (number) print "Durian", otherwise print "Nani!?" 317 | 318 | | INPUT | OUTPUT | 319 | |:-------:|:-------:| 320 | | 2 | Durian | 321 | | b | Lettuce | 322 | | ! | Nani!? | 323 | 324 | 325 | 326 |
327 | 328 | ### Question 6 - Calculator (Lab3 Q4) 329 | 330 | Write a basic calculator program (+, -, \*, /). The program takes three inputs: float, char, float, then prints the result. 331 | 332 | | INPUT | OUTPUT | 333 | |:-------:|:-------:| 334 | | 1 + 45 | 46.00 | 335 | | 1 - 45 | -44.00 | 336 | | 1 * 45 | 45.00 | 337 | | 1 / 45 | 0.02 | 338 | -------------------------------------------------------------------------------- /Week_05/Q1_chars_from_hell.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | char c; 5 | scanf("%c", &c); 6 | 7 | if(c >= 'a' && c <= 'z'){ 8 | printf("%c", c+('A'-'a')); 9 | } 10 | else if(c >= 'A' && c <= 'Z'){ 11 | printf("%c", c+('a'-'A')); 12 | } 13 | else if(c >= '0' && c <= '9'){ 14 | printf("%d", 2*(c-'0')); 15 | } 16 | else{ 17 | printf("asdfasdf"); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Week_05/Q2_staaaars.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | 7 | int i = 0; 8 | 9 | while(i < N){ 10 | printf("*\n"); 11 | i++; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Week_05/Q3_numbeeeers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | 7 | int i = 1; 8 | 9 | while(i <= N){ 10 | printf("%d ", i); 11 | i++; 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Week_05/Q4_hooping_numbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int A, B, t; 5 | scanf("%d %d %d", &A, &B, &t); 6 | 7 | int i; 8 | 9 | // while version 10 | i = B; 11 | while(i >= A){ 12 | printf("%d ", i); 13 | i -= t; 14 | } 15 | 16 | printf("\n"); 17 | 18 | // for version 19 | for(i = B ; i >= A ; i -= t){ 20 | printf("%d ", i); 21 | } 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Week_05/Q5_even_numbeeeers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int A, B; 5 | scanf("%d %d", &A, &B); 6 | 7 | int sum = 0; 8 | int i; 9 | 10 | for(i = A+1 ; i < B ; i++){ 11 | if(i % 2 == 0) 12 | sum += i; 13 | } 14 | 15 | printf("%d", sum); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Week_05/Q6_boom_boom_pow.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | int result = 1; 7 | 8 | int i; 9 | for(i = 0 ; i < b ; i++){ 10 | result *= a; 11 | } 12 | 13 | printf("%d", result); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Week_05/README.md: -------------------------------------------------------------------------------- 1 | # Week 5 Notes 2 | 3 | ## Star Items of the Last Week 4 |
5 | Star Item 1 6 | *** Indentation saves lives (and codes) *** 7 |
8 | 9 |
10 | Star Item 2 11 | *** No boolean expression after else *** 12 |
13 | 14 | ## Warm-Up 15 | 16 | ### Question 1 - Chars from Hell 17 | 18 | Write a program that reads a character. Prints upper-case version of the entered letter if it is a lower-case letter and vice versa. If it is a numeric character, then prints 2 times of the entered number as an integer. If not a letter or number then prints "asdfasdf". 19 | 20 | | INPUT | OUTPUT | 21 | |:-------:|:-------:| 22 | | d | D | 23 | | B | b | 24 | | 7 | 14 | 25 | | . | asdfasdf | 26 | 27 |
28 | 29 | 30 | ## Loops 31 | 32 | 33 | **Conditional (if)** 34 | * Do it if the boolean-expression is true (Only once) 35 | 36 | **Loops (while - for)** 37 | * Do it while the boolean-expression is true 38 | * Do it until the boolean-expression is not true 39 | 40 | | Conditional | Loops | 41 | |:-------:|:-------:| 42 | | ![Conditional Statement](https://www.tutorialspoint.com/cplusplus/images/cpp_decision_making.jpg)     |     ![Loops](https://www.tutorialspoint.com/cplusplus/images/loop_architecture.jpg) | 43 | 44 |
45 | 46 |
47 | 48 | | while( ) | for( ; ; ) | 49 | |:-------:|:-------:| 50 | | ![while](https://www.tutorialspoint.com/cplusplus/images/cpp_while_loop.jpg)     |     ![for](https://www.tutorialspoint.com/cplusplus/images/cpp_for_loop.jpg)| 51 | 52 |
53 | 54 | 55 | ```c 56 | #include 57 | 58 | int main(){ 59 | 60 | /* WHILE LOOP */ 61 | i = 0; // i is the loop control variable, initialize before using 62 | while ( i < 10) // executes 0 or more times, each execution is called iteration 63 | { 64 | /* statements */ 65 | i++; // do NOT forget to update the control variable 66 | 67 | } 68 | 69 | /* FOR LOOP */ 70 | int j; // declare the control variable 71 | for ( j = 0; j < 10; j++) // control variable initialization, control statement, and update 72 | { // in the same line 73 | /* statements*/ 74 | 75 | } 76 | 77 | /* DO-WHILE LOOP */ 78 | int z=0; // initialize the control variable 79 | do // executes 1 or more times!! 80 | { 81 | /* statements */ 82 | z++; 83 | 84 | } while ( z < 10 ); // do not forget ; 85 | 86 | } 87 | ``` 88 | 89 |
90 | 91 | * Control only happens at the if(), while(), for() statement rows. Not a continuous control. 92 | 93 | ```c 94 | int i = 5; 95 | if(i > 0){ 96 | printf('A'); 97 | i = -5; 98 | printf('B'); 99 | } 100 | ``` 101 | 102 |
103 | 104 | ### Question 2 - Staaaars 105 | 106 | #### Question 2a 107 | Write a program that prints 1 star ('*') to the screen. 108 | 109 | #### Question 2b 110 | Write a program that prints 10 stars ('*') next to each other to the screen. 111 | 112 | #### Question 2c 113 | Write a program that reads an integer N and prints N stars ('*') next to each other to the screen. 114 | 115 | #### Question 2d 116 | Write a program that reads an integer N and prints N stars ('*') vertically to the screen. 117 | 118 |
119 | 120 | ### Question 3 - Numbeeeers 121 | 122 | Write a program that reads an integer N and prints numbers from 1 to N to the screen. 123 | 124 | | INPUT | OUTPUT | 125 | |:-------:|:-------:| 126 | | 5 | 1 2 3 4 5 | 127 | | 2 | 1 2 | 128 | 129 |
130 | 131 | ### Question 4 - Hooping Numbers 132 | 133 | Write a program that reads 3 integers A, B and t from the user. Then prints numbers from B to A with decrement of t. 134 | 135 | | INPUT | OUTPUT | 136 | |:-------:|:-------:| 137 | | 7 16 3 | 16 13 10 7 | 138 | | 2 30 6 | 30 24 18 12 6 | 139 | 140 |
141 | 142 | ### Question 5 - Even Numbeeeers 143 | 144 | Write a program that reads two integers A and B. Then prints the sum of even numbers between A and B. Assume that B > A. 145 | 146 | | INPUT | OUTPUT | 147 | |:-------:|:-------:| 148 | | 3 8 | 10 | 149 | | 2 13 | 40 | 150 | | 3 5 | 4 | 151 | 152 |
153 | 154 | ### Question 6 - Boom Boom POW (LAB4 Q6) 155 | 156 | Write a program that takes 2 integers a and b, then prints the result of a^b (a\*a\*a...\*a\*a). 157 | 158 | | INPUT | OUTPUT | 159 | |:-------:|:-------:| 160 | | 3 4 | 81 | 161 | | 2 10 | 1024 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | -------------------------------------------------------------------------------- /Week_06/Q1_digiiiits.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int x; 5 | scanf("%d", &x); 6 | 7 | int even_sum = 0; 8 | int digit_count = 0; 9 | int last_digit; 10 | 11 | while(x > 0){ 12 | last_digit = x % 10; 13 | x /= 10; 14 | 15 | if(last_digit % 2 == 0){ 16 | even_sum += last_digit; 17 | } 18 | digit_count++; 19 | } 20 | 21 | printf("%d %d", digit_count, even_sum); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Week_06/Q2_pma.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int sum = 0; 5 | int x; 6 | 7 | scanf("%d", &x); 8 | while(x >= 0){ 9 | if(x % 2 == 1) 10 | sum += x; 11 | 12 | scanf("%d", &x); 13 | } 14 | 15 | printf("%d", sum); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Week_06/Q3_noBig.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int x, y; 5 | float avg = 0; 6 | float ctr = 0; 7 | 8 | scanf("%d %d", &x, &y); 9 | avg += x; 10 | ctr++; 11 | while(y <= x){ 12 | avg += y; 13 | ctr++; 14 | 15 | x = y; 16 | scanf("%d", &y); 17 | } 18 | 19 | printf("%.2f", avg/ctr); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Week_06/Q4_rectangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int i, j; 5 | int r, c; 6 | 7 | scanf("%d %d", &r, &c); 8 | 9 | for(i = 0 ; i < r ; i++){ 10 | for(j = 0 ; j < c ; j++){ 11 | printf("*"); 12 | } 13 | printf("\n"); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Week_06/Q5_trihard.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N; 5 | scanf("%d", &N); 6 | 7 | int i, j; 8 | 9 | for(i = 1 ; i <= N ; i++){ 10 | for(j = 0 ; j < N-i ; j++){ 11 | printf("-"); 12 | } 13 | for(j = 0 ; j < i ; j++){ 14 | printf("*"); 15 | } 16 | printf("\n"); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Week_06/Q6_prime.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int a, b; 5 | scanf("%d %d", &a, &b); 6 | 7 | int x; 8 | for(x = a ; x <= b ; x++){ 9 | int i; 10 | int isPrime = 1; 11 | for(i = 2 ; i < x ; i++){ 12 | if(x % i == 0){ 13 | isPrime = 0; 14 | break; 15 | } 16 | } 17 | if(x < 2) 18 | isPrime = 0; 19 | if(isPrime == 1){ 20 | printf("%d ", x); 21 | } 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Week_06/README.md: -------------------------------------------------------------------------------- 1 | # Week 6 Notes 2 | 3 | 4 | ### Question 1 - Digiiiits (LAB4 Q10) 5 | 6 | Write a program that takes a number and prints its total digit count and also even digit sum. 7 | 8 | | INPUT | OUTPUT | 9 | |:-------:|:-------:| 10 | | 542175 | 6 6 | 11 | | 24680 | 5 20 | 12 | | 135 | 3 0 | 13 | 14 |
15 | 16 | 17 | ### Question 2 - PMA (Positive Mental Attitude) 18 | 19 | Write a program that will take integers as inputs until the user enters a negative number. Then shows the user the sum of all ODD non-negative numbers that is entered. 20 | 21 | | INPUT | OUTPUT | 22 | |:-------:|:-------:| 23 | | 4 7 45 9 2 0 0 5 8 -4 | 66 | 24 | | 1 1 0 1 -1 | 3 | 25 | 26 |
27 | 28 | ### Question 3 - NoBig 29 | 30 | Write a program that reads numbers until the entered number is greater than the previous entered number. Then print the average of all entered numbers except the last one. (with two decimal places) (At least two numbers will be entered) 31 | 32 | | INPUT | OUTPUT | 33 | |:-------:|:-------:| 34 | | 5 4 3 3 8 | 3.75 | 35 | | 5 4 3 3 8 7 5 9 | 3.75 | 36 | | 1 2 | 1.00 | 37 | | 100 54 46 2 3 50| 50.50 | 38 | 39 |
40 | 41 | 42 | 43 | ### Question 4 - R\*\*\*tangle 44 | 45 | Write a program which reads 2 integers from the user as the length of vertical and horizontal sides of a rectangle respectively. Then prints the rectangle with stars '\*'. 46 | 47 | | INPUT | OUTPUT | 48 | |:-------:|-------| 49 | | 3 4 | \*\*\*\*
\*\*\*\*
\*\*\*\* | 50 | | 2 6 | \*\*\*\*\*\*
\*\*\*\*\*\* | 51 | 52 |
53 | 54 | ### Question 5 - TriHard 55 | 56 | Write a program which takes an integer N as input and prints a right triangle which consists of N many lines. 57 | 58 | 59 | ``` 60 | Input: 61 | 3 62 | 63 | Output: 64 | --* 65 | -** 66 | *** 67 | 68 | Input: 69 | 5 70 | 71 | Output: 72 | ----* 73 | ---** 74 | --*** 75 | -**** 76 | ***** 77 | ``` 78 | 79 |
80 | 81 | ### Question 6 - Prime Example 82 | 83 | #### Question 6a 84 | Write a program that reads a positive integer number and prints "Prime" to the screen if the entered number is a Prime number, otherwise "Prime Not". 85 | 86 | #### Question 6b 87 | Write a program that reads two positive integer numbers and prints all the prime numbers between (inclusive) them. You may assume that the first entered number will always be smaller than the second number. 88 | 89 | | INPUT | OUTPUT | 90 | |:-------:|:-------:| 91 | | 3 22 | 3 5 7 11 13 17 19 | 92 | | 1 3 | 2 3 | 93 | | 32 36 | | -------------------------------------------------------------------------------- /Week_07/Q1_pyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void pyramid(int num_of_rows){ 4 | int i, j; 5 | int counter = 1; 6 | 7 | for(i = 1 ; i <= num_of_rows ; i++){ 8 | for(j = 0 ; j < num_of_rows-i ; j++) 9 | printf(" "); 10 | for(j = 0 ; j < i ; j++) 11 | printf("%d ", counter++); // increase counter after printing 12 | 13 | printf("\n"); 14 | } 15 | } 16 | 17 | int main() { 18 | int x; 19 | scanf("%d", &x); 20 | 21 | pyramid(x); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Week_07/Q2_minimum.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int find_min(int x, int y, int z){ 4 | int minimum; 5 | 6 | if(x <= y && x <= z) 7 | minimum = x; 8 | if(y <= x && y <= z) 9 | minimum = y; 10 | else 11 | minimum = z; 12 | 13 | return minimum; 14 | } 15 | 16 | int main() { 17 | int x, y, z; 18 | scanf("%d %d %d", &x, &y, &z); 19 | 20 | printf("%d", find_min(x, y, z)); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Week_07/Q3_word_up.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | char to_upper(char c){ 4 | if(c >= 'a' && c <= 'z') 5 | return c + ('A'-'a'); 6 | return c; 7 | } 8 | 9 | int main() { 10 | char c; 11 | 12 | scanf("%c", &c); 13 | 14 | while(c != '\n'){ 15 | printf("%c", to_upper(c)); 16 | scanf("%c", &c); 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Week_07/Q4_reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int reverse(int x){ 4 | int reverse_x = 0; 5 | 6 | while(x > 0){ 7 | reverse_x = 10*reverse_x + (x%10); 8 | x /= 10; 9 | } 10 | 11 | return reverse_x; 12 | } 13 | 14 | int main() { 15 | int x; 16 | 17 | scanf("%d", &x); 18 | 19 | printf("%d", reverse(x)); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Week_07/Q5_perfect.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is_perfect(int x){ 4 | int i; 5 | int sum = 0; 6 | 7 | for(i = 1 ; i < x ; i++){ 8 | if(x % i == 0){ 9 | sum += i; 10 | printf("%d ", i); 11 | } 12 | } 13 | printf("\n"); 14 | 15 | if(sum == x) 16 | return 1; 17 | else 18 | return 0; 19 | } 20 | 21 | int main() { 22 | int x; 23 | 24 | scanf("%d", &x); 25 | 26 | printf("%d", is_perfect(x)); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Week_07/Q6_prime_one.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is_prime(int x){ 4 | int i; 5 | int isPrime = 1; 6 | 7 | if(x < 2) 8 | isPrime = 0; 9 | 10 | for(i = 2 ; i < x ; i++){ 11 | if(x % i == 0){ 12 | isPrime = 0; 13 | break; 14 | } 15 | } 16 | 17 | return isPrime; 18 | } 19 | 20 | int main() { 21 | int x; 22 | 23 | scanf("%d", &x); 24 | 25 | printf("%d", is_prime(x)); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Week_07/Q7_prime_two.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is_prime(int x){ 4 | int i; 5 | int isPrime = 1; 6 | 7 | if(x < 2) 8 | isPrime = 0; 9 | 10 | for(i = 2 ; i < x ; i++){ 11 | if(x % i == 0){ 12 | isPrime = 0; 13 | break; 14 | } 15 | } 16 | 17 | return isPrime; 18 | } 19 | 20 | void prime_sum(int x){ 21 | int i; 22 | 23 | for(i = 2 ; i <= x/2 ; i++){ 24 | if(is_prime(i) && is_prime(x-i)){ 25 | printf("%d %d\n", i, x-i); 26 | } 27 | } 28 | } 29 | 30 | int main() { 31 | int x; 32 | 33 | scanf("%d", &x); 34 | 35 | prime_sum(x); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Week_07/Q8_binary.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // long int has 8 byte length while int has 4 byte 4 | // we use long int because we are trying to represent a binary number in decimal format 5 | // and they are too long to fit in normal int variables. 6 | long int to_binary(int x){ 7 | int mult = 1; 8 | long int binary_x = 0; 9 | 10 | while(x > 0){ 11 | binary_x += mult*(x%2); 12 | mult *= 10; 13 | x /= 2; 14 | } 15 | 16 | return binary_x; 17 | } 18 | 19 | int main() { 20 | int x; 21 | 22 | scanf("%d", &x); 23 | 24 | printf("%ld", to_binary(x)); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Week_07/Q9_fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int fibonacci(int x){ 4 | int i; 5 | int f1 = 0, f2 = 1; 6 | 7 | if(x == 1) 8 | return 0; 9 | else if(x == 2) 10 | return 1; 11 | 12 | for(i = 2 ; i < x ; i++){ 13 | int fsum = f1+f2; 14 | f1 = f2; 15 | f2 = fsum; 16 | } 17 | 18 | return f2; 19 | } 20 | 21 | int main() { 22 | int x; 23 | 24 | scanf("%d", &x); 25 | 26 | printf("%d", fibonacci(x)); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Week_07/README.md: -------------------------------------------------------------------------------- 1 | # Week 7 Notes 2 | 3 |
Function Syntax
4 | 5 | ### Question 1 - Number Pyramid 6 | 7 | Write a function named **pyramid** which takes an integer as input: **num_of_rows**, then prints the pyramid of numbers increased by 1 shown below. This function shouldn't return anything. 8 | 9 | ``` 10 | Input: 11 | 4 12 | 13 | Output: 14 | 1 15 | 2 3 16 | 4 5 6 17 | 7 8 9 10 18 | 19 | 20 | Input: 21 | 3 22 | 23 | Output: 24 | 1 25 | 2 3 26 | 4 5 6 27 | ``` 28 | 29 |
30 | 31 | ### Question 2 - Minimum 32 | 33 | Write a function **find_min** which takes three integers as parameters and returns **minimum** among them. Then, call this function in main() with the following inputs and print corresponding outputs. 34 | 35 | | Input | Output | 36 | | ------- | ------ | 37 | | 5 3 8 | 3 | 38 | | 14 -1 9 | -1 | 39 | 40 |
41 | 42 | ### Question 3 - Word Up (LAB5 Q2) 43 | 44 | Write a function named **to_upper** which takes a char as input, and returns the upper case version of the character if it is a lower case letter. Otherwise it should return the same character. 45 | 46 | Then write a program which reads a word from the user, and prints it with upper case characters. Assume valid words will be given. 47 | 48 | 49 | | INPUT | OUTPUT | 50 | |:-------:|:-------:| 51 | | bogazici | BOGAZICI | 52 | | CmpE | CMPE | 53 | 54 |
55 | 56 | ### Question 4 - Reverse 57 | 58 | Write a function named **reverse** that will take an integer number and return its reverse version as another integer. Then fill/write main() appropriately to see the result. 59 | 60 | | Input | Output | 61 | | ----- | ------ | 62 | | 1234 | 4321 | 63 | | 68436 | 63486 | 64 | 65 |
66 | 67 | ### Question 5 - The Perfect Question 68 | 69 | Write a function **is_perfect()** that checks whether the given number is perfect. Perfect number is a positive integer that is equal to the sum of its positive divisors excluding the number itself. The function prints the positive divisors (excluding itself) and returns 1 if the number is perfect and returns 0 otherwise. 70 | 71 | * (These 0 (false) or 1 (true) returning functions are also known as "boolean functions") 72 | 73 | | Input | Output | Return | 74 | | ----- | ---------- | ------ | 75 | | 5 | 1 | 0 | 76 | | 6 | 1 2 3 | 1 | 77 | | 28 | 1 2 4 7 14 | 1 | 78 | | 12 | 1 2 3 4 6 | 0 | 79 | 80 |
81 | 82 | ### Question 6 - Am I a Prime to You (LAB5 Q14) 83 | 84 | Write a function named **is_prime** that takes an integer as a parameter and returns 1 if the number is prime, otherwise returns 0. 85 | 86 | Then write main appropriately to see the result. 87 | 88 | | INPUT | OUTPUT | 89 | |:-------:|:-------:| 90 | | 17 | 1 | 91 | | 345 | 0 | 92 | | 1 | 0 | 93 | 94 |
95 | 96 | 97 | ### Question 7 - Am I TWO Prime to You 98 | 99 | Write a function named **prime_sum** that takes an integer to check if this integer can be expressed as the sum of two prime numbers of all possible combinations and prints them. 100 | 101 | * NOTE: You can use **is_prime** function you wrote previously. 102 | 103 | | INPUT | OUTPUT | 104 | |:-------:|:-------:| 105 | | 34 | 3 31
5 29
11 23
17 17 | 106 | 107 | 108 |
109 | 110 | 111 | ### Question 8 - Binary 112 | 113 | Write a function that will take an integer as a parameter. Then calculates and returns its binary version as another integer. Then write main appropriately to see the result. 114 | 115 | | Input | Output | 116 | | ----- | ---------- | 117 | | 25 | 11001 | 118 | | 532 | 1000010100 | 119 | | 5 | 101 | 120 | 121 |
122 | 123 | ### Question 9 - Fibonacci 124 | 125 | Write a function that will take a positive integer n, and returns the nth Fibonacci number. Then write main appropriately to see the result. 126 | 127 | | Input | Output | 128 | | ----- | ------ | 129 | | 1 | 0 | 130 | | 2 | 1 | 131 | | 3 | 1 | 132 | | 4 | 2 | 133 | | 5 | 3 | 134 | | 10 | 34 | 135 | | 15 | 377 | -------------------------------------------------------------------------------- /Week_08/Q1_alter.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 4 | void alter(int *x, char c){ 5 | if(c == 'i'){ 6 | (*x)++; 7 | } 8 | else if(c == 'd'){ 9 | (*x)--; 10 | } 11 | else if(c == 's'){ 12 | *x = *x * *x; 13 | } 14 | } 15 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 16 | 17 | int main() 18 | { 19 | int x; 20 | char c; 21 | 22 | scanf("%d %c", &x, &c); 23 | 24 | alter( 25 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 26 | &x, c 27 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 28 | ); 29 | 30 | printf("%d", x); 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Week_08/Q2_divide_remain.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // DO NOT EDIT ANYTHING ABOVE 4 | void divideWithRemainder(int num, int div, int *result, int *remainder){ 5 | *result = num/div; 6 | *remainder = num%div; 7 | } 8 | // DO NOT EDIT ANYTHING BELOW 9 | 10 | int main() 11 | { 12 | int n, d, res, rem; 13 | 14 | scanf("%d %d", &n, &d); 15 | 16 | divideWithRemainder( 17 | // DO NOT EDIT ANYTHING ABOVE 18 | n, d, &res, &rem 19 | // DO NOT EDIT ANYTHING BELOW 20 | ); 21 | 22 | printf("%d %d", res, rem); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Week_08/Q3_powered_sums.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 4 | 5 | void sums(int a, int b,int *sum){ 6 | *sum = a + b; 7 | } 8 | 9 | void powered(int sum, int a, int b, int *p1, int *p2){ 10 | *p1 = 1; 11 | *p2 = 1; 12 | 13 | int i; 14 | for(i = 0 ; i < a ; i++){ 15 | *p1 *= sum; 16 | } 17 | for(i = 0 ; i < b ; i++){ 18 | *p2 *= sum; 19 | } 20 | } 21 | 22 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 23 | int main(){ 24 | int a, b; 25 | scanf("%d %d", &a, &b); 26 | 27 | int sum; 28 | 29 | sums( 30 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 31 | a, b, &sum 32 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 33 | ); 34 | printf("%d\n", sum); 35 | 36 | int sum_p1, sum_p2; 37 | powered( 38 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 39 | sum, a, b, &sum_p1, &sum_p2 40 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 41 | ); 42 | 43 | printf("%d %d", sum_p1, sum_p2); 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Week_08/Q4_complex_power.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // a regular power function with pointer for you to compare it with the complex_power finction 4 | void poww(int a, int p, int *res){ 5 | // *poww = a^p 6 | 7 | *res = a; 8 | 9 | int i; 10 | for(i = 1 ; i < p ; i++){ 11 | *res = *res * a; 12 | } 13 | } 14 | 15 | void complex_power(float a, float b, int p, float *re, float *im){ 16 | *re = a; 17 | *im = b; 18 | 19 | int i; 20 | for(i = 1 ; i < p ; i++){ 21 | float temp_re, temp_im; 22 | temp_re = a * *re - b * *im; 23 | temp_im = a * *im + b * *re; 24 | 25 | *re = temp_re; 26 | *im = temp_im; 27 | } 28 | } 29 | 30 | 31 | int main(){ 32 | float a, b; 33 | int p; 34 | scanf("%f %f %d", &a, &b, &p); 35 | 36 | float re, im; 37 | complex_power( 38 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 39 | a, b, p, &re, &im 40 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 41 | ); 42 | printf("%.2f %.2f", re, im); 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Week_08/README.md: -------------------------------------------------------------------------------- 1 | # Week 8 Notes 2 | 3 | ## Functions - Call by Reference - Pointers 4 | 5 | ### Function Syntax 6 | 7 | 8 | 9 |
10 | 11 | ### Swap - Call by Value (Scope of Variables) 12 | 13 | ```c 14 | #include 15 | 16 | /* function declaration */ 17 | void swap(int a, int b); 18 | 19 | /* function definition to swap the values */ 20 | void swap(int a, int b) { 21 | 22 | int temp; 23 | 24 | temp = a; /* save the value of a */ 25 | a = b; /* put b into a */ 26 | b = temp; /* put temp into b */ 27 | } 28 | 29 | int main () { 30 | 31 | /* local variable definition */ 32 | int a = 100; 33 | int b = 200; 34 | 35 | printf("Before swap, value of a : %d\n", a ); 36 | printf("Before swap, value of b : %d\n", b ); 37 | 38 | /* calling a function to swap the values */ 39 | swap(a, b); 40 | 41 | printf("After swap, value of a : %d\n", a ); 42 | printf("After swap, value of b : %d\n", b ); 43 | 44 | return 0; 45 | } 46 | 47 | ``` 48 |
49 | 50 | ![](figures/W08-swap_val1.png) 51 | 52 | --- 53 | 54 | ![](figures/W08-swap_val2.png) 55 | 56 | --- 57 | 58 | ![](figures/W08-swap_val3.png) 59 | 60 | --- 61 | 62 | ![](figures/W08-swap_val4.png) 63 | 64 | --- 65 | 66 | ![](figures/W08-swap_val5.png) 67 | 68 | 69 |
70 | 71 | ## Pointers 72 | 73 | ```c 74 | #include 75 | 76 | int main () { 77 | int *p; 78 | int c; 79 | 80 | p = &c; 81 | c = 4; 82 | 83 | *p = 21; 84 | 85 | int d = 34; 86 | 87 | p = &d; 88 | 89 | printf("%d") 90 | 91 | return 0; 92 | } 93 | 94 | ``` 95 | 96 |
97 | 98 | ![](figures/W08-ptr1.png) 99 | 100 | --- 101 | 102 | ![](figures/W08-ptr2.png) 103 | 104 | --- 105 | 106 | ![](figures/W08-ptr3.png) 107 | 108 |
109 | 110 | **Line by line outputs** 111 | ```c 112 | #include 113 | 114 | int main () { 115 | int *p; 116 | int c; 117 | 118 | printf("int *p, c\n"); 119 | printf("&p: %p - p: %p\n",&p ,p); 120 | printf("&c: %p - c: %d\n",&c ,c); 121 | printf("\n\n"); 122 | 123 | p = &c; 124 | c = 4; 125 | 126 | printf("p = &c;\nc = 4;\n"); 127 | printf("&p: %p - p: %p - *p: %d\n",&p ,p, *p); 128 | printf("&c: %p - c: %d\n",&c ,c); 129 | printf("\n\n"); 130 | 131 | *p = 21; 132 | 133 | printf("*p = 21;\n"); 134 | printf("&p: %p - p: %p - *p: %d\n",&p ,p, *p); 135 | printf("&c: %p - c: %d\n",&c ,c); 136 | printf("\n\n"); 137 | 138 | int d = 34; 139 | 140 | p = &d; 141 | 142 | printf("int d = 34;\np = &d;\n"); 143 | printf("&p: %p - p: %p - *p: %d\n",&p ,p, *p); 144 | printf("&c: %p - c: %d\n",&c ,c); 145 | printf("&d: %p - d: %d\n",&d ,d); 146 | 147 | return 0; 148 | } 149 | ``` 150 | 151 |
152 | 153 | **Side note: Two different ways to define a pointer and then assing an address to it.** 154 | 155 | ```c 156 | int n = 5; 157 | int *p; 158 | p = &n; 159 | ``` 160 | 161 | ```c 162 | int n = 5; 163 | int *p = &n; 164 | ``` 165 |
166 | 167 | ### Swap - Call by Reference 168 | 169 | ```c 170 | #include 171 | 172 | /* function declaration */ 173 | void swap(int *a, int *b); 174 | 175 | /* function definition to swap the values */ 176 | void swap(int *a, int *b) { 177 | int temp; 178 | 179 | temp = *a; /* save the value of *a */ 180 | *a = *b; /* put *b into *a */ 181 | *b = temp; /* put temp into *b */ 182 | } 183 | 184 | int main () { 185 | /* local variable definition */ 186 | int a = 100; 187 | int b = 200; 188 | 189 | printf("Before swap, value of a : %d\n", a ); 190 | printf("Before swap, value of b : %d\n", b ); 191 | 192 | /* calling a function to swap the values */ 193 | swap(&a, &b); 194 | 195 | printf("After swap, value of a : %d\n", a ); 196 | printf("After swap, value of b : %d\n", b ); 197 | 198 | return 0; 199 | } 200 | 201 | ``` 202 | 203 |
204 | 205 | ![](figures/W08-swap_ptr1.png) 206 | 207 | --- 208 | 209 | ![](figures/W08-swap_ptr2.png) 210 | 211 | --- 212 | 213 | ![](figures/W08-swap_ptr3.png) 214 | 215 | --- 216 | 217 | ![](figures/W08-swap_ptr4.png) 218 | 219 | --- 220 | 221 | ![](figures/W08-swap_ptr5.png) 222 | 223 |
224 | 225 | ### Question 1 - Number Alteration 226 | 227 | In the main function, one integer number and a char are read from the user into the variables x and c respectively. Complete the program by writing the function **alter**. 228 | 229 | **alter** function takes one integer pointer (address of an integer) and one char variable as parameters. Function does the following operations: 230 | * If char parameter is 'i', then increase the integer pointer parameter by one. 231 | * If char parameter is 'd', then decrease the integer pointer parameter by one. 232 | * If char parameter is 's', then assign the square of the integer pointer parameter to itself. 233 | * Otherwise, do not change anything. 234 | 235 | In the main function send variables x and c to the **alter** function and then print x in the main function. 236 | 237 | | INPUT | OUTPUT | 238 | |:-------:|:-------:| 239 | | 22 i | 23 | 240 | | -4 d | -5 | 241 | | 20 s | 400 | 242 | | 20 j | 20 | 243 | 244 |
245 | 246 | ```c 247 | #include 248 | 249 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 250 | 251 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 252 | 253 | int main() 254 | { 255 | int x; 256 | char c; 257 | 258 | scanf("%d %c", &x, &c); 259 | 260 | alter( 261 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 262 | 263 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 264 | ); 265 | 266 | printf("%d", x); 267 | return 0; 268 | } 269 | ``` 270 | 271 |
272 | 273 | ### Question 2 - Divide and Remain Still 274 | 275 | Write a function named **divideWithRemainder** which takes two integer (number, divisor) and two integer pointer variables (\*result, \*remainder) as parameters: 276 | 277 | This function should divide the **number** by **divisor**, then store the result in the parameter **\*result**, and store the remainder in the parameter **\*remainder**. 278 | 279 | | INPUT | OUTPUT | 280 | |:-------:|:-------:| 281 | | 8 3 | 2 2 | 282 | | 27 4 | 6 3 | 283 | | 10 2 | 5 0 | 284 | 285 |
286 | 287 | ```c 288 | #include 289 | 290 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 291 | 292 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 293 | 294 | int main() 295 | { 296 | int n, d, res, rem; 297 | 298 | scanf("%d %d", &n, &d); 299 | 300 | divideWithRemainder( 301 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 302 | 303 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 304 | ); 305 | 306 | printf("%d %d", res, rem); 307 | return 0; 308 | } 309 | ``` 310 | 311 |
312 | 313 | ### Question 3 - Powered Sums 314 | 315 | In the main function, two integer numbers are read from the user into the variables a and b respectively. Complete the program by writing two functions named **sums** and **powered**. 316 | 317 | **sums** takes two integers (a and b) and an integer pointer (\*sum). The function should store the sum of these two integers in \*sum pointer. 318 | 319 | **powered** takes three integers (sum, a and b) and two integer pointers (\*sum_p1 and \*sum_p2). The function should store the $a^{th}$ power of the sum in \*sum_p1 and $b^{th}$ power of the sum in \*sum_p2. 320 | 321 | Output: 322 | 323 | First Line: Sum of two numbers 324 | 325 | Second Line: $a^{th}$ power of the a+b and $b^{th}$ power of the a+b 326 | 327 | | INPUT | OUTPUT | 328 | |:-------:|:-------| 329 | | 6 1 | 7
117649 7 | 330 | | 4 4 | 8
4096 4096 | 331 | | 5 0 | 5
3125 1 | 332 | 333 |
334 | 335 | ```c 336 | #include 337 | 338 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 339 | 340 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 341 | 342 | int main(){ 343 | int a, b; 344 | scanf("%d %d",&a,&b); 345 | 346 | int sum; 347 | sums( 348 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 349 | 350 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 351 | ); 352 | printf("%d\n",sum); 353 | 354 | int sum_p1,sum_p2; 355 | powered( 356 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 357 | 358 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE); 359 | ); 360 | printf("%d %d", sum_p1, sum_p2); 361 | 362 | TC_END; 363 | return 0; 364 | } 365 | 366 | ``` 367 | 368 |
369 | 370 | ### Question 4 - Complex Powers (Old Midterm Question) 371 | 372 | * Think about how we write the regular power. 373 | 374 | ![Complex Powers](figures/q_complex.png) 375 | -------------------------------------------------------------------------------- /Week_08/figures/W08-ptr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-ptr1.png -------------------------------------------------------------------------------- /Week_08/figures/W08-ptr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-ptr2.png -------------------------------------------------------------------------------- /Week_08/figures/W08-ptr3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-ptr3.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_ptr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_ptr1.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_ptr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_ptr2.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_ptr3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_ptr3.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_ptr4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_ptr4.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_ptr5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_ptr5.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_val1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_val1.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_val2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_val2.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_val3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_val3.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_val4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_val4.png -------------------------------------------------------------------------------- /Week_08/figures/W08-swap_val5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/W08-swap_val5.png -------------------------------------------------------------------------------- /Week_08/figures/q_complex.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_08/figures/q_complex.png -------------------------------------------------------------------------------- /Week_09/Q1_large.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, i; 5 | 6 | scanf("%d", &N); 7 | 8 | int arr[N]; 9 | 10 | for(i = 0 ; i < N ; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | float sum = 0, avg; 14 | 15 | for(i = 0 ; i < N ; i++) 16 | sum += arr[i]; 17 | 18 | avg = sum / N; 19 | 20 | for(i = 0; i < N; i++){ 21 | if(arr[i] > avg){ 22 | printf("%d ", arr[i]); 23 | } 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Week_09/Q2_statistics.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, i; 5 | int arr[51]; 6 | 7 | scanf("%d", &N); 8 | 9 | for(i = 0 ; i < N ; i++) 10 | scanf("%d", &arr[i]); 11 | 12 | float sum = 0, avg, var = 0; 13 | 14 | for(i = 0 ; i < N ; i++) 15 | sum += arr[i]; 16 | 17 | avg = sum / N; 18 | 19 | for(i = 0 ; i < N ; i++) 20 | var += (arr[i] - avg) * (arr[i] - avg); 21 | var /= N; 22 | 23 | printf("%.0f %.2f %.2f", sum, avg, var); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Week_09/Q3_encryption.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i; 6 | char arr[51]; 7 | int s_len = 0; 8 | 9 | char c; 10 | 11 | // read sentence 12 | scanf("%c", &c); 13 | while(c != '\n'){ 14 | arr[s_len] = c; 15 | s_len++; 16 | 17 | scanf("%c", &c); 18 | } 19 | 20 | 21 | // encryption 22 | for(i = 0 ; i < s_len ; i++) 23 | arr[i] = arr[i]+1; 24 | 25 | for(i = 0 ; i < s_len ; i++){ 26 | printf("%c", arr[i]); 27 | } 28 | 29 | // decryption 30 | for(i = 0 ; i < s_len ; i++) 31 | arr[i] = arr[i]-1; 32 | 33 | for(i = 0 ; i < s_len ; i++){ 34 | printf("%c", arr[i]); 35 | } 36 | 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Week_09/Q4_reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i; 6 | 7 | int N; 8 | scanf("%d", &N); 9 | int arr[N]; 10 | 11 | for(i = 0 ; i < N ; i++) 12 | scanf("%d", &arr[i]); 13 | 14 | 15 | // part a 16 | for(i = N-1 ; i >= 0 ; i--) 17 | printf("%d ", arr[i]); 18 | printf("\n"); 19 | 20 | 21 | // part b 22 | for(i = 0 ; i < N/2 ; i++){ 23 | int temp = arr[i]; 24 | arr[i] = arr[N-i-1]; 25 | arr[N-i-1] = temp; 26 | } 27 | 28 | for(i = 0 ; i < N ; i++) 29 | printf("%d ", arr[i]); 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Week_09/Q5_lets_count.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i; 6 | 7 | int N; 8 | scanf("%d", &N); 9 | int arr[N]; 10 | int counter[101] = {0}; 11 | 12 | for(i = 0 ; i < N ; i++) 13 | scanf("%d", &arr[i]); 14 | 15 | 16 | for(i = 0 ; i < N ; i++) 17 | counter[arr[i]]++; 18 | 19 | for(i = 0 ; i < 101 ; i++){ 20 | if(counter[i] > 0){ 21 | printf("%d --> %d", i, counter[i]); 22 | } 23 | } 24 | 25 | // Example 26 | // 5 10 2 5 50 5 10 1 2 2 -> arr[N] 27 | 28 | // 0 1 2 3 4 5 6 7 8 9 10 11 12 ... 50 ... 100 -> counter[101] 29 | // 0 1 3 0 0 3 0 0 0 0 2 0 0 1 0 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Week_09/Q6_inception.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i, j; 6 | 7 | int N, M; 8 | scanf("%d %d", &N, &M); 9 | int arr1[N], arr2[M]; 10 | 11 | for(i = 0 ; i < N ; i++) 12 | scanf("%d", &arr1[i]); 13 | 14 | for(i = 0 ; i < M ; i++) 15 | scanf("%d", &arr2[i]); 16 | 17 | for(j = 0 ; j < M ; j++){ 18 | for(i = 0 ; i < N ; i++){ 19 | if(arr1[i] != arr2[j+i]) 20 | break; 21 | } 22 | 23 | // i will be equal to N if and only if the above loop terminates without entering the if statement 24 | // So if i is equal to N, it means that we found the same sub array to arr1. 25 | if(i == N){ 26 | printf("%d", j); 27 | break; 28 | } 29 | } 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Week_09/Q7_pairs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, X, i, j; 5 | 6 | scanf("%d %d", &N, &X); 7 | 8 | int arr[N]; 9 | 10 | for(i = 0 ; i < N ; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | for(i = 0 ; i < N-1 ; i++){ 14 | for(j = i+1 ; j < N ; j++){ 15 | if(arr[i]+arr[j] == X){ 16 | printf("(%d, %d)\n", arr[i], arr[j]); 17 | } 18 | } 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Week_09/Q8_fix34.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, i, j; 5 | 6 | scanf("%d", &N); 7 | 8 | int arr[N]; 9 | 10 | for(i = 0; i < N; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | for(i = 0; i < N; i++) { 14 | if(arr[i] == 3) { 15 | for(j = 0; j < N; j++) { 16 | if(arr[j] == 4 && arr[j-1] != 3) { 17 | int temp = arr[i+1]; 18 | arr[i+1] = arr[j]; 19 | arr[j] = temp; 20 | } 21 | } 22 | } 23 | } 24 | 25 | for(i = 0; i < N; i++) 26 | printf("%d ", arr[i]); 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Week_09/Q9_bubble_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int N, i, j; 5 | 6 | scanf("%d", &N); 7 | 8 | int arr[N]; 9 | 10 | for(i = 0 ; i < N ; i++) 11 | scanf("%d", &arr[i]); 12 | 13 | for(i = 0 ; i < N ; i++){ 14 | for(j = 0 ; j < N-1 ; j++){ 15 | if(arr[j] > arr[j+1]){ 16 | int temp = arr[j]; 17 | arr[j] = arr[j+1]; 18 | arr[j+1] = temp; 19 | } 20 | } 21 | } 22 | 23 | for(i = 0; i < N; i++) 24 | printf("%d ", arr[i]); 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Week_09/README.md: -------------------------------------------------------------------------------- 1 | # Week 9 Notes 2 | 3 | ## Arrays 4 | 5 |
6 | 7 | Brief Summary 8 | 9 |
10 | 11 | * An array is a group of memory locations. 12 | * These locations are related by the fact that they have all the same name and same type. 13 | * We can define an array as follows: 14 | 15 | - type arrName[size]; 16 | 17 | * Where, **type** is any data type like int, float etc. 18 | * **arrName** is the name of the array variable. 19 | * The square brackets contains an integer value **size**. It is the total number of values we can store in the array variable. 20 | 21 | * In order to access to a particular location, i.e., an element within the array, we specify the name of the array and the position number (index) of the variable in square brackets. 22 | 23 | 24 | ### Examples: 25 | 26 | * We want to create an array named digit of size 10 and type as int. 27 | 28 | ```c 29 | #include 30 | 31 | int main() 32 | { 33 | int digit[10]; 34 | 35 | return 0; 36 | } 37 | ``` 38 | 39 | * The above code allocates memory space for 10 integer data values. Indices starts from 0. Also, be aware of that, these elements are not initialized, so that we don't know their values. 40 | 41 | 42 | 43 |
44 | 45 | --- 46 | 47 | * We can assign values directly to each element of the array: 48 | 49 | ```c 50 | #include 51 | 52 | int main() 53 | { 54 | int digit[10]; 55 | 56 | digit[3] = 123; 57 | 58 | return 0; 59 | } 60 | ``` 61 | 62 | 63 | 64 |
65 | 66 | --- 67 | 68 | * We can read values from the user and store them in the array: 69 | 70 | ```c 71 | #include 72 | 73 | int main() 74 | { 75 | int digit[10]; 76 | 77 | scanf("%d", &digit[5]); // Assume user enters 13 78 | digit[0] = digit[5] * 2; 79 | 80 | return 0; 81 | } 82 | ``` 83 | 84 | 85 | 86 |
87 | 88 | --- 89 | 90 | * Now, let's initialize our digit array with digits from 0 to 9 to: 91 | 92 | ```c 93 | #include 94 | 95 | int main() 96 | { 97 | int digit[10] = {0,1,2,3,4,5,6,7,8,9}; 98 | 99 | return 0; 100 | } 101 | ``` 102 | 103 | 104 | 105 |
106 | 107 | --- 108 | 109 | * What if we don't initialize all the elements of the array: 110 | 111 | ```c 112 | #include 113 | 114 | int main() 115 | { 116 | int digit[10] = {0,1,2,3,4}; 117 | 118 | return 0; 119 | } 120 | ``` 121 | 122 | * Then the uninitialized elements will be automaticly initialized to 0 (zero). 123 | 124 | 125 | 126 |
127 | 128 | --- 129 | 130 | * Now, let's assign digits from 0 to 9 to the corresponding locations in our digit array with a loop: 131 | 132 | ```c 133 | #include 134 | 135 | int main() 136 | { 137 | int i; 138 | int digit[10]; 139 | 140 | for(i = 0; i < 10; i++) 141 | digit[i] = i; 142 | 143 | return 0; 144 | } 145 | ``` 146 | 147 | 148 | 149 |
150 | 151 | --- 152 | 153 | * We can assign values to an array form the user with a for loop: 154 | 155 | ```c 156 | #include 157 | 158 | int main() 159 | { 160 | int i; 161 | int digit[10]; 162 | 163 | for(i = 0; i < 10; i++) 164 | scanf("%d", &digit[i]); // Assume the user enters 1 3 5 7 9 11 13 15 17 19 165 | 166 | return 0; 167 | } 168 | ``` 169 | 170 | 171 | 172 |
173 | 174 | --- 175 | 176 | * You can also create a Variable Length Array (VLA), using an integer variable for the size. 177 | * However, you can not initialize a VLA when declaring. 178 | * Even though VLA's are supported from C99, there are compilers that are not compatible with VLA's. 179 | 180 | ```c 181 | #include 182 | 183 | int main() 184 | { 185 | int n = 10; 186 | int digit[n]; 187 | 188 | return 0; 189 | } 190 | ``` 191 | 192 | * The above code allocates memory space for 10 (value of variable n) integer data values. But again be aware of that, these elements are not initialized, so that we don't know their values. 193 | 194 | 195 | 196 |
197 | 198 | --- 199 | 200 |
201 | 202 | **THINK ABOUT**: What are the ways to initialize an array with 0's (zeros). 203 | 204 | ```c 205 | #include 206 | 207 | int main() 208 | { 209 | int digit[10] = {0}; 210 | 211 | return 0; 212 | } 213 | ``` 214 | 215 |
216 | 217 |
218 | 219 | ## Questions 220 | 221 | ### Question 1 - Large 222 | 223 | Write a program that reads an integer N from the user, then reads N more integers from the user and store them in an array. Then prints elements larger than the average of numbers held in the array. 224 | 225 | | Input | Output | 226 | | ------------------------------ | ---------- | 227 | | 10
1 2 3 4 5 6 7 8 9 10 | 6 7 8 9 10 | 228 | | 4
4 1 3 8 | 8 | 229 | 230 |
231 | 232 | ### Question 2 - Statistics 233 | 234 | Write a program that reads an integer N from the user, then reads N more integers from the user and store them in an array. Then prints the sum, average and variance of the elements (Assume N is smaller than 50). 235 | 236 | 237 | 238 | 239 | | Input | Output | 240 | | ----------------------------- | ----------- | 241 | | 10
1 2 3 4 5 6 7 8 9 10 | 55 5.50 8.25| 242 | | 4
4 1 3 8 | 16 4.00 6.50| 243 | 244 |
245 | 246 | ### Question 3 - Encryption (Teaching Codes Lab7) 247 | 248 | Write a program which reads a sentence from the user (read characters until '\n' appears). Your program then should put this sentence into a char array (assume the sentence will be shorter than 50 characters). 249 | 250 | Then, encrypt this sentence by adding +1 to the value of each character and print the encrypted version of the char array to the screen. 251 | 252 | Then, decrypt this sentence by adding -1 to each character, and print the decrypted version of the char array to the screen. 253 | 254 | | Input | Output | 255 | | -------------- | --------------------------------- | 256 | | Hello Bogazici | Ifmmp!Cphb{jdj
Hello Bogazici | 257 | | Stay at home | Tubz!bu!ipnf
Stay at home | 258 | 259 | 260 |
261 | 262 | ### Question 4 - Reverse (Teaching Codes Lab7) 263 | 264 | Write a program which reads an integer N from the user, then reads N integers from the user and store them in an array (Assume N is smaller than 50). 265 | 266 | **a**. Then print them to the screen in reverse order. 267 | 268 | **b.** Then reverse the order of these integers in the array and print the array. 269 | 270 | | Input | Output | 271 | | --------------------- | -------------- | 272 | | 5
3 1 -4 5 2 | 2 5 -4 1 3 | 273 | | 6
15 7 2 89 8 12 | 12 8 89 2 7 15 | 274 | 275 |
276 | 277 | ### Question 5 - Let's Count 278 | 279 | Write a program which reads an integer N from the user, then reads N integers from the user to an array. Then find frequency of each element in the array. Assume N is smaller than 1000 and the entered numbers will be between 0 and 100 (inclusive). 280 | 281 | | INPUT | OUTPUT | 282 | |-------|-------| 283 | | 10
5 10 2 5 50 5 10 1 2 2 | 1 --> 1
2 --> 3
5 --> 3
10 --> 2
50 --> 1 | 284 | 285 |
286 | 287 | ### Question 6 - Inception (Teaching Codes Lab7) 288 | 289 | Write a program which reads two integers: N and M from the user. Then the program should read N more integers, then M more integers from the user. Assume N < M < 50. 290 | 291 | If the first combination of N integers appears at least once in the second combination of M integers, the program should write the starting index of the first appearance in the second combination. Otherwise it should not print anything. 292 | 293 | | Input | Output | 294 | | ---------------------------------------------- | ------ | 295 | | 4 12
4 5 6 5
1 2 1 2 99 99 4 5 6 5 9 9 | 6 | 296 | | 3 5
2 2 2
1 2 2 2 2 5 | 1 | 297 | | 3 5
2 3 2
1 2 2 2 5 | | 298 | 299 |
300 | 301 | ### Question 7 - Pairs 302 | 303 | Write a program which reads an integer N and an integer X from the user, then reads N integers from the user to an array. Then find all pairs in the array whose sum is equal to X. 304 | 305 | * A number can not be a pair with itself. Pairs should consist of different numbers. 306 | * You can print either (3,4) or (4,3) for one pair. 307 | * You should print all the pairs even if they are same. 308 | 309 | | INPUT | OUTPUT | 310 | |-------|-------| 311 | | 7 7
2 4 3 5 7 8 9 | (2, 5)
(4, 3) | 312 | | 10 7
2 4 3 5 6 -2 4 7 8 9 | (2, 5)
(4, 3)
(3, 4)
(-2, 9) | 313 | 314 | 315 |
316 | 317 | ### Question 8 - fix34 318 | 319 | Write a program which reads an integer N from the user, then reads N integers from the user to an array. Then rearrange the array so that every 3 is immeddiately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has number after it that is not a 3, and a 3 appears in the array before any 4. 320 | 321 | | INPUT | OUTPUT | 322 | |-------|-------| 323 | | 4
1 3 1 4 | 1 3 4 1 | 324 | | 7
1 3 1 4 4 3 1 | 1 3 4 1 1 3 4 | 325 | | 4
3 2 2 4 | 3 4 2 2 | 326 | 327 |
328 | 329 | ### Question 9 - Bubble Sort 330 | 331 | Write a program which reads an integer N from the user, then reads N integers from the user to an array. Then sort elements of the array in ascending order. 332 | 333 | | INPUT | OUTPUT | 334 | |-------|-------| 335 | | 8
4 2 8 6 7 3 1 5 | 1 2 3 4 5 6 7 8 | 336 | 337 | 338 | Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. 339 | 340 | Example: 341 | 342 | First Pass: 343 | * ( **5** **1** 4 2 8 ) –> ( **1** **5** 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. 344 | * ( 1 **5** **4** 2 8 ) –> ( 1 **4** **5** 2 8 ), Swap since 5 > 4 345 | * ( 1 4 **5** **2** 8 ) –> ( 1 4 **2** **5** 8 ), Swap since 5 > 2 346 | * ( 1 4 2 **5** **8** ) –> ( 1 4 2 **5** **8** ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. 347 | 348 | Second Pass: 349 | * ( **1** **4** 2 5 8 ) –> ( **1** **4** 2 5 8 ) 350 | * ( 1 **4** **2** 5 8 ) –> ( 1 **2** **4** 5 8 ), Swap since 4 > 2 351 | * ( 1 2 **4** **5** 8 ) –> ( 1 2 **4** **5** 8 ) 352 | * ( 1 2 4 **5** **8** ) –> ( 1 2 4 **5** **8** ) 353 | 354 | Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. 355 | 356 | Third Pass: 357 | * ( **1** **2** 4 5 8 ) –> ( **1** **2** 4 5 8 ) 358 | * ( 1 **2** **4** 5 8 ) –> ( 1 **2** **4** 5 8 ) 359 | * ( 1 2 **4** **5** 8 ) –> ( 1 2 **4** **5** 8 ) 360 | * ( 1 2 4 **5** **8** ) –> ( 1 2 4 **5** **8** ) 361 | 362 | -------------------------------------------------------------------------------- /Week_09/figures/array_fig1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/array_fig1.png -------------------------------------------------------------------------------- /Week_09/figures/array_fig2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/array_fig2.png -------------------------------------------------------------------------------- /Week_09/figures/array_fig3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/array_fig3.png -------------------------------------------------------------------------------- /Week_09/figures/array_fig4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/array_fig4.png -------------------------------------------------------------------------------- /Week_09/figures/array_fig5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/array_fig5.png -------------------------------------------------------------------------------- /Week_09/figures/array_fig6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/array_fig6.png -------------------------------------------------------------------------------- /Week_09/figures/variance.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_09/figures/variance.png -------------------------------------------------------------------------------- /Week_10/Q1_pairs.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a1.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int main(void) { 15 | 16 | int N, X; 17 | int i, j; 18 | 19 | scanf("%d %d",&N, &X); 20 | 21 | int arr[N]; 22 | 23 | for(i = 0; i < N; i++) 24 | { 25 | scanf("%d",&arr[i]); 26 | } 27 | 28 | for(i = 0; i < N-1; i++) 29 | { 30 | for(j = i+1; j < N; j++) 31 | { 32 | if(arr[i] + arr[j] == X) 33 | printf("%d %d\n", arr[i], arr[j]); 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Week_10/Q2_fix34.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a2.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | void swap(int N, int arr[], int k, int l) 15 | { 16 | int temp; 17 | 18 | temp = arr[k]; 19 | arr[k] = arr[l]; 20 | arr[l] = temp; 21 | } 22 | int main(void) { 23 | 24 | int N, i, j; 25 | int index_to_be_changed; 26 | 27 | scanf("%d", &N); 28 | 29 | int arr[N]; 30 | 31 | for(i = 0; i < N;i++) 32 | { 33 | scanf("%d",&arr[i]); 34 | } 35 | 36 | for(i = 0; i < N-1; i++) 37 | { 38 | if(arr[i] == 3) 39 | { 40 | index_to_be_changed = i+1; 41 | 42 | for(j = 0; j < N; j++) 43 | { 44 | if(arr[j] == 4 && arr[j-1] != 3) 45 | swap(N, arr, index_to_be_changed, j); 46 | } 47 | } 48 | } 49 | for(i = 0; i < N; i++) 50 | printf("%d ", arr[i]); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Week_10/Q3_bubble_sort.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a3.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | void swap(int N, int arr[], int i, int j) 15 | { 16 | int temp; 17 | 18 | temp = arr[i]; 19 | arr[i] = arr[j]; 20 | arr[j] = temp; 21 | 22 | } 23 | int main(void) { 24 | 25 | int N, i, j; 26 | 27 | scanf("%d",&N); 28 | 29 | int arr[N]; 30 | 31 | for(i = 0; i < N; i++) 32 | scanf("%d", &arr[i]); 33 | 34 | for(i = 0; i < N-1; i++) 35 | { 36 | for(j = i+1; j < N; j++) 37 | { 38 | if(arr[i] > arr[j]) 39 | swap(N, arr, i , j); 40 | } 41 | } 42 | for(i = 0; i < N; i++) 43 | printf("%d ", arr[i]); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Week_10/Q4_transpose.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a4.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int main(void) { 15 | 16 | int N, M; 17 | 18 | int i, j; 19 | 20 | scanf("%d %d", &N, &M); 21 | 22 | int arr[N][M]; 23 | 24 | for(i = 0; i < N; i++) 25 | { 26 | for(j = 0; j < M; j++) 27 | { 28 | scanf("%d",&arr[i][j]); 29 | } 30 | } 31 | 32 | for(j = 0; j < M; j++) 33 | { 34 | for(i = 0; i < N; i++) 35 | printf("%d ",arr[i][j]); 36 | printf("\n"); 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Week_10/Q5_2d_functions.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a5.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | void read_2d_array(int N, int M, int arr[N][M]) 15 | { 16 | int i, j; 17 | 18 | for(i = 0; i < N; i++) 19 | { 20 | for(j = 0; j < M; j++) 21 | scanf("%d", &arr[i][j]); 22 | 23 | } 24 | } 25 | void print_2d_array(int N, int M, int arr[N][M]) 26 | { 27 | int i, j; 28 | 29 | for(i = 0; i < N; i++) 30 | { 31 | for(j = 0; j < M; j++) 32 | printf("%d ", arr[i][j]); 33 | 34 | printf("\n"); 35 | } 36 | } 37 | 38 | int main(void) { 39 | 40 | int N, M; 41 | 42 | scanf("%d %d", &N, &M); 43 | 44 | int arr[N][M]; 45 | 46 | read_2d_array(N, M, arr); 47 | 48 | print_2d_array(N, M, arr); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /Week_10/Q6_matrix_add.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a6.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | void read_2d_array(int N, int M, int arr[N][M]) 15 | { 16 | int i, j; 17 | 18 | for(i = 0; i < N; i++) 19 | { 20 | for(j = 0; j < M; j++) 21 | scanf("%d", &arr[i][j]); 22 | 23 | } 24 | } 25 | void print_2d_array(int N, int M, int arr[N][M]) 26 | { 27 | int i, j; 28 | 29 | for(i = 0; i < N; i++) 30 | { 31 | for(j = 0; j < M; j++) 32 | printf("%d ", arr[i][j]); 33 | 34 | printf("\n"); 35 | } 36 | } 37 | void matrix_sum(int N, int M, int firstArr[N][M], int secondArr[N][M], int summation[N][M]) 38 | { 39 | int i, j; 40 | 41 | for(i = 0; i < N; i++) 42 | { 43 | for(j = 0; j < M; j++) 44 | summation[i][j] = firstArr[i][j] + secondArr[i][j]; 45 | } 46 | } 47 | 48 | int main(void) { 49 | 50 | int N, M; 51 | 52 | scanf("%d %d", &N, &M); 53 | 54 | int firstArr[N][M], secondArr[N][M], sum[N][M]; 55 | 56 | read_2d_array(N, M, firstArr); 57 | 58 | read_2d_array(N, M, secondArr); 59 | 60 | matrix_sum(N, M, firstArr, secondArr, sum); 61 | 62 | print_2d_array(N, M, sum); 63 | 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Week_10/Q7_diagonal.c: -------------------------------------------------------------------------------- 1 | /* 2 | ============================================================================ 3 | Name : lab10_a7.c 4 | Author : 5 | Version : 6 | Copyright : Your copyright notice 7 | Description : Hello World in C, Ansi-style 8 | ============================================================================ 9 | */ 10 | 11 | #include 12 | #include 13 | void read_2d_array(int N, int M, int arr[N][M]) 14 | { 15 | int i, j; 16 | 17 | for(i = 0; i < N; i++) 18 | { 19 | for(j = 0; j < M; j++) 20 | scanf("%d", &arr[i][j]); 21 | 22 | } 23 | } 24 | int main(void) { 25 | 26 | int N, i, j; 27 | int left_sum = 0, right_sum = 0; 28 | 29 | scanf("%d",&N); 30 | 31 | int arr[N][N]; 32 | 33 | read_2d_array(N, N, arr); 34 | 35 | for(i = 0; i < N; i++) 36 | { 37 | for(j = 0; j < N; j++) 38 | { 39 | if(i == j) 40 | left_sum += arr[i][j]; 41 | if(i == N-1-j) 42 | right_sum += arr[i][j]; 43 | } 44 | } 45 | printf("left diagonal sum: %d\n",left_sum); 46 | printf("right diagonal sum: %d\n",right_sum); 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /Week_10/README.md: -------------------------------------------------------------------------------- 1 | # Week 10 Notes 2 | 3 | ## A Brief Summary of Arrays (Continued) 4 |
5 | 6 | Using Arrays with Functions 7 | 8 |
9 | 10 | * Arrays are always *passed by reference* to a function. 11 | - Remember: the name of an array (let's say arrName) is actually equivalent to the address of the first element of the array, i.e., 12 | arrName is equal to &arrName[0] 13 | 14 | * Three ways of passing an array to a function: 15 | 16 | - (1) as a pointer, 17 | 18 | - (2) as a sized array, 19 | 20 | - (3) as an unsized array. 21 | 22 | * Actually, these three methods all imply the same thing. Let's give an example to each method: 23 | 24 | ### Example 1: Passing array as a pointer: 25 | 26 | ```c 27 | #include 28 | 29 | void printArray(int *arr, int len) 30 | { 31 | int i; 32 | for(i = 0; i < len; i++) 33 | { 34 | printf("%d ",*arr); 35 | arr++; 36 | } 37 | } 38 | int main() 39 | { 40 | int myArr[5] = {1, 5, 6, 2, 2}; 41 | 42 | printArray(myArr, 5); 43 | 44 | return 0; 45 | } 46 | ``` 47 | The output will be: 48 | 1 5 6 2 2 49 | 50 | * Printing the elements of the array with the following way is also valid: 51 | 52 | ```c 53 | #include 54 | 55 | void printArray(int *arr, int len) 56 | { 57 | int i; 58 | for(i = 0; i < len; i++) 59 | { 60 | printf("%d ", arr[i]); 61 | } 62 | } 63 | ``` 64 | 65 | ### Example 2: Passing array as a sized array: 66 | 67 | ```c 68 | #include 69 | 70 | void printArray(int arr[5], int len) 71 | { 72 | int i; 73 | for(i = 0; i < len; i++) 74 | { 75 | printf("%d ",arr[i]); 76 | 77 | } 78 | } 79 | int main() 80 | { 81 | int myArr[5] = {1, 5, 6, 2, 2}; 82 | 83 | printArray(myArr, 5); 84 | 85 | return 0; 86 | } 87 | ``` 88 | The output will be: 89 | 1 5 6 2 2 90 | 91 | * Writing any other number inside the curly brackets in the function definition does not alter the result: 92 | 93 | 94 | ```c 95 | #include 96 | 97 | void printArray(int arr[2], int len) 98 | { 99 | int i; 100 | for(i = 0; i < len; i++) 101 | { 102 | printf("%d ",arr[i]); 103 | 104 | } 105 | } 106 | int main() 107 | { 108 | int myArr[5] = {1, 5, 6, 2, 2}; 109 | 110 | printArray(myArr, 5); 111 | 112 | return 0; 113 | } 114 | ``` 115 | The output will be: 116 | 1 5 6 2 2 117 | 118 | * This is because the compiler does not care the given size in the function definition. 119 | 120 | ### Example 3: Passing array as an unsized array: 121 | 122 | ```c 123 | #include 124 | 125 | void printArray(int arr[], int len) 126 | { 127 | int i; 128 | for(i = 0; i < len; i++) 129 | { 130 | printf("%d ",arr[i]); 131 | 132 | } 133 | } 134 | int main() 135 | { 136 | int myArr[5] = {1, 5, 6, 2, 2}; 137 | 138 | printArray(myArr, 5); 139 | 140 | return 0; 141 | } 142 | ``` 143 | The output again will be: 144 | 1 5 6 2 2 145 | 146 | ## Returning an array from a function: 147 | 148 | * In C, we cannot return an entire array directly from a function. However, we can return a pointer to an array by specifying the array's name without an index. Let's see an example: 149 | 150 | ### Example: 151 | 152 | ```c 153 | #include 154 | 155 | int * doubleArray(int arr[], int len) 156 | { 157 | int i; 158 | for(i = 0; i < len; i++) 159 | { 160 | arr[i] = arr[i]*2; 161 | } 162 | return arr; 163 | } 164 | int main() 165 | { 166 | int arr[5] = {1, 5, 6, 2, 2}; 167 | int * doubled; 168 | int i; 169 | 170 | doubled = doubleArray(arr, 5); 171 | 172 | for(i = 0; i < 5; i++) 173 | { 174 | printf("%d ",doubled[i]); 175 | } 176 | 177 | return 0; 178 | } 179 | ``` 180 | * The output will be: 181 | 2 10 12 4 4 182 | 183 | * If we want to return a local array defined in the function, we have to define it as static. Let's look at the following two examples to understand the difference: 184 | 185 | ```c 186 | #include 187 | 188 | int * getArray() 189 | { 190 | int i; 191 | int arr[5]; 192 | 193 | for(i = 0; i < 5; i++) 194 | { 195 | arr[i] = i+1; 196 | } 197 | return arr; 198 | } 199 | int main() 200 | { 201 | int * arr; 202 | 203 | int i; 204 | 205 | arr = getArray(); 206 | 207 | for(i = 0; i < 5; i++) 208 | { 209 | printf("%d ",arr[i]); 210 | } 211 | 212 | return 0; 213 | } 214 | ``` 215 | 216 | * In the example above, we define a local array *arr* inside the function *getArray*, assign and then return it to the main function. When we run the program, we will get a warning saying: 217 | 218 | *warning: function returns address of local variable* 219 | 220 | * This is because it is illegal in C to return memory location that is allocated within a function. When we run this program, the output looks weird: 221 | 222 | 1 2 6277760 0 5 223 | 224 | * In order to overcome this problem, we need to define the array as static inside the function: 225 | 226 | ```c 227 | #include 228 | 229 | int * getArray() 230 | { 231 | int i; 232 | static int arr[5]; 233 | 234 | for(i = 0; i < 5; i++) 235 | { 236 | arr[i] = i+1; 237 | } 238 | return arr; 239 | } 240 | int main() 241 | { 242 | int * arr; 243 | 244 | int i; 245 | 246 | arr = getArray(); 247 | 248 | for(i = 0; i < 5; i++) 249 | { 250 | printf("%d ",arr[i]); 251 | } 252 | 253 | return 0; 254 | } 255 | ``` 256 | 257 | * Now we get the desired output: 258 | 259 | 1 2 3 4 5 260 | 261 | 262 |
263 | 264 | ## A Brief Summary of Multidimensional Arrays 265 | 266 |
267 | 268 | Multidimensional Arrays 269 | 270 | ![Two dimensional array in C programming](https://cdn.programiz.com/sites/tutorial2program/files/two-dimensional-array_0.jpg) 271 | 272 | * We will only consider the 2-dimensional arrays. 273 | * We can define a 2-dim array as follows: 274 | 275 | - type arrName[rowSize][columnSize]; 276 | 277 | * Where, **type** is any data type like int, float etc. 278 | * **arrName** is the name of the array variable. 279 | * **rowSize** is a number denoting the number of rows and **columnSize** denotes the number of columns in a 2-dimensional array. 280 | 281 |
282 | 283 | ### Memory Representation of Multidimensional Arrays 284 | 285 | ![MDA1](figures/W11_01.png) 286 | ![MDA2](figures/W11_02.png) 287 | 288 |
289 | 290 | ### Multidimensional Arrays Declarations 291 | 292 | ```c 293 | float x[3][4]; 294 | 295 | int c[2][3] = {{1, 3, 0}, {-1, 5, 9}}; 296 | 297 | int y[2][3][4] = { 298 | {{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}}, 299 | {{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}}; 300 | 301 | int c[][3] = {{1, 3, 0}, {-1, 5, 9}}; 302 | 303 | int c[2][3] = {1, 3, 0, -1, 5, 9}; 304 | 305 | // To initialize every element to 0 306 | int c[2][3] = {{0}}; 307 | ``` 308 | 309 |
310 | 311 | 312 | ### Example: 2-dim Array 313 | 314 | ```c 315 | #include 316 | int main() 317 | { 318 | int arr[3][5] = {{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}}; 319 | // we can also initialize the array as follows: int arr[3][5] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 320 | 321 | int i,j; 322 | 323 | 324 | for(i = 0; i < 3; i++) 325 | { 326 | for(j = 0; j < 5; j++) 327 | printf("%d ",arr[i][j]); 328 | printf("\n"); 329 | } 330 | 331 | return 0; 332 | } 333 | ``` 334 | * The output will be: 335 | 336 | 1 2 3 4 5
337 | 6 7 8 9 10
338 | 11 12 13 14 15 339 | 340 | * Let's look inside the each element of the array: 341 | 342 | arr[0][0] : 1
343 | arr[0][1] : 2
344 | .
345 | .
346 | .
347 | arr[1][3] : 9
348 | .
349 | .
350 | .
351 | arr[2][4] : 15
352 | 353 | 354 |
355 | 356 | 357 | ## Multidimensional Arrays with Functions 358 | 359 | * When defining a function that takes a multidimensional array as an argument, we need to specify the size of each dimension other than the first one. 360 | 361 | ### Example: 362 | 363 | * The following example shows a function named *sumIndices* which takes a 2-dim array and the sizes of its dimensions as arguments, and then fills the array by assigning the element in the i'th row and the j'th column with the sum of i and j. 364 | 365 | ```c 366 | #include 367 | 368 | void sumIndices(int arr[][5], int N, int M) 369 | { 370 | int i, j; 371 | 372 | for(i = 0; i < N; i++) 373 | { 374 | for(j = 0; j < M; j++) 375 | arr[i][j] = i+j; 376 | } 377 | } 378 | int main() 379 | { 380 | int arr[3][5]; 381 | 382 | int i,j; 383 | 384 | 385 | sumIndices(arr,3,5); 386 | 387 | for(i = 0; i < 3; i++) 388 | { 389 | for(j = 0; j < 5; j++) 390 | printf("%d ",arr[i][j]); 391 | printf("\n"); 392 | } 393 | 394 | return 0; 395 | } 396 | ``` 397 | 398 | The output will be: 399 | 400 | 0 1 2 3 4
401 | 1 2 3 4 5
402 | 2 3 4 5 6 403 | 404 |
405 | 406 | If your multidimensional array is a variable length array, then you can take variable length multidimensional arrays as function parameters. 407 | 408 | But you should also take the array sizes as parameters before the multidimensional array. 409 | 410 | ```c 411 | #include 412 | 413 | // N and M must come before arr 414 | void sumIndices(int N, int M, int arr[N][M]) 415 | { 416 | int i, j; 417 | 418 | for(i = 0; i < N; i++) 419 | { 420 | for(j = 0; j < M; j++) 421 | arr[i][j] = i+j; 422 | } 423 | } 424 | ``` 425 | 426 |
427 | 428 | ## Questions 429 | 430 | ### Question 1 - Pairs (7th question from previous lab) 431 | 432 | Write a program which reads an integer N and an integer X from the user, then reads N integers from the user to an array. Then find all pairs in the array whose sum is equal to X. 433 | 434 | * A number can not be a pair with itself. Pairs should consist of different numbers. 435 | * You can print either (3,4) or (4,3) for one pair. 436 | * You should print all the pairs even if they are same. 437 | 438 | | INPUT | OUTPUT | 439 | |-------|-------| 440 | | 7 7
2 4 3 5 7 8 9 | (2, 5)
(4, 3) | 441 | | 10 7
2 4 3 5 6 -2 4 7 8 9 | (2, 5)
(4, 3)
(3, 4)
(-2, 9) | 442 | 443 | 444 |
445 | 446 | ### Question 2 - fix34 (A modified version of the 8th question from previous lab) 447 | 448 | Write a program which reads an integer N from the user, then reads N integers from the user to an array. Then rearrange the array so that every 3 is immeddiately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has number after it that is not a 3, and a 3 appears in the array before any 4. 449 | 450 | For this purpose, write a function called *swap* that takes an integer array, an integer that denotes the size of the array, and two integers k and l as arguments. The function is supposed to swap the k'th and l'th elements in the array. 451 | 452 | | INPUT | OUTPUT | 453 | |-------|-------| 454 | | 4
1 3 1 4 | 1 3 4 1 | 455 | | 7
1 3 1 4 4 3 1 | 1 3 4 1 1 3 4 | 456 | | 4
3 2 2 4 | 3 4 2 2 | 457 | 458 |
459 | 460 | ### Question 3 - Bubble Sort (A modified version of the 9th question from previous lab) 461 | 462 | Write a program which reads an integer N from the user, then reads N integers from the user to an array. Then sort elements of the array in ascending order. *You may want to use the same swap function in the previous question.* 463 | 464 | | INPUT | OUTPUT | 465 | |-------|-------| 466 | | 8
4 2 8 6 7 3 1 5 | 1 2 3 4 5 6 7 8 | 467 | 468 | 469 | Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. 470 | 471 | Example: 472 | 473 | First Pass: 474 | * ( **5** **1** 4 2 8 ) –> ( **1** **5** 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. 475 | * ( 1 **5** **4** 2 8 ) –> ( 1 **4** **5** 2 8 ), Swap since 5 > 4 476 | * ( 1 4 **5** **2** 8 ) –> ( 1 4 **2** **5** 8 ), Swap since 5 > 2 477 | * ( 1 4 2 **5** **8** ) –> ( 1 4 2 **5** **8** ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. 478 | 479 | Second Pass: 480 | * ( **1** **4** 2 5 8 ) –> ( **1** **4** 2 5 8 ) 481 | * ( 1 **4** **2** 5 8 ) –> ( 1 **2** **4** 5 8 ), Swap since 4 > 2 482 | * ( 1 2 **4** **5** 8 ) –> ( 1 2 **4** **5** 8 ) 483 | * ( 1 2 4 **5** **8** ) –> ( 1 2 4 **5** **8** ) 484 | 485 | Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted. 486 | 487 | Third Pass: 488 | * ( **1** **2** 4 5 8 ) –> ( **1** **2** 4 5 8 ) 489 | * ( 1 **2** **4** 5 8 ) –> ( 1 **2** **4** 5 8 ) 490 | * ( 1 2 **4** **5** 8 ) –> ( 1 2 **4** **5** 8 ) 491 | * ( 1 2 4 **5** **8** ) –> ( 1 2 4 **5** **8** ) 492 | 493 |
494 | 495 | ### Question 4 - Transpose of a Matrix 496 | 497 | Write a program which reads two integers N, M, and a 2-dimensional array with size NxM from the user and prints its transpose. 498 | 499 | | INPUT | OUTPUT | 500 | |-------|-------| 501 | | 2 3
5 2 4
1 4 0 | 5 1
2 4
4 0| 502 | 503 |
504 | 505 | ### Question 5 - 2D Arrays with Functions 506 | 507 | Write a function named **print_2d_array()** which takes the sizes of a 2-dimensional array and an array as parameters and prints it. 508 | 509 | Write a function named **read_2d_array()** which takes the sizes of a 2-dimensional array and an array as parameters and reads its content from the user. 510 | 511 |
512 | 513 | ### Question 6 - Matrix Addition 514 | 515 | Write a program that reads two integers N and M, and then reads two 2-dimensional integer arrays with the size NxM from the user. The program then performs the matrix addition on these two 2d arrays and prints the result. 516 | 517 | | INPUT | OUTPUT | 518 | |-------|-------| 519 | |2 4

1 1 1 2
1 2 3 4

6 6 6 5
6 5 4 3|7 7 7 7
7 7 7 7| 520 | 521 |
522 | 523 | ### Question 7 - Diagonal Sum 524 | 525 | Write a program to find sum of left and right diagonals of a 2-dimensional array. The program should first read an integer N and then a 2d integer array of size NxN from the user. 526 | 527 | | INPUT | OUTPUT | 528 | |-------|-------| 529 | |3
1 6 10
5 2 3
6 80 6|The sum of the left diagonal: 9
The sum of the right diagonal: 18 | 530 | -------------------------------------------------------------------------------- /Week_10/figures/W11_01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_10/figures/W11_01.png -------------------------------------------------------------------------------- /Week_10/figures/W11_02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_10/figures/W11_02.png -------------------------------------------------------------------------------- /Week_11/Q1_rook_pawns.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int abs(int x){ 4 | if(x < 0) 5 | return -x; 6 | return x; 7 | } 8 | 9 | int main() { 10 | int R=8, C=8; 11 | int i, j; 12 | int table[10][10]; 13 | 14 | // read the chess table 15 | for(i = 0 ; i < R ; i++) 16 | for(j = 0 ; j < C ; j++) 17 | scanf("%d", &table[i][j]); 18 | 19 | // print the chess table to be sure that you have read it correctly. 20 | // printf("\n"); 21 | // for(i = 0 ; i < R ; i++){ 22 | // for(j = 0 ; j < C ; j++){ 23 | // printf("%d ", table[i][j]); 24 | // } 25 | // printf("\n"); 26 | // } 27 | 28 | int rookR, rookC; 29 | for(i = 0 ; i < R ; i++){ 30 | for(j = 0 ; j < C ; j++){ 31 | if(table[i][j] == 8){ 32 | rookR = i; 33 | rookC = j; 34 | } 35 | } 36 | } 37 | 38 | int pawnDist = 100, pawnR, pawnC; 39 | for(i = 0 ; i < R ; i++){ 40 | if(table[i][rookC] == 1 && abs(i-rookR) < pawnDist ){ 41 | pawnDist = abs(i-rookR); 42 | pawnR = i; 43 | pawnC = rookC; 44 | } 45 | } 46 | for(j = 0 ; j < C ; j++){ 47 | if(table[rookR][j] == 1 && abs(j-rookC) < pawnDist ){ 48 | pawnDist = abs(j-rookC); 49 | pawnR = rookR; 50 | pawnC = j; 51 | } 52 | } 53 | 54 | table[rookR][rookC] = 0; 55 | table[pawnR][pawnC] = 8; 56 | 57 | printf("\n"); 58 | for(i = 0 ; i < R ; i++){ 59 | for(j = 0 ; j < C ; j++){ 60 | printf("%d ", table[i][j]); 61 | } 62 | printf("\n"); 63 | } 64 | 65 | return 0; 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /Week_11/Q2_mystrcmp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int mystrcmp(char s1[], char s2[]){ 5 | int i; 6 | for(i = 0 ; i < strlen(s1)+1 && i < strlen(s2)+1 ; i++) 7 | if(s1[i] - s2[i] != 0) 8 | return s1[i]-s2[i]; 9 | 10 | return 0; 11 | } 12 | 13 | int main() 14 | { 15 | int i; 16 | char s1[100], s2[100]; 17 | fgets(s1, 100, stdin); 18 | fgets(s2, 100, stdin); 19 | 20 | printf("mystrcmp(s1,s2) --> %d ?==? %d <-- strcmp(s1,s2)", mystrcmp(s1,s2), strcmp(s1,s2)); 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Week_11/Q3_getmeout.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void normalizeString(char s[]){ 5 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 6 | s[strlen(s)-2] = '\0'; 7 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 8 | s[strlen(s)-1] = '\0'; 9 | } 10 | 11 | int main() 12 | { 13 | int i; 14 | char sent[100], word[100]; 15 | fgets(sent,100,stdin); 16 | fgets(word,100,stdin); 17 | 18 | // This part ensures that strings do not have \n or \r at the end before \0. 19 | normalizeString(sent); 20 | normalizeString(word); 21 | 22 | for(i = 0 ; i < strlen(sent) ; i++){ 23 | if(strncmp(sent+i, word, strlen(word)) == 0){ 24 | char temp[100]; 25 | strcpy(temp, sent+i+strlen(word)+1); 26 | strcpy(sent+i, temp); 27 | break; 28 | } 29 | } 30 | 31 | puts(sent); 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Week_11/Q4_find_me.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void normalizeString(char s[]){ 5 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 6 | s[strlen(s)-2] = '\0'; 7 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 8 | s[strlen(s)-1] = '\0'; 9 | } 10 | 11 | void copyWord(char sent[], int x, char word[]){ 12 | int i, j; 13 | 14 | int spaceCtr = 1; 15 | for(i = 0 ; spaceCtr < x ; i++){ 16 | if(sent[i] == ' ') 17 | spaceCtr++; 18 | } 19 | 20 | int word_ctr = 0; 21 | for(j = i ; sent[j] != ' ' && sent[j] != '\0' ; j++) 22 | word[word_ctr++] = sent[j]; 23 | 24 | word[word_ctr] = '\0'; 25 | 26 | } 27 | 28 | int main(){ 29 | int i; 30 | char sent[100], word[100]; 31 | int x; 32 | 33 | fgets(sent, 100, stdin); 34 | normalizeString(sent); 35 | 36 | scanf("%d", &x); 37 | 38 | copyWord(sent, x, word); 39 | puts(word); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Week_11/README.md: -------------------------------------------------------------------------------- 1 | # Week 11 Notes 2 | 3 | ### Warm Up Question 1 - Rook and Pawns (Lab9 Q2) 4 | Write a program which reads a chessboard as an 8x8 matrix from the user. In the chessboard, there will be one rook (kale) represented by integer: 8, and multiple pawns (piyonlar) represented by integer: 1. The rest will be empty: 0. 5 | 6 | The rook will capture the closest possible pawn. Your program should print the chessboard to the screen after the capture. 7 | 8 | ``` 9 | Input Output 10 | 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 11 | 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 12 | 0 0 8 0 0 0 0 1 0 0 0 0 0 0 0 1 13 | 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 14 | 0 0 1 0 0 0 0 0 0 0 8 0 0 0 0 0 15 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 17 | 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 | ``` 19 | 20 | ## Strings 21 | 22 | * character array. `char str[101];` 23 | * ends with the null character (`'\0'`). 24 | * `char str[] = "Hello";` = `char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};` 25 | 26 | **Single quotes for characters, double quotes for strings!** 27 | 28 | * No need to track size of it. (Because it ends with null) 29 | 30 | * ```c 31 | char str[] = "Hello World!"; 32 | for(int i=0; str[i] != '\0'; i++) 33 | printf("%c-", str[i]); 34 | ``` 35 | 36 | ### Output 37 | 38 | * `printf("%s\n", str);` 39 | 40 | * `puts(str);` 41 | 42 | ### Input 43 | 44 | * `char str[101];` 45 | 46 | * `scanf("%s", str); // Reads until space` 47 | 48 | * `gets(str); // Reads until enter` 49 | 50 | * **Safer**: `fgets(str, 21, stdin); // Reads until enter or specified numbers of chars` 51 | 52 | ### Passing strings to functions 53 | * Same as arrays: pass by reference. 54 | * No need to pass the size. 55 | 56 | #### Reading Problems - String Normalization 57 | When reading a string from user with `gets()` or `fgets()`, `\r` or `\n` characters can be appended at the end of the string before `\0`. It is always good to check your string after reading it and get rid of extra characters. 58 | 59 | ```c 60 | void normalizeString(char s[]){ 61 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 62 | s[strlen(s)-2] = '\0'; 63 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 64 | s[strlen(s)-1] = '\0'; 65 | } 66 | ``` 67 | 68 | ### Reminder about Array-Pointer relation 69 | 70 | ```c 71 | int main() 72 | { 73 | int myArr[5] = {1, 5, 6, 2, 2}; 74 | int i; 75 | 76 | for(i = 0; i < 5; i++) 77 | printf("%d ", *(myArr+i)); // Output: 1 5 6 2 2 78 | 79 | return 0; 80 | } 81 | ``` 82 |
83 | 84 | ![Array-Pointer](figures/array_pointer.png) 85 | 86 | 87 | ### Built-in string library 88 | * **`#include `** 89 | * **`strlen()`: Length of a string** 90 | * `size_t strlen(const char *str);` 91 | * The size of a string, not counting the terminating zero '\0'. 92 | * Example 1: 93 | ```c 94 | int main(){ 95 | int i; 96 | char s1[100] = "01234 56789"; 97 | 98 | printf("strlen(s1): %d\n", strlen(s1)); // strlen(s1): 11 99 | 100 | 1[3] = '\0'; 101 | printf("strlen(s1): %d\n", strlen(s1)); // strlen(s1): 3 102 | 103 | for(i = 0 ; i < 12 ; i++) 104 | printf("%c-", s1[i]); // 0-1-2- -4- -5-6-7-8-9- - 105 | // output is actually: 0-1-2-\0-4- -5-6-7-8-9-\0- 106 | 107 | return 0; 108 | } 109 | ``` 110 |
111 | 112 | * **`strcpy()` and `strncpy()`: Copy strings** 113 | * `char *strcpy(char *s1, const char *s2); ` 114 | * `char *strncpy(char *s1, const char *s2, size_t n)` 115 | * The destination should be large enough! 116 | * `strcpy()` copies and pastes null (`\0`) character. But `strncpy()` does not automaticly puts a null (`\0`) character at the end of the pasted part. You should be aware of that. (see Example 2 and 3) 117 | * Example 1: 118 | ```c 119 | int main(){ 120 | int i; 121 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 122 | 123 | strcpy(s1, s2); 124 | 125 | printf("s1: %s\n", s1); // s1: abc defg 126 | printf("s2: %s\n", s2); // s2: abc defg 127 | 128 | printf("\ns1: "); 129 | for(i = 0 ; i < 12 ; i++) 130 | printf("%c-", s1[i]); // s1: a-b-c- -d-e-f-g-\0-d-e-f-g-\0-8-9-\0- 131 | 132 | return 0; 133 | } 134 | ``` 135 |
136 | * Example 2: 137 | ```c 138 | int main(){ 139 | int i; 140 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 141 | 142 | strcpy(s1+2, s2+5); 143 | 144 | printf("s1: %s\n", s1); // s1: 01efg 145 | 146 | for(i = 0 ; i < 12 ; i++) 147 | printf("%c-", s1[i]); //0-1-e-f-g-\0-5-6-7-8-9-\0- 148 | 149 | return 0; 150 | } 151 | ``` 152 | * Example 3: 153 | ```c 154 | int main(){ 155 | int i; 156 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 157 | 158 | strncpy(s1+2, s2+5, 2); 159 | 160 | printf("s1: %s\n", s1); // s1: 01ef4 56789 161 | 162 | for(i = 0 ; i < 12 ; i++) 163 | printf("%c-", s1[i]); // 0-1-e-f-4- -5-6-7-8-9-\0- 164 | 165 | return 0; 166 | } 167 | ``` 168 |
169 | 170 | * **`strcmp()` and `strncmp()`: Compare strings** 171 | * `int strcmp(const char *s1, const char *s2);` 172 | * `int strncmp(const char *s1, const char *s2, size_t n);` 173 | * Returns `s1[i] - s2[i]` where the $i^{th}$ character is the first character that is not same except null. 174 | * In other words, takes difference of each character in `s1` and `s2` respectively (`diff = s1[i] - s2[i]`). If all same returns `0`, else returns `diff`. 175 | * Returns `< 0` if `s1` comes before `s2` alphabetically. 176 | * Returns `0` if `s1` is identical to `s2` alphabetically. 177 | * Returns `> 0` if `s1` comes after `s2` alphabetically. 178 | * Example 1: 179 | ```c 180 | int main(){ 181 | char s1[100] = "abc fefg", s2[100] = "abc defg"; 182 | 183 | printf("%d", strcmp(s1, s2)); // 2 -> ('f' - 'd') 184 | 185 | return 0; 186 | } 187 | ``` 188 |
189 | * Example 2: 190 | ```c 191 | int main(){ 192 | char s1[100] = "a1234", s2[100] = "b11234"; 193 | 194 | printf("%d\n", strcmp(s1, s2)); // -1 -> ('a' - 'b') 195 | printf("%d\n", strcmp(s1+1, s2+2)); // 0 -> Same 196 | 197 | return 0; 198 | } 199 | ``` 200 |
201 | * Example 3: 202 | ```c 203 | int main(){ 204 | char s1[100] = "9HELLO", s2[100] = "HELLOL"; 205 | 206 | printf("%d\n", strcmp(s1, s2)); // -15 -> ('9' - 'H') 207 | printf("%d\n", strcmp(s1+1, s2)); // -76 -> ('\0' - 'L') 208 | printf("%d\n", strncmp(s1+1, s2, 5)); // 0 -> Same 209 | 210 | return 0; 211 | } 212 | ``` 213 |
214 | * **`strcat()`, `strncat()`: Append a string to the end of another** 215 | * `char *strcat(char *s1, const char *s2);` 216 | * `char *strncat(char *s1, const char *s2, size_t n);` 217 | * The destination should be large enough! 218 | * Example 1: 219 | ```c 220 | int main(){ 221 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 222 | 223 | strcat(s1, s2); 224 | 225 | printf("s1: %s\n", s1); // s1: 01234 56789abc defg 226 | printf("s2: %s\n", s2); // s2: abc defg 227 | 228 | return 0; 229 | } 230 | ``` 231 |
232 | * Example 2: 233 | ```c 234 | int main(){ 235 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 236 | 237 | strcat(s1, s2+5); 238 | 239 | printf("s1: %s\n", s1); // s1: 01234 56789efg 240 | 241 | return 0; 242 | } 243 | ``` 244 |
245 | * Example 3: 246 | ```c 247 | int main(){ 248 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 249 | 250 | strncat(s1, s2+5, 1); 251 | 252 | printf("s1: %s\n", s1); // s1: 01234 56789e 253 | 254 | return 0; 255 | } 256 | ``` 257 |
258 | * Side Note: `strcat(s1+k, s2)` generally does the same thing with `strcat(s1, s2)` because it doesn't matter where we start for the destination string. `strcat()` appends the second string to the **end** of the second string. 259 | 260 |
261 | 262 | ### Question 2 - MyStrcmp 263 | 264 | Write a function that takes two strings as argument and compares them. The function should perform what strcmp function from string.h library. Validate your function by using strcmp along with your function. 265 | 266 | | INPUT | OUTPUT | 267 | |-------|-------| 268 | | abz abc | 23 | 269 | 270 |
271 | 272 | 273 | ### Question 3 - Get Me Out Of Here 274 | 275 | Write a program which reads a sentence as a string, and a word as a string. Then the program should exclude this word from the sentence if the sentence contains the word, then print the new version to the screen. Assume no punctuation will occur, and every letter is lower case. 276 | 277 | | INPUT | OUTPUT | 278 | |-------|-------| 279 | | you are already dead
already | you are dead | 280 | 281 |
282 | 283 | ### Question 4 - Find Me 284 | 285 | Write a function which takes a sentence as a string (char array) with size at most 150, and one integer (x), and another empty string with size at most 30. The function should put the x.th word of the sentence to the empty string. 286 | 287 | In the main program read a sentence, and an integer from the user, then call the function and print the resulting word to the screen. Assume every letter is lower case, and no punctuation will occur, and the sentence will have enough words to return the requested one. 288 | 289 | | INPUT | OUTPUT | 290 | |-------|-------| 291 | | omae wa mou shindeiru
3 | mou | -------------------------------------------------------------------------------- /Week_11/figures/array_pointer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/suyunu/c-notes/dc34d65be35432eece50ebac39e240e1bfc3f237/Week_11/figures/array_pointer.png -------------------------------------------------------------------------------- /Week_12/Q1_put_my_words.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void normalizeString(char s[]){ 5 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 6 | s[strlen(s)-2] = '\0'; 7 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 8 | s[strlen(s)-1] = '\0'; 9 | } 10 | 11 | int main() 12 | { 13 | int n; 14 | char sentence[1001], word[21]; 15 | 16 | fgets(sentence, 1000, stdin); 17 | scanf("%d %s", &n, word); 18 | 19 | normalizeString(sentence); 20 | normalizeString(word); 21 | 22 | int i; 23 | int spaceCtr = 1; 24 | for(i = 0 ; spaceCtr < n && i < strlen(sentence) ; i++){ 25 | if(sentence[i] == ' ') 26 | spaceCtr++; 27 | } 28 | 29 | // last word 30 | if(spaceCtr < n){ 31 | sentence[strlen(sentence)+1] = '\0'; 32 | sentence[strlen(sentence)] = ' '; 33 | strcat(sentence, word); 34 | } 35 | else{ 36 | char temp[1000] = ""; 37 | strcpy(temp, sentence+i); 38 | strcpy(sentence+i, word); 39 | sentence[strlen(sentence)+1] = '\0'; 40 | sentence[strlen(sentence)] = ' '; 41 | strcat(sentence, temp); 42 | } 43 | 44 | printf("%s Cool", sentence); 45 | 46 | return 0; 47 | } 48 | 49 | -------------------------------------------------------------------------------- /Week_12/Q2_sorted_sentence.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void normalizeString(char s[]){ 5 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 6 | s[strlen(s)-2] = '\0'; 7 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 8 | s[strlen(s)-1] = '\0'; 9 | } 10 | 11 | int main() { 12 | int i, j; 13 | char str[1000]; 14 | int words_start_ind[100]; // starting indices of each word 15 | int words_len[100]; // length of each word 16 | int words_count = 0; // number of words 17 | 18 | fgets(str,150,stdin); 19 | normalizeString(str); 20 | 21 | // We think like every word has an id starting from 0 to words_count-1 in the order of appearance in the string. 22 | 23 | words_start_ind[0] = 0; 24 | for(i = 0 ; i <= strlen(str) ; i++){ 25 | if(str[i] == ' ' || str[i] == '\0'){ 26 | words_len[words_count] = i - words_start_ind[words_count]; 27 | words_start_ind[words_count+1] = i+1; 28 | words_count++; 29 | } 30 | } 31 | 32 | // Sorting will happen on this array using word ids 33 | int sort_list[100]; 34 | for(i = 0 ; i < words_count ; i++) 35 | sort_list[i] = i; 36 | 37 | 38 | // This part is actually Bubble Sort. 39 | // Instead of comparing numbers in sort_list, 40 | // We compare the words that has the corresponding id from the sort_list. 41 | for(i = 0 ; i < words_count ; i++){ 42 | for(j = 0 ; j < words_count-1 ; j++){ 43 | int w1_ind = sort_list[j], w2_ind = sort_list[j+1]; // get word ids from the sort_list 44 | 45 | // copy the words that we will compare from the main string (str) to temp strings (s1, s2) 46 | char s1[100], s2[100]; 47 | strncpy(s1, str+words_start_ind[w1_ind], words_len[w1_ind]); 48 | s1[words_len[w1_ind]] = '\0'; // Do not forget to put \0 after copying 49 | strncpy(s2, str+words_start_ind[w2_ind], words_len[w2_ind]); 50 | s2[words_len[w2_ind]] = '\0'; // Do not forget to put \0 after copying 51 | 52 | // Compare the strings and make the adjustment on the sort_list 53 | if(strcmp(s1, s2) > 0){ 54 | int temp = sort_list[j]; 55 | sort_list[j] = sort_list[j+1]; 56 | sort_list[j+1] = temp; 57 | } 58 | } 59 | } 60 | 61 | 62 | // print the words in the order of they appear in sort_list 63 | for(i = 0 ; i < words_count ; i++){ 64 | int w_ind = sort_list[i]; // get word id from the sort_list 65 | char s[100]; 66 | 67 | strncpy(s, str+words_start_ind[w_ind], words_len[w_ind]); // Get the word from the main string 68 | s[words_len[w_ind]] = '\0'; // Do not forget to put \0 after copying 69 | printf("%s ", s); 70 | } 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /Week_12/Q3_freq_chars.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void normalizeString(char s[]){ 5 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 6 | s[strlen(s)-2] = '\0'; 7 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 8 | s[strlen(s)-1] = '\0'; 9 | } 10 | 11 | int main() 12 | { 13 | int i; 14 | char str[1001]; 15 | 16 | fgets(str, 1000, stdin); 17 | normalizeString(str); 18 | 19 | int occurences[128] = {0}; 20 | for(i = 0 ; i < strlen(str) ; i++) 21 | occurences[(int)str[i]]++; 22 | 23 | char max_char = str[0]; 24 | int max_occur = 0; 25 | 26 | for(i = 0 ; i < 128 ; i++){ 27 | if(i != ' '){ 28 | if( (occurences[i] > max_occur) || (occurences[i] == max_occur && i <= max_char) ){ 29 | max_char = i; 30 | max_occur = occurences[i]; 31 | } 32 | } 33 | } 34 | 35 | printf("%c %d", max_char, max_occur); 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Week_12/Q4_largerst_sub_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i, j; 6 | 7 | int N; 8 | scanf("%d", &N); 9 | 10 | int arr[N]; 11 | for(i = 0 ; i < N ; i++) 12 | scanf("%d", &arr[i]); 13 | 14 | int maxSum = arr[0]; 15 | int maxSumIndex = 0, maxSumSize = 0; 16 | 17 | for(i = 0 ; i < N ; i++){ 18 | int sum = 0; 19 | for(j = i ; j < N ; j++){ 20 | sum += arr[j]; 21 | if( (sum > maxSum) || (sum == maxSum && (j-i+1) >= maxSumSize) ){ 22 | maxSum = sum; 23 | maxSumIndex = i; 24 | maxSumSize = j-i+1; 25 | } 26 | } 27 | } 28 | 29 | printf("%d %d %d", maxSum, maxSumIndex, maxSumSize); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Week_12/Q5_shift_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void rightShiftA(int N, int arr[N], int T){ 4 | int i; 5 | int temp_arr[N]; 6 | 7 | T %= N; 8 | for(i = 0 ; i < N ; i++) 9 | temp_arr[(i+T)%N] = arr[i]; 10 | 11 | for(i = 0 ; i < N ; i++) 12 | arr[i] = temp_arr[i]; 13 | } 14 | 15 | 16 | int main() 17 | { 18 | int i; 19 | 20 | int N; 21 | scanf("%d", &N); 22 | 23 | int arr[N]; 24 | for(i = 0 ; i < N ; i++) 25 | scanf("%d", &arr[i]); 26 | 27 | int T; 28 | scanf("%d", &T); 29 | 30 | rightShiftA(N, arr, T); 31 | 32 | for(i = 0 ; i < N ; i++) 33 | printf("%d ", arr[i]); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Week_12/Q6_shift_matrix.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void rightShiftM(int N, int M, int arr[N][M], int T){ 4 | int i; 5 | int temp_arr[N][M]; 6 | 7 | T %= (N*M); 8 | for(i = 0 ; i < N*M ; i++) 9 | temp_arr[0][(i+T)%(N*M)] = arr[0][i]; 10 | 11 | for(i = 0 ; i < N*M ; i++) 12 | arr[0][i] = temp_arr[0][i]; 13 | } 14 | 15 | 16 | int main() 17 | { 18 | int i, j; 19 | 20 | int N, M; 21 | scanf("%d %d", &N, &M); 22 | 23 | int arr[N][M]; 24 | for(i = 0 ; i < N ; i++) 25 | for(j = 0 ; j < M ; j++) 26 | scanf("%d", &arr[i][j]); 27 | 28 | int T; 29 | scanf("%d", &T); 30 | 31 | rightShiftM(N, M, arr, T); 32 | 33 | for(i = 0 ; i < N ; i++){ 34 | for(j = 0 ; j < M ; j++){ 35 | printf("%d ", arr[i][j]); 36 | } 37 | printf("\n"); 38 | } 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Week_12/Q7_interstellar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int R=4, C=4; 5 | int i, j, k, l; 6 | char arr[50][50] = {{0}}; // initialize the matrix with 0's 7 | 8 | for(i = 1 ; i <= R ; i++){ 9 | scanf("%s", arr[i]+1); // read the input line by line and put it inside 0's 10 | } 11 | 12 | for(i = 0 ; i <= R+1 ; i++){ 13 | for(j = 0 ; j <= C+1 ; j++){ 14 | printf("%c", arr[i][j]); // or print with %d 15 | } 16 | printf("\n"); 17 | } 18 | // resulting matrix -> now we don't have to check for borders. 19 | /* 20 | * 000000 21 | * 0xxxx0 22 | * 0xxxx0 23 | * 0xxxx0 24 | * 0xxxx0 25 | * 000000 26 | * 27 | */ 28 | 29 | char starChar = '*'; 30 | int starCharCounter = 0; 31 | 32 | for(i = 1 ; i <= R ; i++){ 33 | for(j = 1 ; j <= C ; j++){ 34 | if(arr[i][j] >= 'a' && arr[i][j] <= 'z'){ 35 | int starCounter = 0; 36 | for(k = -1 ; k <= 1 ; k++){ 37 | for(l = -1 ; l <= 1 ; l++){ 38 | if(arr[i+k][j+l] == '*'){ 39 | starCounter++; 40 | } 41 | } 42 | } 43 | if(starCounter == starCharCounter && arr[i][j] < starChar){ 44 | starChar = arr[i][j]; 45 | starCharCounter = starCounter; 46 | } 47 | if(starCounter > starCharCounter){ 48 | starChar = arr[i][j]; 49 | starCharCounter = starCounter; 50 | } 51 | } 52 | } 53 | } 54 | 55 | printf("%c %d", starChar, starCharCounter); 56 | 57 | return 0; 58 | } 59 | 60 | 61 | -------------------------------------------------------------------------------- /Week_12/README.md: -------------------------------------------------------------------------------- 1 | # Week 12 Notes 2 | 3 | ### Question 1 - Put My Words (String) 4 | 5 | Write a program which reads a **sentence (string)** a **word (string)** and a **positive integer number (int)**. Insert the word to the sentence as the given numbered word. Then modified sentence and the word **Cool** will be printed next to each other separated with one space. 6 | 7 | A sentence consists of words separated by spaces. There will be at least one word in a sentence. There will be exactly one space between each word in a sentence. A word consists of only letters (both lowercase and uppercase is possible). A sentence cannot be longer than 1000 characters. A word cannot be longer than 20 letters. The number can't be larger than the number of words in the sentence + 1. 8 | 9 | **Reminder:** A string is a char array that has \0 character at the end. 10 | 11 | **Reminder 2:** There shouldn’t be spaces or new lines at the end or the beginning of the sentence. 12 | 13 | 14 | | INPUT | OUTPUT | 15 | |-------|-------| 16 | | is my life now
1
this| this is my life now Cool | 17 | | WHeN LiFe GiVeS You MaKe LeMoNaDe
5
LeMoNs| WHeN LiFe GiVeS You LeMoNs MaKe LeMoNaDe Cool | 18 | | dont Wait for
4
Opportunity| dont Wait for Opportunity Cool | 19 | 20 | 21 | ```c 22 | #include 23 | #include 24 | 25 | void normalizeString(char s[]){ 26 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 27 | s[strlen(s)-2] = '\0'; 28 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 29 | s[strlen(s)-1] = '\0'; 30 | } 31 | 32 | int main() 33 | { 34 | int n; 35 | char sentence[1001], word[21]; 36 | 37 | fgets(sentence, 1000, stdin); 38 | scanf("%d %s", &n, word); 39 | 40 | normalizeString(sentence); 41 | normalizeString(word); 42 | 43 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 44 | 45 | 46 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 47 | 48 | printf("%s Cool", sentence); 49 | 50 | return 0; 51 | } 52 | ``` 53 | 54 |
55 | 56 | ### Question 2 - Sorted Sentence (String) 57 | 58 | Write a program which reads a sentence from the user and prints the words in the sorted order (ascending). Assume initial string has maximum 100 characters, the length of each word is at most 10 and all characters are lower case letters. 59 | 60 | Hint: You can use bubble sort algorithm. 61 | 62 | | INPUT | OUTPUT | 63 | |-------|-------| 64 | | is this a jojo reference | a is jojo reference this | 65 | 66 |
67 | 68 | ### Question 3 - Frequentist Character (String) 69 | 70 | Write a program which reads a string and finds the maximum occurring character of that string. Then print the 71 | maximum occurring character and the number of occurrences. If two characters have same maximum count, print smallest character in terms of ASCII value. Spaces are not counted. 72 | 73 | | INPUT | OUTPUT | 74 | |-------|-------| 75 | | Is this a pigeon? | i 2 | 76 | 77 |
78 | 79 | ### Question 4 - Largest Sub Array (Array) 80 | 81 | Write a program which reads an integer N from the user, then reads N integers from the user. Find the largest sum of contiguous subarray of the array. Print the sum, starting index and the size of the subarray. Assume N is smaller than 50. 82 | 83 | If there is a tie in sum, accept the subarray with the bigger size. If sizes are also equal, accept the subarray with the greater starting index. 84 | 85 | | INPUT | OUTPUT | 86 | |-------|-------| 87 | | 9
8 3 8 -5 4 3 -4 -3 5 | 21 0 6 | 88 | | 6
3 5 -10 4 -2 6 -1 | 8 3 3 | 89 | 90 |
91 | 92 | 93 | ### Question 5 - Shift the Array (Array) 94 | 95 | Write a function called **rightShiftA()** that takes the size of the array (N), an integer array (arr) and an integer variable (T) as parameters. The function right shifts the array T times. 96 | 97 | Write a proper main function to see the effect of **rightShiftA()**. 98 | 99 | | INPUT | OUTPUT | 100 | |-------|-------| 101 | | 5
1 2 3 4 5
3 | 3 4 5 1 2 | 102 | | 4
1 2 3 4
10 | 3 4 1 2 | 103 | 104 |
105 | 106 | ### Question 6 - Shift the Matrix (2D Array) 107 | 108 | Write a function called **rightShiftM()** that takes two integers as the sizes of the 2D array (N and M), a integer 2D array (arr) and an integer variable (T) as parameters. The function right shifts the 2D array T times. 109 | 110 | Write a proper main function to see the effect of **rightShiftM()**. 111 | 112 | | INPUT | OUTPUT | 113 | |-------|-------| 114 | | 2 3
1 2 3
4 5 6
4 | 3 4 5
6 1 2 | 115 | | 2 3
1 2 3
4 5 6
8 | 5 6 1
2 3 4 | 116 | 117 |
118 | 119 | ### Question 7 - Interstellar (2D Array) 120 | 121 | Given a 4x4 matrix consisting of * and lowercase English alphabets, the task is to find the character which has the maximum number of * around it (including diagonal elements too). If two characters have same maximum count, print lexicographically smallest character. 122 | 123 | ``` 124 | Input Output 125 | b*** a 126 | **c* 127 | *a** 128 | ***d 129 | 130 | Input Output 131 | *r** f 132 | maz* 133 | l*fk 134 | ***d 135 | ``` 136 | 137 | 138 | -------------------------------------------------------------------------------- /Week_13/Q1_distance.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct{ 5 | float x; 6 | float y; 7 | }Point_t; 8 | 9 | float distance(Point_t p1, Point_t p2){ 10 | return sqrt( pow(p1.x-p2.x, 2) + pow(p1.y-p2.y, 2) ); 11 | } 12 | 13 | 14 | int main(){ 15 | Point_t p1, p2; 16 | 17 | scanf("%f %f", &p1.x, &p1.y); 18 | scanf("%f %f", &p2.x, &p2.y); 19 | 20 | printf("%.2f", distance(p1, p2)); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Week_13/Q2_extreme_points.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct{ 5 | float x; 6 | float y; 7 | }Point_t; 8 | 9 | float distance(Point_t p1, Point_t p2){ 10 | return sqrt( pow(p1.x-p2.x, 2) + pow(p1.y-p2.y, 2) ); 11 | } 12 | 13 | 14 | int main(){ 15 | int i, j, N; 16 | scanf("%d", &N); 17 | 18 | Point_t pArr[N]; 19 | for(i = 0 ; i < N ; i++) 20 | scanf("%f %f", &pArr[i].x, &pArr[i].y); 21 | 22 | Point_t p1 = pArr[0], p2 = pArr[1]; 23 | float max_dist = distance(p1, p2); 24 | 25 | for(i = 0 ; i < N-1 ; i++){ 26 | for(j = i+1 ; j < N ; j++){ 27 | if(distance(pArr[i], pArr[j]) > max_dist){ 28 | max_dist = distance(pArr[i], pArr[j]); 29 | p1 = pArr[i]; 30 | p2 = pArr[j]; 31 | } 32 | } 33 | } 34 | 35 | printf("%.2f %.2f\n", p1.x, p1.y); 36 | printf("%.2f %.2f\n", p2.x, p2.y); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /Week_13/Q3_moving_rectangles.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct{ 4 | float x; 5 | float y; 6 | }Point_t; 7 | 8 | typedef struct{ 9 | Point_t ll, ur; 10 | }Rect_t; 11 | 12 | void move(Rect_t *r, int moveX, int moveY){ 13 | r->ll.x += moveX; 14 | r->ll.y += moveY; 15 | 16 | r->ur.x += moveX; 17 | r->ur.y += moveY; 18 | } 19 | 20 | int main(){ 21 | Rect_t r; 22 | scanf("%f %f %f %f", &r.ll.x, &r.ll.y, &r.ur.x, &r.ur.y); 23 | 24 | int moveX, moveY; 25 | scanf("%d %d", &moveX, &moveY); 26 | 27 | move(&r, moveX, moveY); 28 | 29 | printf("%.2f %.2f %.2f %.2f\n", r.ll.x, r.ll.y, r.ur.x, r.ur.y); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Week_13/Q4_letter_grades.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Fill the definition of the struct. 4 | typedef struct{ 5 | int student_id; 6 | float mt_grade; 7 | float final_grade; 8 | char letter_grade; 9 | } Student; 10 | 11 | void set_grade(Student *p){ 12 | float score; 13 | score = p->mt_grade * 0.4 + p->final_grade * 0.6; 14 | 15 | if(score > 75) 16 | p->letter_grade = 'A'; 17 | else if(score > 50) 18 | p->letter_grade = 'B'; 19 | else if(score > 25) 20 | p->letter_grade = 'C'; 21 | else 22 | p->letter_grade = 'F'; 23 | } 24 | 25 | int main(){ 26 | int N; 27 | scanf("%d", &N); 28 | Student cmpe[N]; 29 | int i; 30 | for(i=0; i < N; i++){ 31 | scanf("%f %f", &cmpe[i].mt_grade, &cmpe[i].final_grade); 32 | cmpe[i].student_id = 1000 + i; 33 | // Call set grade function 34 | set_grade(&cmpe[i]); 35 | } 36 | 37 | for(i = 0; i < N; i++) 38 | printf("%d %c\n", cmpe[i].student_id, cmpe[i].letter_grade); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Week_13/Q5_flights.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct{ 5 | int flight_no; 6 | char org[4]; 7 | char dst[4]; 8 | int start; 9 | int arrival; 10 | }Flight_t; 11 | 12 | /* 13 | * sorts flights according to their 14 | * starting time and the originating airport code. 15 | * 16 | * if f1 > f2 return 1 17 | * if f1 == f2 return 0 18 | * if f1 < f2 return -1 19 | */ 20 | int compare_flights(Flight_t *f1, Flight_t *f2){ 21 | if(f1->start > f2->start) 22 | return 1; 23 | else if(f1->start < f2->start) 24 | return -1; 25 | else{ 26 | if(strcmp(f1->org, f2->org) > 0) 27 | return 1; 28 | else if(strcmp(f1->org, f2->org) < 0) 29 | return -1; 30 | else 31 | return 0; 32 | } 33 | } 34 | 35 | int main(){ 36 | int i, j; 37 | int N; 38 | scanf("%d", &N); 39 | 40 | Flight_t flights[N]; 41 | 42 | for(i = 0 ; i < N ; i++){ 43 | scanf("%d", &flights[i].flight_no); 44 | scanf("%s", flights[i].org); 45 | scanf("%s", flights[i].dst); 46 | scanf("%d", &flights[i].start); 47 | scanf("%d", &flights[i].arrival); 48 | } 49 | 50 | 51 | for(i = 0 ; i < N ; i++){ 52 | for(j = 0 ; j < N-1 ; j++){ 53 | if(compare_flights(&flights[j], &flights[j+1]) > 0){ 54 | Flight_t temp = flights[j]; 55 | flights[j] = flights[j+1]; 56 | flights[j+1] = temp; 57 | } 58 | } 59 | } 60 | 61 | 62 | for(i = 0 ; i < N ; i++){ 63 | printf("%d ", flights[i].flight_no); 64 | printf("%s ", flights[i].org); 65 | printf("%s ", flights[i].dst); 66 | printf("%d ", flights[i].start); 67 | printf("%d\n", flights[i].arrival); 68 | } 69 | 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Week_13/Q6_book_prices.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct book{ 4 | int page; 5 | int price; 6 | char name[5]; 7 | }; 8 | 9 | void sort_books(struct book bArr[], int n){ 10 | int i, j; 11 | 12 | for(i = 0 ; i < n ; i++){ 13 | for(j = 0 ; j < n-1 ; j++){ 14 | if(bArr[j].price > bArr[j+1].price){ 15 | struct book temp = bArr[j]; 16 | bArr[j] = bArr[j+1]; 17 | bArr[j+1] = temp; 18 | } 19 | } 20 | } 21 | } 22 | 23 | int main() { 24 | int i, n; 25 | scanf("%d", &n); 26 | 27 | struct book bArr[n]; 28 | for(i = 0 ; i < n ; i++) 29 | scanf("%d %s %d", &bArr[i].page, bArr[i].name, &bArr[i].price); 30 | 31 | sort_books(bArr, n); 32 | 33 | for(i = 0 ; i < n ; i++) 34 | printf("%s: %d pages (%d gold)\n", bArr[i].name, bArr[i].page, bArr[i].price); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Week_13/Q7_sbfs.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct rectangle{ 4 | float edge1; 5 | float edge2; 6 | }Rectangle; 7 | 8 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 9 | 10 | Rectangle min_rectangle_area(Rectangle r[], int N){ 11 | int i; 12 | float min_area; 13 | int ind; 14 | 15 | min_area = r[0].edge1*r[0].edge2; 16 | ind = 0; 17 | 18 | for(i = 1 ; i < N ; i++){ 19 | if(r[i].edge1*r[i].edge2 < min_area){ 20 | min_area = r[i].edge1*r[i].edge2; 21 | ind = i; 22 | } 23 | } 24 | 25 | return r[ind]; 26 | } 27 | 28 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 29 | 30 | int main(){ 31 | Rectangle r_min; 32 | int i; 33 | int N; 34 | Rectangle r[100]; 35 | 36 | scanf("%d", &N); 37 | 38 | // Reads Rectangles 39 | for(i = 0 ; i < N ; i++) 40 | scanf("%f %f", &r[i].edge1, &r[i].edge2); 41 | 42 | r_min = min_rectangle_area( 43 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 44 | r, N 45 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 46 | ); 47 | 48 | // Prints the area of the rectangle that has the smallest area 49 | printf("%.2f\n", r_min.edge1*r_min.edge2); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Week_13/README.md: -------------------------------------------------------------------------------- 1 | # Week 13 Notes 2 | 3 | ## Structures 4 | 5 | ### Question 1 - Distance 6 | 7 | Define a **struct** representing a point (x, y). 8 | 9 | * Write a function **distance** which takes two points and finds Euclidean distance between these points. Use *pow()* and *sqrt()* functions from math library *`include `* 10 | 11 | In main function, take two points from user and print the between them. 12 | 13 | | Input | Output | 14 | | -------------- | ------ | 15 | | 3 0
0 4 | 5.00 | 16 | | 11 7
3 21 | 16.12 | 17 | 18 |
19 | 20 | ### Question 2 - Extreme Points 21 | 22 | Write a program which takes an integer N and reads N points (given by their x and y coordinates) and determines the pair that is the farthest apart. 23 | 24 | *Use the point definition and distance function from the first question* 25 | 26 | 27 | | Input | Output | 28 | | -------------------------------------------- | ------------- | 29 | | 3
3 0
0 0
0 4 | 3.00 0.00
0.00 4.00 | 30 | | 5
10 5
4 8
0 8
4 5
-1 2 | 10.00 5.00
-1.00 2.00 | 31 | 32 |
33 | 34 | ### Question 3 - Moving Rectangle 35 | 36 | Define a **struct** representing a rectangle that are parallel to the axes in a Cartesian coordinate system. Represent a rectangle by its lower left and upper right endpoints using the Point you defined. 37 | 38 | Write a function **move** which takes a rectangle pointer and two integers **moveX** and **moveY** representing the movement in the x and y axes respectively and moves the rectangle. 39 | 40 | **Example**: 41 | 42 | * Rectangle: 43 | * lower left point: (2, 0) 44 | * upper right point: (8, 4) 45 | * moveX: -5 46 | * moveY: 2 47 | * Resulting Rectangle: 48 | * lower left point: (-3, 2) 49 | * upper right point: (3, 6) 50 | 51 |
52 | 53 | ### Question 4 - Letter Grades 54 | 55 | Define a **struct** representing a student having **student_id (integer), mt_grade (integer), final_grade (integer) and letter_grade (char)**. The given program takes the grades of students and creates structs for students. You are supposed to calculate and set the letter grades of students using **set_grade() function**. The grade weights are 40%, 60% respectively and letter grades are as follows: 56 | 57 | * 100-75: A 58 | * 74-50: B 59 | * 49-25: C 60 | * 24-0: F 61 | 62 | | INPUT | OUTPUT | 63 | |-------|-------| 64 | | 2
100 40
40 100| 1000 B
1001 A | 65 | | 3
45 25
100 100
0 90 | 1000 C
1001 A
1002 B | 66 | 67 | ```c 68 | #include 69 | 70 | // Fill the definition of the struct. 71 | typedef struct{ 72 | 73 | } Student; 74 | 75 | void set_grade(Student *p){ 76 | 77 | } 78 | 79 | int main(void){ 80 | int N; 81 | scanf("%d", &N); 82 | Student cmpe[N]; 83 | int i; 84 | for(i = 0; i < N; i++){ 85 | scanf("%d %d", &cmpe[i].mt_grade, &cmpe[i].final_grade); 86 | cmpe[i].student_id = 1000 + i; 87 | // Call set grade function 88 | } 89 | 90 | for(i = 0; i < N; i++) 91 | printf("%d %c\n", cmpe[i].student_id, cmpe[i].letter_grade); 92 | 93 | return 0; 94 | } 95 | ``` 96 | 97 |
98 | 99 | ### Question 5 - Flights 100 | 101 | Define a struct representing a flight which has following fields: 102 | 103 | * Flight number 104 | * Originating airport code (three characters) 105 | * Destination airport code (three characters) 106 | * Starting time 107 | * Arrival time 108 | 109 | Write a program which takes an integer N and then reads N flights. Then sorts the flights according to their starting time and the originating airport code. 110 | 111 | 112 | | Input | Output | 113 | | ------------------------------------------------------------ | ------------------------------------------------------------ | 114 | | 4
3540 SAW ADB 14 15
3440 IST ESB 13 15
4041 ANT STL 13 21
4045 GTG KPG 17 18 | 4041 ANT STL 13 21
3440 IST ESB 13 15
3540 SAW ADB 14 15
4045 GTG KPG 17 18 | 115 | 116 | *Hint 1: Write a function which reads a flight from user.* 117 | 118 | *Hint 2: Write a function for comparing two flights. It first checks the starting times and if they are equal then compares the originating airport codes. Funtion can return 1 if the first flight is greater, 0 if equal, -1 otherwise* 119 | 120 |
121 | 122 | ### Question 6 - Book Prices 123 | Define a struct named: Book. Each book should have a name with exactly 3 letters (abbreviation). Each book should also have a page count (integer), and a price (integer). 124 | 125 | Write a program which reads an integer n first, then read page counts, the names and prices of n books. 126 | 127 | Write a function which takes an array of books, and sorts them according to their prices. 128 | 129 | Using that function, your program should print the names and page counts of each book with the order of their prices. Like: 130 | 131 | 1. LOT: 528 pages (15 gold) 132 | 2. THG: 727 pages (32 gold) 133 | 3. MSB: 340 pages (120 gold) 134 | 4. etc.. 135 | 136 | Reading and printing format is up to you. 137 | 138 | | INPUT | OUTPUT | 139 | |-------|-------| 140 | | 3
727 THG 32
528 LOT 15
340 MSB 120| LOT: 528 pages (15 gold)
THG: 727 pages (32 gold)
MSB: 340 pages (120 gold) | 141 | 142 |
143 | 144 | ### Question 7 - Smaller Better Faster Stronger (2018 Fall Final Q4) 145 | Mossy Land's department of science and technology started a competition to find the rectangle with the smallest area. All the participants submitted their rectangles. Then a group of scientists calculated edge lengths of each rectangle and send them to you. Your task is to find the rectangle with the smallest area. 146 | 147 | You will write a function (min_rectangle_area) that takes two parameters (a Rectangle array and an integer) then returns a Rectangle. 148 | 149 | **Input:** 150 | * The first line of the input contains: N (1 <= N <= 100) the number of rectangles. 151 | * The next N lines contains 2 numbers that describes the edge lengths of each rectangle: edge_1 edge_2. (0 < edge_1, edge_2 < 100000) 152 | 153 | **Output:** 154 | * The area of the rectangle that has the smallest area. 155 | 156 | 157 | | INPUT | OUTPUT | 158 | |-------|-------| 159 | | 2
2 5
3 4| 10.00 | 160 | | 3
4.2 2.1
1.8 5.3
2 8.2 | 8.82 | 161 | | 8
12 684
485 684
48 542
158 48
564 12
21 188
126 441
1200 1450 | 3948.00 | 162 | | 4
1.1 2.2
2.2 3.3
1.1 3.3
1.1 2.2 | 2.42 | 163 | 164 | 165 | ```c 166 | #include 167 | 168 | typedef struct rectangle{ 169 | float edge1; 170 | float edge2; 171 | }Rectangle; 172 | 173 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 174 | 175 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 176 | 177 | int main(){ 178 | Rectangle r_min; 179 | int i; 180 | int N; 181 | Rectangle r[100]; 182 | 183 | scanf("%d", &N); 184 | 185 | // Reads Rectangles 186 | for(i = 0 ; i < N ; i++) 187 | scanf("%f %f", &r[i].edge1, &r[i].edge2); 188 | 189 | r_min = min_rectangle_area( 190 | // DO_NOT_EDIT_ANYTHING_ABOVE_THIS_LINE 191 | 192 | // DO_NOT_EDIT_ANYTHING_BELOW_THIS_LINE 193 | ); 194 | 195 | // Prints the area of the rectangle that has the smallest area 196 | printf("%.2f\n", r_min.edge1*r_min.edge2); 197 | 198 | return 0; 199 | } 200 | ``` 201 | --------------------------------------------------------------------------------