├── week11 ├── q6.png ├── gets.png ├── q4_1.png ├── 1576784076412.png ├── README.md └── Solutions.md ├── week08 ├── figures │ ├── W08-ptr1.png │ ├── W08-ptr2.png │ ├── W08-ptr3.png │ ├── Week8-Q2.png │ └── 1587302715055.png ├── Solutions.md └── README.md ├── week01 ├── figures │ ├── 1569345823541.png │ ├── 1569348346330.png │ ├── 1569348386213.png │ ├── 1569348433566.png │ ├── 1569348477633.png │ ├── 1569348487515.png │ ├── 1569348513992.png │ ├── 1569348537826.png │ ├── 1569348574235.png │ ├── 1569348604330.png │ └── 1569351507071.png └── README.md ├── week02 ├── figures │ ├── 1570300391805.png │ ├── 1570300492161.png │ ├── 1570300588514.png │ ├── 1570300636310.png │ ├── 1570300699622.png │ ├── 1570300750532.png │ ├── 1570300848422.png │ ├── 1570300852266.png │ ├── 1570300948268.png │ ├── 1570300973481.png │ ├── 1570302473710.png │ ├── 1570302539152.png │ ├── 1582270849273.png │ └── 1582270887828.png ├── Solutions.md └── README.md ├── week05 ├── figures │ ├── 1571759785622.png │ ├── 1571760221894.png │ └── 1571761472620.png ├── README.md └── Solutions.md ├── week09 ├── figures │ ├── 1588599881804.png │ ├── 1588599907830.png │ ├── 1588599935727.png │ ├── 1588600071903.png │ ├── 1588600093625.png │ ├── 1588600148394.png │ └── 1588600212155.png ├── Solutions.md └── README.md ├── README.md ├── week03 ├── Solutions.md └── README.md ├── week13 ├── Structs-Summary.md ├── README.md └── Solutions.md ├── week06 ├── README.md └── Solutions.md ├── week04 ├── README.md └── Solutions.md ├── week07 ├── README.md └── Solutions.md └── week12 ├── README.md └── Solutions.md /week11/q6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week11/q6.png -------------------------------------------------------------------------------- /week11/gets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week11/gets.png -------------------------------------------------------------------------------- /week11/q4_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week11/q4_1.png -------------------------------------------------------------------------------- /week11/1576784076412.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week11/1576784076412.png -------------------------------------------------------------------------------- /week08/figures/W08-ptr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week08/figures/W08-ptr1.png -------------------------------------------------------------------------------- /week08/figures/W08-ptr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week08/figures/W08-ptr2.png -------------------------------------------------------------------------------- /week08/figures/W08-ptr3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week08/figures/W08-ptr3.png -------------------------------------------------------------------------------- /week08/figures/Week8-Q2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week08/figures/Week8-Q2.png -------------------------------------------------------------------------------- /week01/figures/1569345823541.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569345823541.png -------------------------------------------------------------------------------- /week01/figures/1569348346330.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348346330.png -------------------------------------------------------------------------------- /week01/figures/1569348386213.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348386213.png -------------------------------------------------------------------------------- /week01/figures/1569348433566.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348433566.png -------------------------------------------------------------------------------- /week01/figures/1569348477633.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348477633.png -------------------------------------------------------------------------------- /week01/figures/1569348487515.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348487515.png -------------------------------------------------------------------------------- /week01/figures/1569348513992.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348513992.png -------------------------------------------------------------------------------- /week01/figures/1569348537826.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348537826.png -------------------------------------------------------------------------------- /week01/figures/1569348574235.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348574235.png -------------------------------------------------------------------------------- /week01/figures/1569348604330.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569348604330.png -------------------------------------------------------------------------------- /week01/figures/1569351507071.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week01/figures/1569351507071.png -------------------------------------------------------------------------------- /week02/figures/1570300391805.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300391805.png -------------------------------------------------------------------------------- /week02/figures/1570300492161.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300492161.png -------------------------------------------------------------------------------- /week02/figures/1570300588514.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300588514.png -------------------------------------------------------------------------------- /week02/figures/1570300636310.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300636310.png -------------------------------------------------------------------------------- /week02/figures/1570300699622.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300699622.png -------------------------------------------------------------------------------- /week02/figures/1570300750532.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300750532.png -------------------------------------------------------------------------------- /week02/figures/1570300848422.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300848422.png -------------------------------------------------------------------------------- /week02/figures/1570300852266.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300852266.png -------------------------------------------------------------------------------- /week02/figures/1570300948268.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300948268.png -------------------------------------------------------------------------------- /week02/figures/1570300973481.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570300973481.png -------------------------------------------------------------------------------- /week02/figures/1570302473710.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570302473710.png -------------------------------------------------------------------------------- /week02/figures/1570302539152.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1570302539152.png -------------------------------------------------------------------------------- /week02/figures/1582270849273.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1582270849273.png -------------------------------------------------------------------------------- /week02/figures/1582270887828.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week02/figures/1582270887828.png -------------------------------------------------------------------------------- /week05/figures/1571759785622.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week05/figures/1571759785622.png -------------------------------------------------------------------------------- /week05/figures/1571760221894.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week05/figures/1571760221894.png -------------------------------------------------------------------------------- /week05/figures/1571761472620.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week05/figures/1571761472620.png -------------------------------------------------------------------------------- /week08/figures/1587302715055.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week08/figures/1587302715055.png -------------------------------------------------------------------------------- /week09/figures/1588599881804.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588599881804.png -------------------------------------------------------------------------------- /week09/figures/1588599907830.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588599907830.png -------------------------------------------------------------------------------- /week09/figures/1588599935727.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588599935727.png -------------------------------------------------------------------------------- /week09/figures/1588600071903.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588600071903.png -------------------------------------------------------------------------------- /week09/figures/1588600093625.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588600093625.png -------------------------------------------------------------------------------- /week09/figures/1588600148394.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588600148394.png -------------------------------------------------------------------------------- /week09/figures/1588600212155.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gokceuludogan/cmpe150/HEAD/week09/figures/1588600212155.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cmpe150 - Spring 2020 2 | 3 | This repository contains notes of Cmpe150.04 lab sessions. 4 | 5 | Bogazici University, CMPE150 (Introduction to Computing C) 6 | 7 | **E-mail:** gokce.uludogan@boun.edu.tr 8 | 9 | ### Repositories of other lab sessions 10 | 11 | * [https://github.com/melsener/cmpe150](https://github.com/melsener/cmpe150) 12 | * [https://github.com/suyunu/c-notes](https://github.com/suyunu/c-notes) 13 | * [https://github.com/zeynepyirmibes/cmpe150-spring20](https://github.com/zeynepyirmibes/cmpe150-spring20) 14 | 15 | ### [Week 1](week01/) 16 | 17 | - Integers 18 | - Displaying outputs 19 | - Getting inputs 20 | 21 | ### [Week 2](week02/) 22 | 23 | * Data types 24 | * Input & output 25 | * Errors 26 | 27 | ### [Week 3](week03/) 28 | 29 | * Operators 30 | * Characters 31 | 32 | ### [Week 4](week04/) 33 | 34 | * Conditional Statements (If, If-else, Switch) 35 | 36 | ### [Week 5](week05/) 37 | 38 | * Loops (While, do while, for) 39 | 40 | ### [Week 6](week06/) 41 | 42 | * Nested Loops 43 | 44 | ### [Week 7](week07/) 45 | 46 | * Function 47 | 48 | ### [Week 8](week08/) 49 | 50 | * Function (Pass by reference) 51 | * Pointers 52 | 53 | ### [Week 9](week09/) 54 | 55 | * Arrays 56 | 57 | ### [Week 10](https://github.com/sb-b/cmpe150-spring2020/tree/master/Week10/) 58 | 59 | * Passing arrays to functions 60 | * Multidimensional arrays 61 | 62 | ### [Week 11](week11/) 63 | 64 | * Strings 65 | 66 | ### [Week 12](week12/) 67 | 68 | * Arrays, Strings, Multidimensional arrays 69 | 70 | ### [Week 13](week13/) 71 | 72 | * Structs 73 | 74 | 75 | -------------------------------------------------------------------------------- /week02/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Warm Up 4 | 5 | ```c 6 | #include 7 | int main() 8 | { 9 | printf("* *\n"); 10 | printf(" * *\n"); 11 | printf(" *\n"); 12 | printf(" * *\n"); 13 | printf("* *\n"); 14 | return(0); 15 | } 16 | ``` 17 | 18 | 19 | 20 | ```c 21 | #include 22 | int main() 23 | { 24 | printf("######\n"); 25 | printf("#\n"); 26 | printf("#\n"); 27 | printf("#####\n"); 28 | printf("#\n"); 29 | printf("#\n"); 30 | printf("#\n"); 31 | return(0); 32 | } 33 | ``` 34 | 35 | ## Warm Up 36 | 37 | ```c 38 | #include 39 | int main() 40 | { 41 | int num1, num2; 42 | scanf("%d%d", &num1, &num2); 43 | // printf("%d %d ", num1, num2); // Prints inputs 44 | printf("%d", num1 * num2); 45 | return 0; 46 | } 47 | ``` 48 | 49 | #### Alternative 50 | 51 | ```c 52 | #include 53 | int main() 54 | { 55 | int num1, num2; 56 | scanf("%d%d", &num1, &num2); 57 | int product = num1 * num2; 58 | printf("%d", product); 59 | return 0; 60 | } 61 | ``` 62 | 63 | ### Q1 64 | 65 | ```c 66 | #include 67 | 68 | int main() { 69 | float pi = 3.14; 70 | float radius; /* radius of the sphere */ 71 | float volume; /* volume of the sphere */ 72 | 73 | printf("Input the radius of the sphere : "); 74 | scanf("%f", &radius); 75 | 76 | volume = 4.0 / 3 * pi * (radius * radius * radius); 77 | printf("The volume of sphere is %f.\n", volume); 78 | 79 | return 0; 80 | } 81 | ``` 82 | 83 | 84 | 85 | ### Q2 86 | 87 | ```c 88 | #include 89 | 90 | int main(void) 91 | { 92 | float fahr; 93 | scanf("%f", &fahr); 94 | int celc = (fahr - 32) * 5.0 / 9; 95 | printf("%d", celc); 96 | return 0; 97 | } 98 | ``` 99 | 100 | ### Q3 101 | 102 | ``` c 103 | #include 104 | int main() { 105 | int sec, h, m, s; 106 | printf("Input seconds: "); 107 | scanf("%d", &sec); 108 | 109 | h = sec / 3600; 110 | 111 | m = (sec - (3600 * h)) / 60; 112 | 113 | s = (sec - (3600 * h) - (m * 60)); 114 | 115 | printf("H:M:S - %d:%d:%d\n", h, m, s); 116 | 117 | return 0; 118 | } 119 | ``` 120 | 121 | #### Alternative solution 122 | 123 | ``` c 124 | #include 125 | int main() { 126 | int sec, h, m, s; 127 | printf("Input seconds: "); 128 | scanf("%d", &sec); 129 | 130 | // total seconds = 3600 * hours + 60 * minutes + seconds 131 | h = sec / 3600; 132 | m = (sec % 3600) / 60; 133 | s = sec%60; 134 | printf("H:M:S - %d:%d:%d\n", h, m, s); 135 | return 0; 136 | } 137 | ``` 138 | 139 | 140 | 141 | ### Q4 142 | 143 | ``` c 144 | #include 145 | int main() 146 | { 147 | int n1, n2, n3; 148 | scanf("%d%d%d", &n1, &n2, &n3); 149 | n1 = n1 * (n1 % 2) ; 150 | n2 = n2 * (n2 % 2); 151 | n3 = n3 * (n3 % 2); 152 | printf("%d", n1+n2+n3); 153 | return 0; 154 | } 155 | ``` 156 | 157 | #### Alternative Solution 158 | 159 | ```c 160 | #include 161 | int main() 162 | { 163 | int n1, n2, n3; 164 | scanf("%d%d%d", &n1, &n2, &n3); 165 | printf("%d", n1 * (n1 % 2) + n2 * (n2 % 2) + n3 * (n3 % 2)); 166 | return 0; 167 | } 168 | 169 | ``` 170 | 171 | -------------------------------------------------------------------------------- /week03/Solutions.md: -------------------------------------------------------------------------------- 1 | # Warm up 2 | 3 | ```c 4 | #include 5 | int main() { 6 | int num; 7 | scanf("%d", &num); 8 | float result = (num / 3.0 + 10) * 2; 9 | printf("%.2f", result); 10 | return 0; 11 | } 12 | ``` 13 | 14 | ## Alternative 15 | 16 | ```c 17 | #include 18 | int main() { 19 | int num; 20 | scanf("%d", &num); 21 | float result = ((float) num / 3 + 10) * 2; 22 | printf("%.2f", result); 23 | return 0; 24 | } 25 | ``` 26 | 27 | # Q1 28 | 29 | ```c 30 | #include 31 | #include 32 | 33 | int main() { 34 | int num1, num2, num3; 35 | scanf("%d%d%d", &num1, &num2, &num3); 36 | num1 = (num1 % 3 > 0) * num1; 37 | num2 = (num2 % 3 > 0) * num2; 38 | num3 = (num3 % 3 > 0) * num3; 39 | printf("%d", num1 + num2 + num3); 40 | return 0; 41 | } 42 | 43 | ``` 44 | 45 | ## Alternative 1 46 | 47 | ```c 48 | #include 49 | #include 50 | 51 | int main() { 52 | int num1, num2, num3; 53 | scanf("%d%d%d", &num1, &num2, &num3); 54 | num1 = (num1 % 3 != 0) * num1; 55 | num2 = (num2 % 3 != 0) * num2; 56 | num3 = (num3 % 3 != 0) * num3; 57 | printf("%d", num1 + num2 + num3); 58 | return 0; 59 | } 60 | 61 | ``` 62 | 63 | ## Alternative 2 64 | 65 | ```c 66 | #include 67 | #include 68 | 69 | int main() { 70 | int num1, num2, num3; 71 | scanf("%d%d%d", &num1, &num2, &num3); 72 | num1 = (num1 % 3 == 1 || num1 % 3 == 2) * num1; 73 | num2 = (num2 % 3 == 1 || num2 % 3 == 2) * num2; 74 | num3 = (num3 % 3 == 1 || num1 % 3 == 2) * num3; 75 | printf("%d", num1 + num2 + num3); 76 | return 0; 77 | } 78 | 79 | ``` 80 | 81 | # Q2 82 | 83 | ```c 84 | #include 85 | #include 86 | int main(void) { 87 | char ch; 88 | scanf("%c", &ch); 89 | // printf("%d\n", ch - 'a'); // Finds the order of the lowercase letter 90 | // printf("%d\n", 'A'); // Prints the integer value of 'A' 91 | // printf("%d\n", ch - 'a' + 'A'); // Adds the order of the letter to integer value of A to find its uppercase version. Outputs the integer value of the uppercase. 92 | printf("%c\n", ch - 'a' + 'A'); 93 | return 0; 94 | } 95 | ``` 96 | 97 | ## Alternative 98 | 99 | ``` c 100 | #include 101 | int main() { 102 | char letter, uppercase; 103 | scanf("%c", &letter); 104 | int difference = 'a' - 'A'; 105 | uppercase = letter - difference; 106 | printf("%c", uppercase); 107 | return 0; 108 | } 109 | ``` 110 | 111 | # Q3 112 | 113 | ```c 114 | #include 115 | #define INCHES_IN_FOOT 12 116 | #define CM_IN_INCH 2.54 117 | int main() { 118 | int height; 119 | scanf("%d", &height); 120 | float height_in_feet = height / CM_IN_INCH / INCHES_IN_FOOT; 121 | printf("%f", height_in_feet); 122 | return 0; 123 | } 124 | ``` 125 | 126 | # Q4 127 | 128 | ```c 129 | #include 130 | 131 | int main() { 132 | char letter; 133 | scanf("%c", &letter); 134 | printf("%d", letter - '0' >= 0 && '9' - letter >= 0); 135 | return 0; 136 | } 137 | ``` 138 | 139 | ## Alternative 140 | 141 | ```c 142 | #include 143 | #include 144 | int main() { 145 | char ch; 146 | scanf("%c", &ch); 147 | int is_digit = ch >= '0' && ch <= '9'; 148 | printf("%d", is_digit); 149 | return 0; 150 | } 151 | ``` 152 | 153 | -------------------------------------------------------------------------------- /week13/Structs-Summary.md: -------------------------------------------------------------------------------- 1 | - How can we keep data which is a combination of types? 2 | 3 | - Structure! 4 | 5 | - A new data type 6 | 7 | - Definition: 8 | 9 | - ``` 10 | struct cat { 11 | char name[10]; 12 | int age; 13 | char gender; 14 | }; 15 | ``` 16 | 17 | - Declaration 18 | 19 | - `struct cat x;` 20 | 21 | - Initialization at declaration 22 | 23 | - struct cat x = {"Yumuk", 3, 'M'}; 24 | 25 | - Definition can be combined with declaration 26 | 27 | - ```c 28 | struct cat { 29 | char name[10]; 30 | int age; 31 | char gender; 32 | } pet1, pet2; 33 | ``` 34 | 35 | - Which one is type/variable? 36 | 37 | - Accessing members 38 | 39 | - Dot operator: *x.name* gives *Yumuk* 40 | 41 | - **typedef operator** 42 | 43 | - new name for an existing data type 44 | 45 | - `typedef float real;` 46 | 47 | - with struct 48 | 49 | - ```c 50 | typedef struct cat { 51 | char name[10]; 52 | int age; 53 | char gender; 54 | } Cat_t; 55 | 56 | Cat_t x; 57 | ``` 58 | 59 | - Nested structures: 60 | 61 | - Structure inside structure 62 | 63 | - ```c 64 | typedef struct{ 65 | char name[10]; 66 | int age; 67 | char gender; 68 | } Cat_t; 69 | 70 | typedef struct{ 71 | Cat_t animals[10]; 72 | } Shelter_t; 73 | 74 | Shelter_t sh; 75 | sh.animals[0] = {"Tekir", 2, 'F'}; 76 | ``` 77 | 78 | - Access 79 | 80 | - `puts(sh.animals[0].name);` 81 | 82 | - Function Parameters 83 | 84 | - Pass by value 85 | 86 | - ```c 87 | int same_age(Cat_t c1, Cat_t c2){ 88 | if(c1.age == c2.age){ 89 | return 1; 90 | }else{ 91 | return 0; 92 | } 93 | } 94 | int main(){ 95 | Cat_t c1 = {"Dana", 3, 'M'}, c2 = {"Yumuk", 3, 'M'}; 96 | printf("%d", same_age(c1, c2)); 97 | 98 | return 0; 99 | } 100 | ``` 101 | 102 | - Pass by reference 103 | 104 | - Similar to the basic types. 105 | 106 | - ```c 107 | void append_to_name(Cat_t *p, char second_name[]){ 108 | strcat((*p).name, second_name); 109 | } 110 | ``` 111 | 112 | - `append_to_name(&x, " Mars");` 113 | 114 | - Structure as return value 115 | 116 | - ```c 117 | Cat_t create_cat(char name[], int age, char gender){ 118 | Cat_t c; 119 | strcpy(c.name, name); 120 | c.age = age; 121 | c.gender = gender; 122 | return c; 123 | } 124 | ``` 125 | 126 | - ` Cat_t x = create_cat("Dana", 3, 'M');` 127 | 128 | - Shortcut 129 | 130 | - `(*p).name` == `p->name` 131 | 132 | - Passing arrays of structures to functions 133 | 134 | - Same as basic types. 135 | 136 | - Pass by reference 137 | 138 | - ```c 139 | typedef struct{ 140 | float x; 141 | float y; 142 | } Point_t; 143 | 144 | Point_t center(Point_t p[], int len){ 145 | Point_t center_p = {0, 0}; 146 | int i; 147 | for(i=0; iBreak 13 | Break 14 | 15 | 16 |
Continue 17 | 18 |
19 | 20 | *What about the sum of the factorial of only odd digits?* 21 | 22 | ### Question 2 23 | 24 | **a.** Write a program which takes an integer as input and prints whether the number is prime or not. 25 | 26 | | Input | Output | 27 | | ----- | --------- | 28 | | 7 | prime | 29 | | 8 | not prime | 30 | 31 | **b.** Write a program which takes an integer as input and prints prime numbers up to that integer. 32 | 33 | | Input | Output | 34 | | ----- | ------------- | 35 | | 11 | 2 3 5 7 | 36 | | 15 | 2 3 5 7 11 13 | 37 | 38 | ### Question 3 39 | 40 | **a.** Write a program which takes two positive integers as N and M then prints a rectangle with size NxM. 41 | 42 | | Input | Output | 43 | | :---: | ------------------------------------ | 44 | | 3 4 | \*\*\*\*
\*\*\*\*
\*\*\*\* | 45 | | 2 6 | \*\*\*\*\*\*
*\*\*\*\*\* | 46 | 47 | **b.** Write a program which takes two positive integers as N and M then prints a rectangle with size NxM which has stars (*) at borders, and lines (-) inside. 48 | 49 | | Input | Output | 50 | | :---: | --------------------------------------------------- | 51 | | 3 3 | \*\*\*
\*-\*
\*\*\* | 52 | | 4 5 | \*\*\*\*\*
*---\*
\*---\*
\*\*\*\*\* | 53 | 54 | ### Question 4 55 | 56 | **a.** Write a program to display a right angle triangle with N number of rows, like below. 57 | 58 | | Input | Output | 59 | | :---: | --------------------------------------- | 60 | | 3 | 1
12
123 | 61 | | 5 | 1
12
123
1234
12345 | 62 | 63 |
Hint At each row, we iterate from 1 to the row number.
64 | 65 | **b.** Write a program which takes two integer as N and x and display a right angle triangle with N number of rows formed by powers of x as follows: 66 | 67 | | Input | Output | 68 | | :---: | ------------------------------------- | 69 | | 3 4 | 4
4 16
4 16 64 | 70 | | 4 3 | 3
3 9
3 9 27
3 8 27 81 | 71 | 72 |
Hint At each row, we take power of input integer from once to the row number times.
73 | 74 | ### Question 5 75 | 76 | Write a program which takes an integer as input and prints an upper triangle of letters as follows: 77 | 78 | | Input | Output | 79 | | :---: | ------------------------------------------------- | 80 | | 5 | A B C D E
B C D E
C D E
D E
E | 81 | | 3 | A B C
B C
C | 82 | 83 | ### Question 6 (Requested in the Lab Session) 84 | 85 | Write a program which takes an integer as input and prints the following pattern: 86 | 87 | | Input | Output | 88 | | :---: | :----------------------------------------------------------- | 89 | | 5 |    \*
   \*\*
  \*\*\*
 \*\*\*\*
\*\*\*\*\* | 90 | | 3 |  \*
 \*\*
\*\*\* | 91 | 92 | -------------------------------------------------------------------------------- /week08/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Question 1 4 | 5 | ```c 6 | #include 7 | #include 8 | 9 | int main() { 10 | char a = 'F'; 11 | int b = 6; 12 | float c = 3.5; 13 | char *ptr_a = &a; 14 | int *ptr_b = &b; 15 | float *ptr_c = &c; 16 | 17 | printf("The address of char\t%p\n", &a); 18 | printf("The address of int\t%p\n", &b); 19 | printf("The address of float\t%p\n", &c); 20 | printf("The address of char*\t%p\n", &ptr_a); 21 | printf("The address of int*\t%p\n", &ptr_b); 22 | printf("The address of float*\t%p\n", &ptr_c); 23 | 24 | printf("The value of char\t%c\n", a); 25 | printf("The value of int\t%d\n", b); 26 | printf("The value of float\t%f\n", c); 27 | printf("The value of char*\t%p\n", ptr_a); 28 | printf("The value of int*\t%p\n", ptr_b); 29 | printf("The value of float*\t%p\n", ptr_c); 30 | 31 | printf("The value addressed by char* %c", *ptr_a); 32 | printf("The value addressed by int* %d", *ptr_b); 33 | printf("The value addressed by float* %d", *ptr_c); 34 | 35 | printf("The size of char\t%lu\n", sizeof(a)); 36 | printf("The size of int \t%lu\n", sizeof(b)); 37 | printf("The size of float\t%lu\n", sizeof(c)); 38 | printf("The size of char*\t%lu\n", sizeof(ptr_a)); 39 | printf("The size of int*\t%lu\n", sizeof(ptr_b)); 40 | printf("The size of float*\t%lu\n", sizeof(ptr_c)); 41 | return 0; 42 | } 43 | 44 | ``` 45 | 46 | ## Question 2 47 | 48 | ```c 49 | #include 50 | #include 51 | 52 | void increment(int a){ 53 | printf("Address: %p\n", &a); 54 | a++; 55 | } 56 | void decrement(int *ptr){ 57 | printf("Address: %p\n", &(*ptr)); 58 | (*ptr)++; 59 | } 60 | int main() { 61 | int number; 62 | scanf("%d", &number); 63 | printf("Before %p\n", &number); 64 | increment(number); 65 | printf("%d\n", number); 66 | decrement(&number); 67 | printf("%d\n", number); 68 | return 0; 69 | } 70 | 71 | ``` 72 | 73 | ## Question 3 74 | 75 | ```c 76 | #include 77 | #include 78 | 79 | void divide_with_remainder(int number, int divisor, int *res, int *rem){ 80 | *res = number / divisor; 81 | *rem = number % divisor; 82 | } 83 | int main() { 84 | int number, divisor, result, remainder; 85 | 86 | scanf("%d %d", &number, &divisor); 87 | 88 | divide_with_remainder(number, divisor, &result, &remainder); 89 | 90 | printf("%d %d", result, remainder); 91 | 92 | return 0; 93 | } 94 | ``` 95 | 96 | ### Alternative 97 | 98 | This solution works too. However, the results are written over the user input. 99 | 100 | ```c 101 | #include 102 | #include 103 | 104 | void divide_with_remainder(int number, int divisor, int *res, int *rem){ 105 | *res = number / divisor; 106 | *rem = number % divisor; 107 | } 108 | int main() { 109 | int result, remainder; 110 | 111 | scanf("%d %d", &result, &remainder); 112 | 113 | divide_with_remainder(result, remainder, &result, &remainder); 114 | 115 | printf("%d %d", result, remainder); 116 | 117 | return 0; 118 | } 119 | ``` 120 | 121 | ## Question 4 122 | 123 | ```c 124 | #include 125 | #include 126 | 127 | void swap(int *n1, int *n2){ 128 | int temp; 129 | temp = *n2; 130 | *n2 = *n1; 131 | *n1 = temp; 132 | } 133 | 134 | int main() { 135 | int a, b; 136 | scanf("%d %d", &a, &b); 137 | swap(&a, &b); 138 | printf("%d %d", a, b); 139 | return 0; 140 | } 141 | ``` 142 | 143 | ## Question 5 144 | 145 | ```c 146 | #include 147 | #include 148 | 149 | int local_max(int n, int *ptr_to_greatest) { 150 | int i, previous, current, next; 151 | int num_of_local_max = 0; 152 | scanf("%d", ¤t); 153 | scanf("%d", &next); 154 | for (i = 1; i < n - 1; i ++) { 155 | previous = current; 156 | current = next; 157 | scanf("%d", &next); 158 | if (current > previous && current > next) { 159 | num_of_local_max += 1; 160 | if(num_of_local_max == 1 || current > *ptr_to_greatest){ 161 | *ptr_to_greatest = current; 162 | } 163 | } 164 | } 165 | return num_of_local_max; 166 | } 167 | 168 | int main() { 169 | int n, num_of_local_max = 0, greatest; 170 | scanf("%d", &n); 171 | num_of_local_max = local_max(n, &greatest); 172 | printf("Number of local max: %d Greatest local max: %d", num_of_local_max, greatest); 173 | return 0; 174 | } 175 | ``` 176 | 177 | -------------------------------------------------------------------------------- /week05/README.md: -------------------------------------------------------------------------------- 1 | # Cmpe150.03 Week 5 2 | 3 | ![img](https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191202113149/CPP-Decision-Making.png) 4 | 5 | ### Warm up 6 | 7 | Write a program to input two numbers from user and find maximum between two numbers using if else and switch case. 8 | 9 | ``` 10 | input: 40 30 11 | output: 40 12 | ``` 13 | 14 | ## Loops 15 | 16 | **While, do/while, for** 17 | 18 | ### while loops 19 | 20 | * controlled by a boolean expression like if 21 | 22 | * determines how many times statement in code block will execute 23 | 24 | * Repeat until the expression becomes **false**. 25 | 26 | ``` 27 | while () 28 | 29 | ``` 30 | 31 | 1571759785622 32 | 33 | 34 | 35 | * Loop control variable *i* 36 | 37 | ```c 38 | int i = 0; // initialize 39 | while (i < n) { // test 40 | printf("*"); 41 | i++; //update 42 | } 43 | ``` 44 | 45 | 46 | How can we end up in an infinite loop? 47 | 48 | #### Question 1 49 | 50 | ##### a. 51 | 52 | Write a program that takes an integer as input and computes the sum of positive integers up to that integer using while loops. 53 | 54 | ``` 55 | input: 3 56 | output: 6 57 | 58 | input: 5 59 | output: 15 60 | ``` 61 | 62 | ##### b. 63 | 64 | Write a program that takes an integer as input and computes the sum of positive odd integers up to that integer using while loops. 65 | 66 | ``` 67 | input: 3 68 | output: 4 69 | 70 | input: 5 71 | output: 9 72 | ``` 73 | 74 | #### Question 2 75 | 76 | Write a program which reads an integer (smaller than 1000000) then prints out how many of the digits are odd numbers. 77 | 78 | ``` 79 | input: 16283 80 | output: 2 81 | 82 | input: 15672 83 | output 3 84 | ``` 85 | 86 | ## do-while loops 87 | 88 | First statement is executed, then expression is evaluated. 89 | 90 | ```c 91 | do 92 | 93 | while (); 94 | ``` 95 | 96 | 1571760221894 97 | 98 | 99 | 100 | 101 | 102 | #### Question 3 103 | 104 | Write a program that take integers as inputs until the user enters negative number and show the maximum number entered by user. 105 | 106 | ```` 107 | input: 8 3 0 105 18 108 | output: 105 109 | 110 | input: 75 7 1 5 39 45 67 111 | output: 75 112 | ```` 113 | 114 | ## for loops 115 | 116 | for statement includes the three parts needed for loops: initialize, test, and update. 117 | 118 | ```c 119 | for (; ; ){ 120 | 121 | } 122 | ``` 123 | 124 | 1571761472620 125 | 126 | 127 | 128 | 129 | 130 | ## continue and break 131 | 132 | ![Working of break statement](https://cdn.programiz.com/sites/tutorial2program/files/c-break-statement-works.jpg) 133 | 134 | ![Working of continue statement in C programming](https://cdn.programiz.com/sites/tutorial2program/files/c-continue-statement-works.jpg) 135 | 136 | 137 | 138 | ## Which Loops? 139 | 140 | * For loops: definite or counting loops 141 | * The number of iterations is definite. 142 | * i.e. Do sth N times.. 143 | * While and do loops: indefinite or result controlled loops 144 | * Do until some condition is satisfied. 145 | * i.e. Do sth until user inputs negative.. 146 | 147 | #### Question 4 148 | 149 | Write a program that takes two characters from user and displays characters between these. 150 | 151 | ``` 152 | input: a e 153 | output: a b c d e 154 | ``` 155 | 156 | #### Question 5 157 | 158 | Write a program which reads a positive integer number N , and prints the Nth fibonacci number. *0, 1, 1, 2, 3, 5, ...* 159 | 160 | F_0 = 0, F_1 = 1, F_n = F\_(n-2) + F\_(n-1) 161 | 162 | ``` 163 | input: 4 164 | output: 2 165 | input: 6 166 | output: 5 167 | ``` 168 | 169 | #### Question 6 170 | 171 | ##### a. 172 | 173 | Write a program which takes an integer as input and prints whether the number is prime or not. 174 | 175 | ``` 176 | input: 7 177 | output: prime 178 | 179 | input: 8 180 | output: not prime 181 | ``` 182 | 183 | ##### b. 184 | 185 | Write a program which takes an integer as input and prints prime numbers up to that integer. 186 | 187 | ``` 188 | input: 11 189 | output: 2 3 5 7 190 | 191 | input: 15 192 | output: 2 3 5 7 11 13 193 | ``` 194 | 195 | ## References 196 | 197 | https://www.cs.cmu.edu/~mrmiller/15-110/ 198 | 199 | https://www.programiz.com/c-programming/c-break-continue-statement -------------------------------------------------------------------------------- /week04/README.md: -------------------------------------------------------------------------------- 1 | # Cmpe150.04 Week 4 2 | 3 | ### Operators* 4 | 5 | - Assignment Operators (=, +=, -=, *=, /=) 6 | - Arithmetic Operators (+, -, *, /, %) 7 | - Increment-Decrement (++, --) 8 | - Logical Operators (&&, ||, !) 9 | - Zero => False 10 | - Nonzero => True 11 | - Relational Operators (>, <, >=, <=, ==, !=) 12 | - Precedence: Parentheses > Arithmetic Operators > Comparison Operators > Logical Operators > Assignment Operators 13 | 14 | ## Char 15 | 16 | * Represent characters. 17 | 18 | * Maps characters to integers using ASCII table. 19 | 20 | * ASCII Table: Letters and digits are consecutive. 21 | 22 | | Syntax | Meaning | 23 | | ------ | ------------- | 24 | | a | variable name | 25 | | 'a' | character | 26 | | "a" | string | 27 | 28 | ## Warm up 29 | 30 | Write a program that reads two digit as char and print multiplication of them. 31 | 32 | ``` 33 | input: 7 8 34 | output: 56 35 | ``` 36 | 37 | ## Conditional Statements 38 | 39 | * alters the flow of control 40 | * **if, if-else, and switch** 41 | 42 | ### If statements 43 | 44 | ```c 45 | if (boolean_expression) 46 | { 47 | statements 48 | } 49 | ``` 50 | 51 | ![1583474232710](C:\Users\gokce\AppData\Roaming\Typora\typora-user-images\1583474232710.png) 52 | 53 | 54 | 55 | ### if else statement 56 | 57 | ```c 58 | if (boolean_expression) 59 | statement1 60 | else 61 | statement2 62 | ``` 63 | 64 | ![1583474306401](C:\Users\gokce\AppData\Roaming\Typora\typora-user-images\1583474306401.png) 65 | 66 | ### else if 67 | 68 | * useful when checking multiple conditions. 69 | 70 | ```c 71 | if (boolean_expression) 72 | statement1 73 | else if (boolean_expression) 74 | statement2 75 | else 76 | statement3 77 | ``` 78 | 79 | * **else** is optional. 80 | 81 | ### nested if 82 | 83 | ```c 84 | if(boolean_expression) { 85 | //Nested if else inside the body of "if" 86 | if(boolean_expression) { 87 | //Statements inside the body of nested "if" 88 | } 89 | else { 90 | //Statements inside the body of nested "else" 91 | } 92 | } 93 | else { 94 | //Statements inside the body of "else" 95 | } 96 | ``` 97 | 98 | ### ternary if else: condition ? value1 : value2 99 | 100 | * Example: Get the larger of two variables ```a = (b>c) ? b : c;``` 101 | 102 | ### Question 1 103 | 104 | Write a program that takes year as input and prints whether a given year is a leap year or not. 105 | 106 | ``` 107 | input: 2018 108 | output: not a leap year 109 | 110 | input: 2020 111 | output: a leap year 112 | ``` 113 | 114 | ### Question 2 115 | 116 | Write a program to accept a coordinate point in a XY coordinate system and determine in which quadrant the coordinate point lies. 117 | 118 | ``` 119 | input: 5 7 120 | output: The point (5, 7) lies in the first quadrant. 121 | 122 | input: -2 -5 123 | output: The point (-2, -5) lies in the third quadrant. 124 | ``` 125 | 126 | ### Question 3 127 | 128 | Write a program that takes two integers and an operation as char (*, /, +, -) and performs the operations and prints. 129 | 130 | ``` 131 | input: 3 5 * 132 | output: 15 133 | input: 10 7 + 134 | output: 17 135 | ``` 136 | 137 | ### Question 4 138 | 139 | Write a program that takes three integers and find the largest one. 140 | 141 | ``` 142 | input: 4 1 -3 143 | output: 4 144 | 145 | input: 5 19 2 146 | output: 19 147 | ``` 148 | 149 | ### Switch case 150 | 151 | - When a matching case is found, the program will execute all the cases below it as well. 152 | 153 | - Use the break command to end the switch statement and skip remaining cases. 154 | 155 | ```c 156 | char command = 'b'; 157 | switch(command) 158 | { 159 | case 'a': 160 | case 'A': printf("Plan A\n"); 161 | break; 162 | case 'b': 163 | case 'B': printf("Plan B\n"); 164 | break; 165 | default : printf("I have no plans.\n"); // optional 166 | } 167 | ``` 168 | 169 | 170 | 171 | ##### What if we forget *break*? 172 | 173 | ### Question 5 174 | 175 | Write a program that takes a number in range (1, 7) and prints corresponding day of week using switch case 176 | 177 | ``` 178 | input: 3 179 | output: wednesday 180 | ``` 181 | 182 | ### Question 6 183 | 184 | Write a program to input two numbers from user and find maximum between two numbers using switch case. 185 | 186 | ``` 187 | input: 40 30 188 | output: 40 189 | ``` 190 | 191 | ### References 192 | 193 | https://codeforwin.org/2015/05/if-else-programming-practice.html 194 | 195 | https://beginnersbook.com/2014/01/c-if-else-statement-example/ 196 | 197 | ***** [https://github.com/suyunu/c-notes/tree/master/Week_04](https://github.com/suyunu/c-notes/tree/master/Week_04) 198 | 199 | 200 | 201 | 202 | 203 | ![img](https://media.geeksforgeeks.org/wp-content/cdn-uploads/20191202113149/CPP-Decision-Making.png) 204 | 205 | -------------------------------------------------------------------------------- /week06/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Question 1 4 | 5 | ```c 6 | #include 7 | #include 8 | 9 | int main() { 10 | int number, i, sum_of_fact=0; 11 | scanf("%d", &number); 12 | while(1){ 13 | int last_digit = number % 10; 14 | int factorial = 1; 15 | for(i = 2; i <= last_digit; i++){ 16 | factorial *= i; 17 | } 18 | sum_of_fact += factorial; 19 | number /= 10; 20 | if(number == 0){ 21 | break; 22 | } 23 | } 24 | printf("%d", sum_of_fact); 25 | return 0; 26 | } 27 | ``` 28 | 29 | ### Alternative 30 | 31 | ```c 32 | #include 33 | #include 34 | 35 | int main() { 36 | int number, i, sum_of_fact=0; 37 | scanf("%d", &number); 38 | while(number > 0){ 39 | int last_digit = number % 10; 40 | int factorial = 1; 41 | for(i = 2; i <= last_digit; i++){ 42 | factorial *= i; 43 | } 44 | sum_of_fact += factorial; 45 | number /= 10; 46 | } 47 | printf("%d", sum_of_fact); 48 | return EXIT_SUCCESS; 49 | } 50 | ``` 51 | 52 | ## Question 2 53 | 54 | ### a. 55 | 56 | ```c 57 | #include 58 | #include 59 | 60 | int main() { 61 | int number, i, is_prime = 1; 62 | scanf("%d", &number); 63 | for(i = 2; i < number; i++){ 64 | if(number % i == 0){ 65 | is_prime = 0; 66 | break; 67 | } 68 | } 69 | 70 | if(is_prime){ 71 | printf("prime"); 72 | }else{ 73 | printf("not prime"); 74 | } 75 | return 0; 76 | } 77 | 78 | ``` 79 | 80 | ### b. 81 | 82 | ```c 83 | #include 84 | #include 85 | 86 | int main() { 87 | int number, i, j; 88 | scanf("%d", &number); 89 | for(i = 2; i< number; i++){ 90 | int is_prime = 1; 91 | for(j = 2; j < i; j++){ 92 | if(i % j == 0){ 93 | is_prime = 0; 94 | break; 95 | } 96 | } 97 | if(is_prime){ 98 | printf("%d ", i); 99 | } 100 | } 101 | return 0; 102 | } 103 | 104 | ``` 105 | 106 | ## Question 3 107 | 108 | ### a. 109 | 110 | ```c 111 | #include 112 | #include 113 | 114 | int main() { 115 | int N, M, i, j; 116 | scanf("%d%d", &N, &M); 117 | 118 | for(i = 0; i < N; i++){ 119 | for(j = 0; j < M; j++){ 120 | printf("*"); 121 | } 122 | printf("\n"); 123 | } 124 | return 0; 125 | } 126 | ``` 127 | 128 | ### b. 129 | 130 | ```c 131 | #include 132 | #include 133 | 134 | int main() { 135 | int N, M, i, j; 136 | scanf("%d%d", &N, &M); 137 | 138 | for(i = 0; i < N; i++){ 139 | for(j = 0; j < M; j++){ 140 | if(i == 0 || j == 0 || i == N -1 || j == M - 1){ 141 | printf("*"); 142 | }else{ 143 | printf("-"); 144 | } 145 | 146 | } 147 | printf("\n"); 148 | } 149 | return 0; 150 | } 151 | 152 | ``` 153 | 154 | ## Question 4 155 | 156 | ### a. 157 | 158 | ```c 159 | #include 160 | #include 161 | 162 | int main() { 163 | int number, i, j; 164 | scanf("%d", &number); 165 | for(i = 1; i <= number; i++){ 166 | for(j = 1; j <= i; j++){ 167 | printf("%d", j); 168 | } 169 | printf("\n"); 170 | } 171 | return 0; 172 | } 173 | ``` 174 | 175 | ### b. 176 | 177 | ```c 178 | #include 179 | #include 180 | 181 | int main() { 182 | int row, number, i, j; 183 | scanf("%d%d", &row, &number); 184 | for(i = 1; i <= row; i++){ 185 | int power = 1; 186 | for(j = 1; j <= i; j++){ 187 | power *= number; 188 | printf("%d ", power); 189 | } 190 | printf("\n"); 191 | } 192 | return 0; 193 | } 194 | ``` 195 | 196 | ## Question 5 197 | 198 | | Output | Similarly | 199 | | :------------------------------------------------ | -------------------------------------------------- | 200 | | A B C D E
B C D E
C D E
D E
E | 1 2 3 4 5
2 3 4 5
3 4 5
4 5
5 | 201 | | A B C
B C
C | 1 2 3
2 3
3 | 202 | 203 | | Row | Output | 204 | | :--- | --------- | 205 | | 1 | 1 2 3 4 5 | 206 | | 2 | 2 3 4 5 | 207 | | 3 | 3 4 5 | 208 | | 4 | 4 5 | 209 | | 5 | 5 | 210 | 211 | So, we iterate rows with a loop variable **i** from 1 to 5. At each row, we iterate from row number to 5 *(from i to 5)*. 212 | 213 | ```c 214 | #include 215 | #include 216 | 217 | int main() { 218 | int number, i, j; 219 | scanf("%d", &number); 220 | for(i = 0; i < number; i++){ 221 | for(j = i; j < number; j++){ 222 | printf("%c", 'A'+j); 223 | } 224 | printf("\n"); 225 | } 226 | return 0; 227 | } 228 | ``` 229 | 230 | ## Question 6 231 | 232 | ```c 233 | #include 234 | #include 235 | 236 | int main() { 237 | int number; 238 | scanf("%d", &number); 239 | int i, j; 240 | for(i = 1; i <= number; i++){ 241 | for(j=i; j < number; j++){ 242 | printf(" "); 243 | } 244 | for(j=1; j<=i; j++){ 245 | printf("*"); 246 | } 247 | printf("\n"); 248 | } 249 | 250 | return 0; 251 | } 252 | ``` 253 | 254 | 255 | 256 | 257 | 258 | 259 | -------------------------------------------------------------------------------- /week13/README.md: -------------------------------------------------------------------------------- 1 | # Structures 2 | 3 | ### Question 1 4 | 5 | Define a **struct** representing a point (x, y). 6 | 7 | * write a function **distance** which takes two points and finds Euclidean distance between these points. Use *pow()* and *sqrt()* functions from math library *`include `* 8 | * write a function which takes two points and returns the point closest to the origin. *Use the distance function.* 9 | 10 | In main function, take two points from user and prints the coordinates of the point. 11 | 12 | | Input | Output | 13 | | -------------- | ------ | 14 | | 3 0
0 4 | 3 0 | 15 | | 11 7
6 8 | 6 8 | 16 | 17 | ### Question 2 18 | 19 | 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. 20 | 21 | 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. 22 | 23 | **Example**: 24 | 25 | * Rectangle: 26 | * lower left point: (2, 0) 27 | * upper right point: (8, 4) 28 | * moveX: -5 29 | * moveY: 2 30 | * Resulting Rectangle: 31 | * lower left point: (-3, 2) 32 | * upper right point: (3, 6) 33 | 34 | ### Question 3 35 | 36 | 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. 37 | 38 | *Use the point definition and distance function from the first question* 39 | 40 | | Input | Output | 41 | | -------------------------------------------- | ------------- | 42 | | 3
3 0
0 0
0 4 | 0,4
3,0 | 43 | | 5
10 5
4 8
0 8
4 5
-1 2 | -1,2
10,5 | 44 | 45 | ### Question 4 46 | 47 | Define a **struct** representing a student having **student_id, mt_grade, final_grade and letter_grade **. 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: 48 | 49 | - 100-75: A 50 | - 74-50: B 51 | - 49-25: C 52 | - 24-0: F 53 | 54 | ```c 55 | #include 56 | #include 57 | 58 | // Fill the definition of the struct. 59 | typedef struct{ 60 | 61 | } Student; 62 | 63 | void set_grade(Student *p){ 64 | 65 | } 66 | 67 | int main(){ 68 | int N; 69 | scanf("%d", &N); 70 | Student cmpe150_students[N]; 71 | int i; 72 | for(i=0; i < N; i++){ 73 | scanf("%f%f", &cmpe[i].mt_grade, &cmpe[i].final_grade); 74 | cmpe[i].student_id = 1000 + i; 75 | // Call set grade function 76 | } 77 | return 0; 78 | } 79 | ``` 80 | 81 | ### Question 5 82 | 83 | Define a struct representing a flight which has following fields: 84 | 85 | * Flight number (int) 86 | * Originating airport code (three characters) 87 | * Destination airport code (three characters) 88 | * Starting time (int) 89 | * Arrival time (int) 90 | 91 | Write a function which reads a flight from user. 92 | 93 | Write a program that takes an integer N and then reads N flights. Then reads originating and destination airport code from user and lists all the planes that leave from the originating airport and arrive at the destination airport. 94 | 95 | | Input | Output | 96 | | ------------------------------------------------------------ | ------------------------------------------ | 97 | | 5
3540 SAW ADB 14 15
3440 IST ESB 13 15
4041 ANT STL 13 21
4045 GTG KPG 17 18
ANT STL | 4041 ANT STL 13 21 | 98 | | 4
3540 SAW ADB 14 15
3543 SAW ADB 21 22
3440 IST ESB 13 15
4045 JFK IST 9 19
SAW ADB | 3540 SAW ADB 14 15
3543 SAW ADB 21 22 | 99 | 100 | ### Question 6 101 | 102 | 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. 103 | 104 | *Use the definition and functions from Question 5* 105 | 106 | | Input | Output | 107 | | ------------------------------------------------------------ | ------------------------------------------------------------ | 108 | | 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 | 109 | 110 | *Hint: Write a function for comparing two flights. It first checks the starting times and if they are equal then compares the originating airport codes.* 111 | 112 | ## Question 7 (Teaching.Codes - Lab 10 - Question 2) 113 | 114 | Define a struct named: Person which has an age, and a salary. Define a struct named: Family which has up to 9 persons and personCount (integer). 115 | 116 | Write a program which takes 3 families, and returns the age of the person who earns the most in the family with the least total salary. (age of the richest person in the poorest family) 117 | 118 | -------------------------------------------------------------------------------- /week05/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Warm up 4 | 5 | ### If-else 6 | 7 | ```c 8 | #include 9 | 10 | int main() { 11 | int first_num, second_num; 12 | scanf("%d %d", &first_num, &second_num); 13 | 14 | if(first_num > second_num ){ 15 | printf("%d", first_num); 16 | }else{ 17 | printf("%d", second_num); 18 | } 19 | return 0; 20 | } 21 | 22 | ``` 23 | 24 | ### Switch-case 25 | 26 | ```c 27 | #include 28 | 29 | int main() { 30 | int first_num, second_num; 31 | scanf("%d %d", &first_num, &second_num); 32 | 33 | switch(first_num > second_num ){ 34 | case 1: printf("%d", first_num); 35 | break; 36 | case 0: printf("%d", second_num); 37 | break; 38 | } 39 | return 0; 40 | } 41 | 42 | ``` 43 | 44 | ## Question 1 45 | 46 | ### a. 47 | 48 | #### While 49 | 50 | ```c 51 | #include 52 | 53 | int main() { 54 | int threshold, i = 1, result = 0; 55 | scanf("%d", &threshold); 56 | 57 | while(i <= threshold){ 58 | result += i++; 59 | } 60 | 61 | printf("%d\n", result); 62 | return 0; 63 | } 64 | 65 | ``` 66 | 67 | #### For 68 | 69 | ```c 70 | #include 71 | 72 | int main() { 73 | int threshold, i, result = 0; 74 | scanf("%d", &threshold); 75 | 76 | for(i=1; i<= threshold; i++){ 77 | result += i; 78 | } 79 | 80 | printf("%d\n", result); 81 | return 0; 82 | } 83 | 84 | ``` 85 | 86 | ### b. 87 | 88 | #### While 89 | 90 | ```c 91 | #include 92 | 93 | int main() { 94 | int threshold, i = 1, result = 0; 95 | 96 | scanf("%d", &threshold); 97 | 98 | while(i <= threshold){ 99 | if(i % 2 == 1){ 100 | result += i; 101 | } 102 | i++; 103 | } 104 | 105 | printf("%d\n", result); 106 | return 0; 107 | } 108 | 109 | ``` 110 | 111 | #### For 112 | 113 | ```c 114 | #include 115 | 116 | int main() { 117 | int threshold, i, result = 0; 118 | 119 | scanf("%d", &threshold); 120 | 121 | for(i=1; i<= threshold; i += 2){ 122 | result += i; 123 | } 124 | 125 | printf("%d\n", result); 126 | return 0; 127 | } 128 | 129 | ``` 130 | 131 | ## Question 2 132 | 133 | ```c 134 | #include 135 | 136 | int main() { 137 | int number, count = 0; 138 | scanf("%d", &number); 139 | 140 | while(number > 0){ 141 | count += number % 2; 142 | number /= 10; 143 | } 144 | 145 | printf("%d", count); 146 | return 0; 147 | } 148 | ``` 149 | 150 | Alternatively, 151 | 152 | ```c 153 | #include 154 | 155 | int main() { 156 | int number, count = 0; 157 | scanf("%d", &number); 158 | 159 | while(number > 0){ 160 | if(number % 2 == 1){ 161 | count++; 162 | } 163 | number /= 10; 164 | } 165 | 166 | printf("%d", count); 167 | return 0; 168 | } 169 | ``` 170 | 171 | ## Question 3 172 | 173 | ```c 174 | #include 175 | 176 | int main() { 177 | int number, max = 0; 178 | 179 | do{ 180 | scanf("%d", &number); 181 | max = max > number ? max : number; 182 | }while(number > 0); 183 | 184 | printf("%d", max); 185 | return 0; 186 | } 187 | ``` 188 | 189 | Alternatively, 190 | 191 | ```c 192 | #include 193 | 194 | int main() { 195 | int number, max = 0; 196 | 197 | do{ 198 | scanf("%d", &number); 199 | if(number > max){ 200 | max = number; 201 | } 202 | }while(number > 0); 203 | 204 | printf("%d", max); 205 | return 0; 206 | } 207 | 208 | ``` 209 | 210 | ## Question 4 211 | 212 | ```c 213 | #include 214 | int main() { 215 | char ch1, ch2; 216 | 217 | scanf("%c %c", &ch1, &ch2); 218 | 219 | for(; ch1 <= ch2; ch1++){ 220 | printf("%c", ch1); 221 | } 222 | 223 | return 0; 224 | } 225 | ``` 226 | 227 | Alternatively, 228 | 229 | ```c 230 | #include 231 | int main() { 232 | char ch1, ch2, i; 233 | 234 | scanf("%c %c", &ch1, &ch2); 235 | 236 | i = ch1; 237 | while(i <= ch2){ 238 | printf("%c ", i); 239 | i++; 240 | } 241 | return 0; 242 | } 243 | ``` 244 | 245 | ## Question 5 246 | 247 | ```c 248 | #include 249 | int main() { 250 | int i, n, prev = 0, curr = 1, next; 251 | scanf("%d", &n); 252 | 253 | for (i = 2; i <= n; ++i) { 254 | next = prev + curr; 255 | prev = curr; 256 | curr = next; 257 | } 258 | 259 | if(n == 0){ 260 | printf("%d", prev); 261 | }else{ 262 | printf("%d", curr); 263 | } 264 | return 0; 265 | } 266 | ``` 267 | 268 | ## Question 6 269 | 270 | ## a. 271 | 272 | ```c 273 | #include 274 | 275 | int main() { 276 | int number, i, is_prime = 0; 277 | 278 | scanf("%d", &number); 279 | 280 | for(i=2; i < number; i++){ 281 | if(number % i == 0){ 282 | is_prime = 1; 283 | break; 284 | } 285 | } 286 | 287 | if(is_prime != 0){ 288 | printf("not prime"); 289 | }else{ 290 | printf("prime"); 291 | } 292 | return 0; 293 | } 294 | 295 | ``` 296 | 297 | ## b. 298 | 299 | ```c 300 | #include 301 | 302 | int main() { 303 | int number, i, j, is_prime; 304 | 305 | scanf("%d", &number); 306 | 307 | for(j=2; j<=number; j++){ 308 | is_prime = 1; 309 | for(i=2; i < j; i++){ 310 | if(j % i == 0){ 311 | is_prime = 0; 312 | } 313 | } 314 | if(is_prime){ 315 | printf("%d ", j); 316 | } 317 | } 318 | return 0; 319 | } 320 | 321 | ``` 322 | 323 | -------------------------------------------------------------------------------- /week07/README.md: -------------------------------------------------------------------------------- 1 | # Week 7 - Functions 2 | 3 |
Function Syntax
4 | 5 | #### Question 1 6 | 7 | Write a function **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. 8 | 9 | Then write a program which reads a word from the user, and prints it with upper case characters. Assume valid words will be given. 10 | 11 | | Input | Output | 12 | | -------- | -------- | 13 | | cMpe | CMPE | 14 | | funCTi0n | FUNCTI0N | 15 | 16 | #### Question 2 17 | 18 | Write a function **find_max()** which takes three integers as parameters and returns **maximum** among them. Then, call this function in main() function with the following inputs and print corresponding outputs. 19 | 20 | | Input | Output | 21 | | ------- | ------ | 22 | | 5 3 8 | 8 | 23 | | 14 -1 9 | 14 | 24 | 25 | #### Question 3 26 | 27 | Write functions that compute the factorial f(n)=n!, permutation, P(n,k)=n!/(n-k)!, and the combinatorial, C(n,k)=n!/(n-k)! k!, of two numbers n and k, where n>0, k>0, and n ≥ k. 28 | 29 | Then write a function named **selection** whose parameters are a char and two integers. If the character is **C**, calls combinatorial function with the integer parameters and return the result. If the character is **P**, calls permutation function and return the result. Otherwise, returns -1. 30 | 31 | | Function | Input | Output | 32 | | ----------- | ----- | ------ | 33 | | factorial | 5 | 120 | 34 | | permutation | 5 2 | 20 | 35 | | combination | 5 2 | 10 | 36 | | selection | C 5 2 | 10 | 37 | | selection | P 5 2 | 20 | 38 | | selection | X 5 2 | -1 | 39 | 40 | #### Question 4 41 | 42 | Write a function named **fetchDigit** which takes two integers as input: **number** and **n**. Function should return the nth digit (from the right) of the number as an integer. If the number is less than n digits, Function should return -1. 43 | 44 | Then write a program which reads a positive integer z and another integer x, then print out the xth digit of z. If the input is incorrect (z doesn't have x digits), program should print "Incorrect input". Assume negative integers will not be given. 45 | 46 | | Input | Output | 47 | | -------- | --------------- | 48 | | 23423 3 | 4 | 49 | | 204602 5 | 0 | 50 | | 52 9 | Incorrect input | 51 | 52 |
Steps 53 |

23423, last_digit = 3, number = 2342, ctr = 1

54 | 55 |

2342, last_digit = 2, number = 234, ctr = 2

56 | 57 |

234, last_digit = 4, number = 23, ctr = 3

58 |
59 | 60 | #### Question 5 61 | 62 | 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. 63 | 64 | | Input | Output | 65 | | ----- | ----------------------------------------- | 66 | | 4 |    1
  2 3
 4 5 6
7 8 9 10 | 67 | | 3 |   1
 2 3
4 5 6 | 68 | 69 | #### Question 6 70 | 71 | 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 and returns 1 if the number is perfect and returns 0 otherwise. 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 | #### Question 7 81 | 82 | Write a function that will take an integer number and return its reverse version as another integer. Then fill/write main() appropriately to see the result. 83 | 84 | | Input | Output | 85 | | ----- | ------ | 86 | | 1234 | 4321 | 87 | | 68436 | 63486 | 88 | 89 |
Reverse 90 |

1234 % 10 = 4, 1234 / 10 = 123 0 * 10 + 4

, 91 |

123 % 10 = 3, 123 / 10 = 12, 4 * 10 + 3

92 |

12 % 10 = 2, 12 / 10 = 1, 43 * 10 + 2

93 |

1 % 10 = 1, 1 / 10 = 0, 432 * 10 + 1

94 | => 4321 95 |
96 | 97 | #### Question 8 98 | 99 | 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. 100 | 101 | | Input | Output | 102 | | ----- | ---------- | 103 | | 25 | 11001 | 104 | | 532 | 1000010100 | 105 | | 5 | 101 | 106 | 107 |
Conversion 108 |

25 / 2 = 12 with 1 remainder (units) 1 * 10^0

109 |

12 / 2 = 6 with 0 remainder (tens) 0 * 10^1

110 |

6 / 2 = 3 with 0 remainder (hundreds) 0 * 10^2

111 |

3 / 2 = 1 with 1 remainder (thousands) 1 * 10^3

112 |

1 / 2 = 0 with 1 remainder 1 * 10^4

113 | => 11001 114 |
115 | 116 | #### Question 9 117 | 118 | Write a function that will take a positive integer n, and returns the nth Fibonacci number. Then write main appropriately to see the result. 119 | 120 | | Input | Output | 121 | | ----- | ------ | 122 | | 1 | 0 | 123 | | 2 | 1 | 124 | | 3 | 1 | 125 | | 4 | 2 | 126 | | 5 | 3 | 127 | | 10 | 34 | 128 | | 15 | 377 | 129 | -------------------------------------------------------------------------------- /week04/Solutions.md: -------------------------------------------------------------------------------- 1 | # Warm up 2 | 3 | ```c 4 | #include 5 | 6 | int main() { 7 | char a, b; 8 | int result; 9 | scanf("%c%c", &a, &b); 10 | 11 | // printf("%c", a); 12 | // printf("%c", 'a'); 13 | // printf("a"); 14 | // printf("%d\n", a); 15 | // printf("%d\n", '0'); 16 | // printf("%d\n", a - '0'); 17 | // printf("%d\n", a - 0); 18 | // printf("%d\n", 'a' - '0'); 19 | result = (a - '0') * (b - '0'); 20 | printf("%d", result); 21 | return 0; 22 | } 23 | 24 | ``` 25 | 26 | ## Alternative 27 | 28 | ```c 29 | #include 30 | int main() { 31 | char first_digit, second_digit; // keeps the digits as characters. 32 | scanf("%c %c", &first_digit, &second_digit); 33 | // we subtract '0' character from the input since digits are successive in ASCII table. 34 | // '0' => 48 35 | // '1' => 49, etc. 36 | 37 | int first_number = first_digit - '0'; // finds the integer value of first digit. 38 | int second_number = second_digit - '0'; // finds the integer value of second digit. 39 | 40 | printf("%d", first_number * second_number); 41 | return 0; 42 | } 43 | ``` 44 | 45 | # Question 1 46 | 47 | ```c 48 | #include 49 | #include "teachingcodes.h" 50 | int main() { 51 | int year; 52 | scanf("%d", &year); 53 | 54 | if(year % 4 == 0){ 55 | printf("%d is a leap year.", year); 56 | }else{ 57 | printf("%d is not a leap year.", year); 58 | } 59 | 60 | return 0; 61 | } 62 | ``` 63 | 64 | ## Alternative 65 | 66 | ```c 67 | #include 68 | #include 69 | 70 | int main() { 71 | int year; 72 | scanf("%d", &year); 73 | printf("%d", year % 4); 74 | if(year % 4){ 75 | printf("not a leap year"); 76 | }else{ 77 | printf("leap year"); 78 | } 79 | return 0; 80 | } 81 | ``` 82 | 83 | 84 | 85 | # Question 2 86 | 87 | ```c 88 | #include 89 | #include 90 | 91 | int main() { 92 | // Ignore the cases where the point is on an axis. 93 | int x, y; 94 | scanf("%d %d", &x, &y); 95 | 96 | if(x > 0 && y > 0){ 97 | printf("First quadrant"); 98 | }else if(x < 0 && y > 0){ 99 | printf("Second quadrant"); 100 | }else if(x < 0 && y < 0){ 101 | printf("Third quadrant"); 102 | }else if(x > 0 && y < 0){ 103 | printf("Fourth quadrant"); 104 | } 105 | ``` 106 | 107 | ## Alternative solution 108 | 109 | ```c 110 | #include 111 | #include 112 | 113 | int main() { 114 | // Ignore the cases where the point is on an axis. 115 | 116 | int x, y; 117 | scanf("%d %d", &x, &y); 118 | 119 | if(x > 0){ 120 | if(y > 0){ 121 | printf("First quadrant"); 122 | }else{ 123 | printf("Fourth quadrant"); 124 | } 125 | }else{ 126 | if(y > 0){ 127 | printf("Second quadrant"); 128 | }else{ 129 | printf("Third quadrant"); 130 | } 131 | } 132 | ``` 133 | 134 | # Question 3 135 | 136 | ```c 137 | #include 138 | #include 139 | 140 | int main() { 141 | char op; 142 | int num1, num2; 143 | 144 | scanf("%d %d %c", &num1, &num2, &op); 145 | 146 | if(op == '+'){ 147 | printf("%d", num1 + num2); 148 | }else if (op == '-'){ 149 | printf("%d", num1 - num2); 150 | }else if (op == '*'){ 151 | printf("%d", num1 * num2); 152 | }else if (op == '/'){ 153 | printf("%d", num1 / num2); 154 | }else{ 155 | printf("Invalid operation!"); 156 | } 157 | 158 | return 0; 159 | } 160 | 161 | ``` 162 | 163 | # Question 4 164 | 165 | ## Solution using Nested Ifs 166 | 167 | ```c 168 | #include 169 | int main() { 170 | 171 | int num1, num2, num3, largest; 172 | scanf("%d %d %d", &num1, &num2, &num3); 173 | 174 | if(num1 >= num2){ 175 | if(num3 > num1){ 176 | largest = num3; 177 | }else{ 178 | largest = num1; 179 | } 180 | }else{ 181 | if(num3 > num2){ 182 | largest = num3; 183 | }else{ 184 | largest = num2; 185 | } 186 | } 187 | 188 | printf("%d", largest); 189 | return 0; 190 | } 191 | ``` 192 | 193 | ## Solution without Nested Ifs 194 | 195 | ```c 196 | #include 197 | 198 | int main() { 199 | 200 | int num1, num2, num3, largest; 201 | scanf("%d %d %d", &num1, &num2, &num3); 202 | 203 | if(num1 >= num2){ 204 | largest = num1; 205 | }else{ 206 | largest = num2; 207 | } 208 | 209 | if(num3 > largest){ 210 | largest = num3; 211 | } 212 | 213 | printf("%d", largest); 214 | return 0; 215 | } 216 | 217 | ``` 218 | 219 | ## Solution without Nested Ifs - 2 220 | 221 | ```c 222 | #include 223 | #include 224 | 225 | int main() { 226 | int num1, num2, num3; 227 | scanf("%d%d%d", &num1, &num2, &num3); 228 | if(num1 > num2 && num1 > num3){ 229 | printf("%d", num1); 230 | }else if(num2 > num1 && num2 > num3){ 231 | printf("%d", num2); 232 | }else{ 233 | printf("%d", num3); 234 | } 235 | 236 | return 0; 237 | } 238 | ``` 239 | 240 | 241 | 242 | ## Solution with ternary if else 243 | 244 | ```c 245 | #include 246 | 247 | int main() { 248 | 249 | int num1, num2, num3, largest; 250 | scanf("%d %d %d", &num1, &num2, &num3); 251 | 252 | largest = num1 > num2 ? num1 : num2; 253 | largest = num3 > largest ? num3 : largest; 254 | 255 | printf("%d", largest); 256 | return 0; 257 | } 258 | ``` 259 | 260 | # Question 5 261 | 262 | ```c 263 | #include 264 | int main() { 265 | int day_number; 266 | scanf("%d", &day_number); 267 | 268 | switch(day_number){ 269 | case 1: printf("Monday"); 270 | break; 271 | case 2: printf("Tuesday"); 272 | break; 273 | case 3: printf("Wednesday"); 274 | break; 275 | case 4: printf("Thursday"); 276 | break; 277 | case 5: printf("Friday"); 278 | break; 279 | case 6: printf("Saturday"); 280 | break; 281 | case 7: printf("Sunday"); 282 | break; 283 | default: printf("Not in range!"); 284 | } 285 | return 0; 286 | } 287 | ``` 288 | 289 | -------------------------------------------------------------------------------- /week12/README.md: -------------------------------------------------------------------------------- 1 | # Cmpe150 Week12 2 | 3 | ## Question 1 - Is symmetric? 4 | 5 | Write a program reads an integer N from the user, then reads N integers from the user. Then checks whether the array is symmetric. 6 | 7 | | Input | Output | 8 | | ------------------------ | ------------- | 9 | | 7
10 3 -1 5 -1 3 10 | symmetric | 10 | | 5
1 3 5 2 0 | not symmetric | 11 | 12 | ## Question 2 - Successively Repeating 13 | 14 | Write a program reads an integer N from the user. Takes N integers from user and place them in an array. Then, print the maximum number of repeating elements successively and this repeating element. 15 | 16 | | Input | Output | 17 | | -------------------------------- | ------ | 18 | | 7
10 2 4 4 5 5 5 | 3 5 | 19 | | 13
8 8 8 8 2 2 4 4 4 5 5 2 9 | 4 8 | 20 | 21 | **a.** First, find the successive occurrences of all elements and then print the element and number of successive occurrences of that. 22 | 23 | | Input | Output | 24 | | -------------------------------- | ------------------------------------------- | 25 | | 13
8 8 8 8 2 2 4 4 4 5 5 2 9 | 4 8
2 2
3 4
2 5
1 2
1 9 | 26 | 27 | **b.** Now you have the successive occurrences of elements. Find maximum of them. 28 | 29 | | Input | Output | 30 | | --------------------------------- | ------------------------------------------------------------ | 31 | | 13
8 8 8 8 2 2 4 4 4 5 5 2 9 | 4 8
2 2
3 4
2 5
1 2
1 9
Max: 4 8 | 32 | 33 | ## Question 3 - Array with Pointer 34 | 35 | Create a one dimensional array in the main function and initialize it with some integers. Then print the content of the array and the address of the each cell of the array using pointers. In other words, you are not allowed to use ``[ ]`` operators inside the loop you have written. You can use the size of the array in the loop you have written. 36 | 37 | | Example Output | 38 | | ------------------------------------------------------------ | 39 | | 1 0x7ffee72153c0
2 0x7ffee72153c4
3 0x7ffee72153c8
4 0x7ffee72153cc
5 0x7ffee72153d0 | 40 | 41 |
Arrays and pointers* 42 |

the address of &x[0] and x is the same. It's because the variable name x points to the first element of the array.

43 | 44 |

45 | &x[0] is equivalent to x. And, x[0] is equivalent to *x. 46 |

47 |

48 | &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i) 49 |

50 |
51 | 52 | ## Question 4 - Shift the Array 53 | 54 | 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. 55 | 56 | Write a proper main function to see the effect of **rightShiftA()**. 57 | 58 | | Input | Output | 59 | | --------------------- | --------- | 60 | | 5
1 2 3 4 5
3 | 3 4 5 1 2 | 61 | | 4
1 2 3 4
10 | 3 4 1 2 | 62 | 63 | ## Question 5 - Interstellar 64 | 65 | 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. 66 | 67 | | Input | Output | 68 | | ---------------------------------------------------------- | ------ | 69 | | b\*\*\*
\*\*c\*
\*a\*\*
\*\*\*d | a | 70 | | \*r\*\*
maz\*
l\*fk
\*\*\*d | f | 71 | 72 | * Array after reading 73 | 74 | * 000000 75 | 76 | 0xxxx0 77 | 0xxxx0 78 | 0xxxx0 79 | 0xxxx0 80 | 000000 81 | 82 | ## Question 6 - Put My Words 83 | 84 | 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 the modified sentence will be printed. 85 | 86 | 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. 87 | 88 | **Reminder:** A string is a char array that has \0 character at the end. 89 | 90 | **Reminder 2:** There shouldn’t be spaces or new lines at the end or the beginning of the sentence. 91 | 92 | 93 | | Input | Output | 94 | | -------------------------------------------------- | ---------------------------------------- | 95 | | is my life now
1
this | this is my life now | 96 | | WHeN LiFe GiVeS You MaKe LeMoNaDe
5
LeMoNs | WHeN LiFe GiVeS You LeMoNs MaKe LeMoNaDe | 97 | | dont Wait for
4
Opportunity | dont Wait for Opportunity | 98 | 99 | ## Question 7 - Remove Non-Letters 100 | 101 | Write a program that takes a string from the user and removes the characters the non-letter characters. Of course the null character ``\0`` should stay. 102 | 103 | | Input | Output | 104 | | ------------ | ------ | 105 | | h3ll0 w0r1d! | hllwrd | 106 | | 2020 may 19 | may | 107 | 108 | ## References 109 | 110 | * https://www.programiz.com/c-programming/c-pointers-arrays -------------------------------------------------------------------------------- /week09/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Question 1 4 | 5 | ### a. 6 | 7 | ```c 8 | #include 9 | #include 10 | 11 | int main() { 12 | int N; 13 | scanf("%d", &N); 14 | int arr[N]; 15 | int i, sum = 0; 16 | for(i = 0; i < N; i++){ 17 | scanf("%d", &arr[i]); 18 | sum += arr[i]; 19 | } 20 | printf("%d %f", sum, 1.0 * sum / N); 21 | return 0; 22 | } 23 | ``` 24 | 25 | ### b. 26 | 27 | ```c 28 | #include 29 | #include 30 | 31 | int main() { 32 | int N; 33 | scanf("%d", &N); 34 | int arr[N]; 35 | int i, sum = 0; 36 | for(i = 0; i < N; i++){ 37 | scanf("%d", &arr[i]); 38 | sum += arr[i]; 39 | } 40 | sum /= N; 41 | for(i = 0; i < N; i++){ 42 | if(arr[i] > sum){ 43 | printf("%d ", arr[i]); 44 | } 45 | } 46 | return 0; 47 | } 48 | ``` 49 | 50 | ## Question 2 51 | 52 | ```c 53 | #include 54 | #include 55 | 56 | int main(){ 57 | char sentence[50]; 58 | int size = 0, i; 59 | while(1){ 60 | char read; 61 | scanf("%c", &read); 62 | if(read == '\n'){ 63 | break; 64 | }else{ 65 | sentence[size] = read; 66 | size++; 67 | } 68 | } 69 | char encrypted[size]; 70 | for(i = 0; i < size; i++){ 71 | encrypted[i] = sentence[i] + 1; 72 | printf("%c", encrypted[i]); 73 | } 74 | printf("\n"); 75 | for(i = 0; i < size; i++){ 76 | printf("%c", encrypted[i] - 1); 77 | } 78 | printf("\n"); 79 | return 0; 80 | } 81 | 82 | ``` 83 | 84 | ## Question 3 85 | 86 | ### a. 87 | 88 | ```c 89 | #include 90 | #include 91 | 92 | int main() { 93 | int N; 94 | scanf("%d", &N); 95 | int arr[N], i; 96 | for(i = 0; i < N; i++){ 97 | scanf("%d", &arr[i]); 98 | } 99 | for(i = N - 1; i >= 0; i--){ 100 | printf("%d ", arr[i]); 101 | } 102 | return 0; 103 | } 104 | ``` 105 | 106 | ### b. 107 | 108 | ```c 109 | #include 110 | #include 111 | 112 | int main() { 113 | int N; 114 | scanf("%d", &N); 115 | int arr[N], i; 116 | for(i = 0; i < N; i++){ 117 | scanf("%d", &arr[i]); 118 | } 119 | for(i = 0; i <= N/2; i++){ 120 | int temp = arr[i]; 121 | arr[i] = arr[N-1-i]; 122 | arr[N-1-i] = temp; 123 | } 124 | for(i = 0; i < N; i++){ 125 | printf("%d ", arr[i]); 126 | } 127 | 128 | return 0; 129 | } 130 | ``` 131 | 132 | ## Question 4 133 | 134 | ```c 135 | #include 136 | #include 137 | 138 | int main() { 139 | int N, M; 140 | scanf("%d%d", &N, &M); 141 | int first_arr[N], second_arr[M], i, j; 142 | for(i = 0; i < N; i++){ 143 | scanf("%d", &first_arr[i]); 144 | } 145 | for(i = 0; i < M; i++){ 146 | scanf("%d", &second_arr[i]); 147 | } 148 | int start_index = -1; 149 | for(j = 0; j < M - N; j++){ 150 | int is_included = 1; 151 | for(i = 0; i < N; i++){ 152 | if(first_arr[i] != second_arr[j+i]){ 153 | is_included = 0; 154 | break; 155 | } 156 | } 157 | if(is_included){ 158 | start_index = j; 159 | } 160 | } 161 | if(start_index != -1){ 162 | printf("%d", start_index); 163 | } 164 | return 0; 165 | } 166 | ``` 167 | 168 | ### Alternative 169 | 170 | ```c 171 | #include 172 | #include 173 | 174 | int main() { 175 | int N, M; 176 | scanf("%d%d", &N, &M); 177 | int first_arr[N], second_arr[M], i, j; 178 | for(i = 0; i < N; i++){ 179 | scanf("%d", &first_arr[i]); 180 | } 181 | for(i = 0; i < M; i++){ 182 | scanf("%d", &second_arr[i]); 183 | } 184 | 185 | for(j = 0; j < M - N; j++){ 186 | for(i = 0; i < N; i++){ 187 | if(first_arr[i] != second_arr[j+i]){ 188 | break; 189 | } 190 | } 191 | if(i == n){ 192 | printf("%d", i); 193 | } 194 | } 195 | 196 | return 0; 197 | } 198 | ``` 199 | 200 | 201 | 202 | ## Question 5 203 | 204 | ```c 205 | #include 206 | #include 207 | 208 | int main() { 209 | int N; 210 | scanf("%d", &N); 211 | int numbers[N], i; 212 | int counts[101] = {0}; 213 | for(i = 0; i < N; i++){ 214 | scanf("%d", &numbers[i]); 215 | counts[numbers[i]]++; 216 | } 217 | for(i = 0; i < 101; i++){ 218 | if(counts[i]){ 219 | printf("%d: %d\n", i, counts[i]); 220 | } 221 | } 222 | 223 | return 0; 224 | } 225 | ``` 226 | 227 | ## Question 6 228 | 229 | ### a. 230 | 231 | ```c 232 | #include 233 | #include 234 | int main() { 235 | int N, M; 236 | scanf("%d%d", &N, &M); 237 | int i, j; 238 | int first_arr[N]; 239 | int second_arr[M]; 240 | for(i=0; i 263 | #include 264 | int main() { 265 | int N, M; 266 | scanf("%d%d", &N, &M); 267 | int i, j; 268 | int first_arr[N]; 269 | int second_arr[M]; 270 | for(i=0; i 293 | #include 294 | 295 | int main() { 296 | int N, i, j; 297 | scanf("%d", &N); 298 | int sequence[N]; 299 | for(i=0; i = 24 | * Assign the value of the expression to the variable 25 | * Right to left associative: 26 | * ```a = b = c = 10;``` ``` a = (b = (c = 10));``` 27 | * Initialization: 28 | * int a = 3, b = 4; 29 | float x = 2.75; 30 | * Compound assignment operator: 31 | * a = a + b; 32 | a += b; 33 | 34 | #### Type Conversion 35 | 36 | * Narrow to broader (i.e. int to float, 3 => 3.0): no loss of information. 37 | 38 | * Broad to narrower (i.e. float to int, 3.5 => 3): lost info. 39 | 40 | * Explicitly: (type) expression 41 | 42 | * int y = 18; 43 | * float x = ((float) y + 3) / 5; 44 | 45 | * ```c 46 | #include 47 | #include 48 | 49 | int main() { 50 | float x = 3.75; 51 | printf("%f ", x); // Outputs 3.750000 52 | int y = x; 53 | printf("%d ", y); // Outputs 3 54 | x = y; 55 | printf("%f", x); // Outputs 3.000000 56 | return 0; 57 | } 58 | 59 | ``` 60 | 61 | ### Arithmetic Operators (+, -, *, /, %) 62 | 63 | * The results of an operator expression is of the "wider" type (int < float < double) 64 | 65 | * Integer division vs Float division 66 | 67 | * Float division of integers: Multiply with 1.0 or cast into float. 68 | 69 | * Left to right associative: ```a/b/c``` <=>```(a/b)/c``` 70 | 71 | * Variable updates: 72 | 73 | * x = x +1, 74 | 75 | * ++, --: pre/post 76 | 77 | * x++: use x, then increment its value 78 | 79 | * ++x: increment x, then use updated value 80 | 81 | * ```c 82 | #include 83 | #include 84 | 85 | int main() { 86 | int x = 5; 87 | printf("%d ", x--); // Prints 5 88 | printf("%d ", x); // Prints 4 89 | printf("%d", ++x); // Prints 5 90 | return 0; 91 | } 92 | ``` 93 | 94 | ### Logical Operators 95 | 96 | * Boolean operations (true vs false) 97 | * Zero => false 98 | * Nonzero => true 99 | * Operators: 100 | * "&&" logical-AND 101 | * "||" logical-OR 102 | * "!" negation 103 | 104 | ### Relational Operators 105 | 106 | * Value comparisons: 1 if true else 0 107 | 108 | | **Operators** | **Example/Description** | 109 | | ------------- | ---------------------------------------- | 110 | | > | x > y (x is greater than y) | 111 | | < | x < y (x is less than y) | 112 | | >= | x >= y (x is greater than or equal to y) | 113 | | <= | x <= y (x is less than or equal to y) | 114 | | == | x == y (x is equal to y) | 115 | | != | x != y (x is not equal to y) | 116 | 117 | ### Expressions and precedence 118 | 119 | - Highest precedence: Parentheses 120 | - *, /, % are evaluated before +, - 121 | - **Arithmetic Operators > Comparison Operators > Logical Operators > Assignment Operators** 122 | - Associativity is used when there are two or more operators of same precedence 123 | 124 | ### Question 1 125 | 126 | Write a program that takes three integer from user and calculates the sum of the numbers which cannot be divided by 3. (Use relational operator) 127 | 128 | ``` 129 | input: 1 3 5 130 | output: 6 131 | 132 | input: 2 7 1 133 | output: 10 134 | ``` 135 | 136 | ## Data Type: Char 137 | 138 | - Represent characters (letters, digits, signs) 139 | 140 | - 1 byte = 8 bits = 256 values. 141 | 142 | - Syntax: `char ch = 'a';` 143 | 144 | - Identifier: `%c` 145 | 146 | - **Encoding:** Mapping characters to strings (integer values mostly) 147 | 148 | - ASCII table: maps 256 characters. 149 | 150 | ![](https://cdn.shopify.com/s/files/1/1014/5789/files/Standard-ASCII-Table_large.jpg?10669400161723642407) 151 | 152 | 153 | 154 | 155 | 156 | ![](https://cdn.shopify.com/s/files/1/1014/5789/files/Extended-ASCII-Table_large.jpg?14948660931384897703) 157 | 158 | 159 | 160 | - Letters and digits are consecutive. 161 | 162 | - Variable stores corresponding integer value of character. The translation is done by i/o functions. 163 | 164 | | Syntax | Meaning | 165 | | ------ | ------------- | 166 | | a | variable name | 167 | | 'a' | character | 168 | | "a" | string | 169 | 170 | - ```c 171 | #include 172 | #include 173 | 174 | int main() { 175 | char ch = 'D'; 176 | 177 | printf("%c ", ch); // Prints D 178 | printf("%d ", ch); // Prints 68 (corresponding integer value of D) 179 | printf("%d ", ch - 2); // Prints 66 180 | printf("%c", ch - 2); // Prints B (character corresponding to 66) 181 | return 0; 182 | } 183 | ``` 184 | 185 | ### Question 2 186 | 187 | Write a program takes a lowercase letter as input and converts it to uppercase and prints. 188 | 189 | ``` 190 | input: h 191 | output: H 192 | ``` 193 | 194 | ## Symbolic Constants 195 | 196 | - #define 197 | - Cannot be changed, fixed value. 198 | - Changes all occurrences of that name with given value. 199 | - Syntax: #define constant_name constant_value 200 | 201 | ### Question 3 202 | 203 | Write a program to convert height from centimeters to feet. 204 | 205 | ``` 206 | define constants: 207 | INCHES_IN_FOOT = 12 // 1 foot = 12 inches 208 | CM_IN_INCH=2.54 // 1 inch = 2.54 cms 209 | height in cm, read from user 210 | 211 | input: 180 212 | output: 5.9055 213 | ``` 214 | 215 | ### Question 4 216 | 217 | Write a program that reads one character as input and print 1 if it is a digit else 0. 218 | 219 | ``` 220 | input: 7 221 | output: 1 222 | 223 | input: a 224 | output: 0 225 | ``` 226 | 227 | ## References 228 | 229 | * https://www.geeksforgeeks.org/errors-in-cc/ 230 | 231 | * https://www.commfront.com/pages/ascii-chart 232 | 233 | * https://fresh2refresh.com/c-programming/c-operators-expressions/c-relational-operators/ 234 | 235 | -------------------------------------------------------------------------------- /week02/README.md: -------------------------------------------------------------------------------- 1 | # Cmpe150.04 Week 2 2 | 3 | ## Summary of Last Week 4 | 5 | ### Output 6 | 7 | ```c 8 | #include 9 | int main() 10 | { 11 | printf("Hello world!"); 12 | return 0; 13 | } 14 | ``` 15 | 16 | ### Integer variables 17 | 18 | ```c 19 | #include 20 | int main() 21 | { 22 | int number_1, number_2, sum; 23 | scanf("%d", &number_1); 24 | scanf("%d", &number_2); 25 | sum = number_1 + number_2; 26 | printf("Number1: %d Number2: %d Sum: %d \n", number_1, number_2, sum); 27 | return 0; 28 | } 29 | ``` 30 | 31 | ------ 32 | 33 | ### Variable names 34 | 35 | * It can contain alphanumeric characters and underscore(_) 36 | 37 | * ```c 38 | int number; 39 | int car_type; 40 | int week2_notes; 41 | ``` 42 | 43 | * It cannot start with number. 44 | 45 | * Case-sensitive. 46 | 47 | * ```c 48 | #include 49 | int main(){ 50 | int x = 2; 51 | int X = 4; 52 | printf("%d\n", x); 53 | printf("%d\n", X); 54 | return 0; 55 | } 56 | ``` 57 | 58 | ### Warm Up 59 | 60 | Write a program that prints the following shape to the screen using only *printf()*. 61 | 62 | ``` 63 | * * 64 | * * 65 | * 66 | * * 67 | * * 68 | ``` 69 | 70 | or 71 | 72 | Write a program that prints a block F using hash (#), where the F has a height of six characters and width of five and four characters. 73 | 74 | ``` 75 | ###### 76 | # 77 | # 78 | ##### 79 | # 80 | # 81 | # 82 | ``` 83 | 84 | ### Warm Up 85 | 86 | Write a program that takes two integers from the user and then prints the product of the numbers. 87 | 88 | ``` 89 | input: 3 4 90 | output: 12 91 | 92 | input: 5 8 93 | output: 40 94 | ``` 95 | 96 | ### Bit vs Byte 97 | 98 | * **Bit:** the smallest unit, *0 or 1*. 99 | * **Byte:** a collection of bits: *8 bits.* *(i.e. 01010111)* 100 | 101 | ### Fundamental Data Types 102 | 103 | ![img](https://qph.fs.quoracdn.net/main-qimg-7852cd4b11648b9ec8029a5df338bc2b) 104 | 105 | 106 | 107 | | Type | Size (bytes) | Format Specifier | 108 | | :----------------------- | :---------------------------- | :--------------: | 109 | | `int` | at least 2, usually 4 | `%d` | 110 | | `char` | 1 | `%c` | 111 | | `float` | 4 | `%f` | 112 | | `double` | 8 | `%lf` | 113 | | `short int` | 2 usually | `%hd` | 114 | | `unsigned int` | at least 2, usually 4 | `%u` | 115 | | `long int` | at least 4, usually 8 | `%li` | 116 | | `long long int` | at least 8 | `%lli` | 117 | | `unsigned long int` | at least 4 | `%lu` | 118 | | `unsigned long long int` | at least 8 | `%llu` | 119 | | `signed char` | 1 | `%c` | 120 | | `unsigned char` | 1 | `%c` | 121 | | `long double` | at least 10, usually 12 or 16 | `%Lf` | 122 | 123 | ### Integer Representation 124 | 125 | * If an integer type is stored in N bits 126 | * Signed range: -2^(N-1) and 2^(N-1) -1 127 | * Unsigned range: 0 to 2^N - 1. 128 | * Example: int: 4 bytes = 32 bits 129 | * int: - 2^(31) and 2^(31) -1. 130 | * unsigned int: 0 and 2^(32) - 1. 131 | * The sizeof() function returns the number of bytes occupied by each type. Returns *long*, so the identifier: *%lu* 132 | 133 | #### Overflow: Outside of the range. 134 | 135 | ```c 136 | #include 137 | int main() 138 | { 139 | // the sizes of data types 140 | printf("size of char = %lu\n", sizeof(int)); 141 | printf("size of char = %lu\n", sizeof(long)); 142 | printf("size of char = %lu\n", sizeof(unsigned int)); 143 | int a; // declaration 144 | a = 2147483647; // assignment of largest int 145 | printf("%d\n", a+1); 146 | return 0; 147 | } 148 | ``` 149 | 150 | ### Floating Point Representation 151 | 152 | Stores nonintegers. 153 | 154 | | Type | Storage size | Value range | Precision | 155 | | :---------: | :----------: | :--------------------: | :---------------: | 156 | | float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places | 157 | | double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places | 158 | | long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places | 159 | 160 | ### Q1 161 | 162 | Write a C program that reads the radius of a sphere and calculates the volume. *(π: 3.14)* 163 | $$ 164 | V = 4/3*π*r^3 165 | $$ 166 | 167 | ``` 168 | input: 3.5 169 | output: 179.50 170 | ``` 171 | 172 | ### Q2 173 | 174 | Write a program that converts a temperature from Fahrenheit to Centigrade. 175 | $$ 176 | C = (5/9) * (F - 32) 177 | $$ 178 | 179 | ``` 180 | input: 32.0 181 | output: 0 182 | 183 | input: 212.0 184 | output: 100 185 | 186 | input: 140.0 187 | output: 60 188 | ``` 189 | 190 | ### Q3 191 | 192 | Write a program to convert a given integer (in seconds) to hours, minutes and seconds. 193 | 194 | ``` 195 | input: 25300 196 | output: H:M:S - 7:1:40 197 | ``` 198 | 199 | ### Q4 200 | 201 | Write a program that takes 3 integer from the user and then prints the sum of odd numbers. 202 | 203 | ``` 204 | input: 5 7 8 205 | output: 12 206 | 207 | input: 3 5 8 208 | output: 8 209 | ``` 210 | 211 | ## Errors 212 | 213 | ### Compile Time Errors 214 | 215 | - Violating rules of writing syntax of C code. 216 | 217 | - Missing parenthesis 218 | - Missing semicolon 219 | - Printing value without declaration 220 | 221 | - Examples 222 | 223 | - ![1570300391805](figures/1570300391805.png) 224 | 225 | 226 | 227 | 228 | 229 | ![1570300492161](figures/1570300492161.png) 230 | 231 | 232 | 233 | - ![1570300588514](figures/1570300588514.png) 234 | 235 | 236 | 237 | ![1570300636310](figures/1570300636310.png) 238 | 239 | 240 | 241 | ### Run Time Errors 242 | 243 | - Errors cannot be detected by compilers and occur during program execution. 244 | 245 | - Example: 246 | 247 | - Division by zero. 248 | 249 | ![1582270849273](figures/1582270849273.png) 250 | 251 | ![1582270887828](figures/1582270887828.png) 252 | 253 | - Logic errors. 254 | 255 | ## Warnings 256 | 257 | - ![1570300699622](figures/1570300699622.png) 258 | 259 | 260 | 261 | 262 | 263 | ![1570300750532](figures/1570300750532.png) 264 | 265 | 266 | 267 | - ![1570300948268](figures/1570300948268.png) 268 | 269 | 270 | 271 | ![1570300973481](figures/1570300973481.png) 272 | 273 | 274 | 275 | - ![1570302473710](figures/1570302473710.png) 276 | 277 | 278 | 279 | 280 | 281 | ![1570302539152](figures/1570302539152.png) 282 | 283 | 284 | 285 | -------------------------------------------------------------------------------- /week07/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Question 1 4 | 5 | ```c 6 | #include 7 | #include 8 | 9 | char to_upper(char lowercase){ 10 | if(lowercase >= 'a' && lowercase <= 'z'){ 11 | lowercase = lowercase - 'a' + 'A'; 12 | } 13 | return lowercase; 14 | } 15 | 16 | int main() { 17 | char input, output; 18 | scanf("%c", &input); 19 | while(input != '\n'){ 20 | output = to_upper(input); 21 | printf("%c", output); 22 | scanf("%c", &input); 23 | } 24 | return 0; 25 | } 26 | 27 | ``` 28 | 29 | ### Alternative Solution 30 | 31 | ```c 32 | #include 33 | #include 34 | 35 | char to_upper(char lowercase){ 36 | if(lowercase >= 'a' && lowercase <= 'z'){ 37 | lowercase = lowercase - 'a' + 'A'; 38 | } 39 | return lowercase; 40 | } 41 | 42 | int main() { 43 | char input, output; 44 | scanf("%c", &input); 45 | while(1){ 46 | output = to_upper(input); 47 | printf("%c", output); 48 | scanf("%c", &input); 49 | if(input == '\n'){ 50 | break; 51 | } 52 | } 53 | return 0; 54 | } 55 | 56 | ``` 57 | 58 | ## Question 2 59 | 60 | ```c 61 | #include 62 | #include 63 | 64 | int find_max(int a, int b, int c){ 65 | int max; 66 | max = a > b ? a : b; 67 | max = max > c ? max : c; 68 | return max; 69 | } 70 | int main() { 71 | printf("%d\n", find_max(5, 3, 8)); 72 | printf("%d", find_max(14, -1, 9)); 73 | return 0; 74 | } 75 | ``` 76 | 77 | ## Question 3 78 | 79 | ```c 80 | 81 | #include 82 | #include 83 | 84 | int factorial(int n){ 85 | int i, fact = 1; 86 | for(i=2; i<=n; i++){ 87 | fact = fact * i; 88 | } 89 | return fact; 90 | } 91 | 92 | int permutation(int n, int k){ 93 | return factorial(n) / factorial(n-k); 94 | } 95 | 96 | int combinatorial(int n, int k){ 97 | return factorial(n) / (factorial(n-k) * factorial(k)); 98 | } 99 | 100 | int selection(char choice, int n, int k){ 101 | if(choice == 'C'){ 102 | return combinatorial(n, k); 103 | }else if(choice == 'P'){ 104 | return permutation(n, k); 105 | }else{ 106 | return -1; 107 | } 108 | } 109 | int main(void) { 110 | printf("%d\n", factorial(5)); 111 | printf("%d\n", permutation(5, 2)); 112 | printf("%d\n", combinatorial(5, 2)); 113 | printf("%d\n", selection('C', 5, 2)); 114 | printf("%d\n", selection('P', 5, 2)); 115 | printf("%d\n", selection('X', 5, 2)); 116 | 117 | return EXIT_SUCCESS; 118 | } 119 | 120 | ``` 121 | 122 | ## Question 4 123 | 124 | ```c 125 | #include 126 | #include 127 | 128 | int fetchDigit(int number, int n){ 129 | int counter = 0; 130 | while(number > 0){ 131 | int digit = number % 10; 132 | number /= 10; 133 | counter++; 134 | if(counter == n){ 135 | return digit; 136 | } 137 | } 138 | return -1; 139 | 140 | } 141 | int main() { 142 | int z, x; 143 | scanf("%d%d", &z, &x); 144 | int result = fetchDigit(z, x); 145 | if(result != -1){ 146 | printf("%d", result); 147 | }else{ 148 | printf("Incorrect input"); 149 | } 150 | return 0; 151 | } 152 | ``` 153 | 154 | ### Alternative 155 | 156 | ```c 157 | #include 158 | #include 159 | 160 | int fetchDigit(int number, int n){ 161 | int ctr = 0; 162 | int last_digit = -1; 163 | while(number > 0){ 164 | int digit = number % 10; 165 | number = number / 10; 166 | ctr++; 167 | if(n == ctr){ 168 | last_digit = digit; 169 | break; 170 | } 171 | } 172 | return last_digit; 173 | } 174 | 175 | int main() { 176 | int z, x; 177 | scanf("%d%d", &z, &x); 178 | int result = fetchDigit(z, x); 179 | if(result == -1){ 180 | printf("Incorrect input"); 181 | }else{ 182 | printf("%d", result); 183 | } 184 | return 0; 185 | } 186 | ``` 187 | 188 | ## Question 5 189 | 190 | ```c 191 | #include 192 | #include 193 | 194 | void pyramid(int num_of_rows){ 195 | int i, j, t=1; 196 | for(i=1;i<=num_of_rows;i++){ 197 | for(j=i; j 218 | #include 219 | 220 | void pyramid(int num_of_rows){ 221 | int i, j, temp = 1; 222 | for(i=1; i<=num_of_rows; i++){ 223 | for(j=1; j <= num_of_rows - i; j++){ 224 | printf(" "); 225 | } 226 | for(j=1; j<=i; j++){ 227 | printf("%d ", temp); 228 | temp++; 229 | } 230 | printf("\n"); 231 | } 232 | } 233 | int main() { 234 | pyramid(3); 235 | return 0; 236 | } 237 | 238 | ``` 239 | 240 | ## Question 6 241 | 242 | ```c 243 | #include 244 | #include 245 | 246 | int is_perfect(int number){ 247 | int i, sum=0; 248 | for(i=1; i 270 | #include 271 | #include "teachingcodes.h" 272 | 273 | int reverse(int number){ 274 | int reversed = 0; 275 | while(number > 0){ 276 | reversed = number % 10 + reversed * 10; 277 | number /= 10; 278 | } 279 | return reversed; 280 | 281 | } 282 | 283 | int main() { 284 | int input; 285 | scanf("%d", &input); 286 | printf("%d", reverse(input)); 287 | return 0; 288 | } 289 | ``` 290 | 291 | ### Alternative 292 | 293 | ```c 294 | #include 295 | #include 296 | 297 | int reverse(int num){ 298 | int rev_num = 0; 299 | while(1){ 300 | int digit = num % 10; 301 | rev_num = rev_num * 10 + digit; 302 | num /= 10; 303 | if(num == 0){ 304 | break; 305 | } 306 | } 307 | return rev_num; 308 | } 309 | int main() { 310 | printf("%d", reverse(1234)); 311 | return 0; 312 | } 313 | 314 | ``` 315 | 316 | ## Question 8 317 | 318 | ```c 319 | #include 320 | #include 321 | 322 | int to_binary(int number){ 323 | int bin = 0, i = 1; 324 | while(number > 0){ 325 | bin += (number % 2) * i; 326 | i *= 10; 327 | number /= 2; 328 | printf("%d %d %d\n", bin, i, number); 329 | } 330 | return bin; 331 | } 332 | 333 | int main() { 334 | int input; 335 | scanf("%d", &input); 336 | printf("%d", to_binary(input)); 337 | return 0; 338 | } 339 | 340 | ``` 341 | 342 | ## Question 9 343 | 344 | ```c 345 | #include 346 | #include 347 | 348 | int fibonacci_number(int number){ 349 | int i, prev = 0, curr = 1, next; 350 | 351 | for (i = 2; i < number; ++i) { 352 | next = prev + curr; 353 | prev = curr; 354 | curr = next; 355 | } 356 | 357 | if(number == 1){ 358 | return prev; 359 | }else{ 360 | return curr; 361 | } 362 | } 363 | 364 | int main() { 365 | int input; 366 | scanf("%d", &input); 367 | printf("%d", fibonacci_number(input)); 368 | return 0; 369 | } 370 | 371 | ``` 372 | 373 | -------------------------------------------------------------------------------- /week11/README.md: -------------------------------------------------------------------------------- 1 | # Cmpe150 Week11 2 | 3 | ## Strings 4 | 5 | * character array. `char str[101];` 6 | * ends with the null character ('\0'). 7 | * `char str[] = "Hello";` = `char str[] = {'H', 'e', 'l', 'l', 'o', '\0'};` 8 | 9 | **Single quotes for characters, double quotes for strings!** 10 | 11 | * Initialization 12 | * `char c[] = "abcd";` 13 | * `char c[50] = "abcd";` 14 | * `char c[] = {'a', 'b', 'c', 'd', '\0'};` 15 | * `char c[5] = {'a', 'b', 'c', 'd', '\0'};` 16 | 17 | * No need to track size of it. 18 | 19 | * ```c 20 | char str[] = "Hello World!"; 21 | for(int i=0; str[i] != '\0'; i++) 22 | printf("%c-", str[i]); 23 | ``` 24 | 25 | * Arrays and strings do not support the assignment operator once it is declared: 26 | 27 | * ```c 28 | char c[100]; 29 | c = "C programming"; // Error! array type is not assignable. 30 | strcpy(c, "C programming") // Correct! 31 | ``` 32 | 33 | ### Output 34 | 35 | * `printf("%s\n", str);` = `puts(str);` 36 | 37 | ### Input 38 | 39 | * `char str[101];` 40 | 41 | * `scanf("%s", str); // Reads until space` 42 | 43 | * `gets(str); // Reads until enter` 44 | 45 | * **Safer**: `fgets(str, 21, stdin);` 46 | 47 | * ```c 48 | #include 49 | int main(){ 50 | char str[100]; 51 | printf("Enter a string: "); 52 | scanf("%s", str); 53 | //gets(str); 54 | //fgets(str, sizeof(str), stdin); 55 | printf("The entered string: %s", str); 56 | return 0; 57 | } 58 | ``` 59 | 60 | * The **gets()** and **fgets()** do not work the same. The **gets()** function converts **newline character** to the **end-of-string character (‘\0’)** but **fgets() don’t convert newline character**, it puts a newline character in the string and appends an end-of-string character (‘\0’). 61 | 62 | ![](gets.png) 63 | 64 | 65 | ### Passing strings to functions 66 | 67 | * Same as arrays: pass by reference. 68 | * No need to pass the size. 69 | 70 | ### Array - Pointer Relation 71 | 72 | ```c 73 | int main(){ 74 | int myArr[5] = {1, 5, 6, 2, 2}; 75 | int i; 76 | 77 | for(i = 0; i < 5; i++) 78 | printf("%d ", *(myArr+i)); // Output: 1 5 6 2 2 79 | 80 | return 0; 81 | } 82 | ``` 83 | 84 | ### Built-in string library 85 | 86 | * #include 87 | * strlen(): Length of a string 88 | 89 | * The size of a string, not counting the terminating zero '\0'. 90 | * strcpy() and strncpy(): Copy strings 91 | * `char *strcpy(char *s1, const char *s2); ` 92 | 93 | * `char *strncpy(char *s1, const char *s2, size_t n)` 94 | 95 | * The destination should be large enough! 96 | 97 | * `strcpy()` copies and pastes null (`\0`) character. But `strncpy()` does not automatically puts a null (`\0`) character at the end of the pasted part. 98 | 99 | * ```c 100 | #include 101 | #include 102 | #include 103 | int main(){ 104 | int i; 105 | char s1[100] = "01234 56789", s2[100] = "abc defg", s3[100]; 106 | char s4[] = "12345678"; 107 | 108 | strcpy(s1, s2); 109 | strcpy(s3, s1+4); 110 | strncpy(s4, s1+4, 2); 111 | printf("s1: %s\n", s1); 112 | printf("s2: %s\n", s2); 113 | printf("s3: %s\n", s3); 114 | printf("s4: %s\n", s4); 115 | 116 | return 0; 117 | } 118 | ``` 119 | 120 | * strcmp() and strncmp(): Compare strings 121 | * `int strcmp(const char *s1, const char *s2);` 122 | * Returns < 0 if s1 comes before s2 alphabetically. 123 | * Returns 0 if s1 is identical to s2 alphabetically. 124 | * Returns > 0 if s1 comes after s2 alphabetically. 125 | 126 | * `int strncmp(const char *s1, const char *s2, size_t n);` 127 | 128 | * ```c 129 | int main(){ 130 | char s1[100] = "a1234", s2[100] = "b11234"; 131 | 132 | printf("%d\n", strcmp(s1, s2)); // -1 -> ('a' - 'b') 133 | printf("%d\n", strcmp(s1+1, s2+2)); // 0 -> Same 134 | 135 | return 0; 136 | } 137 | 138 | ``` 139 | * strcat(), strncat(): Append a string to the end of another 140 | * `char *strcat(char *s1, const char *s2);` 141 | 142 | * `char *strncat(char *s1, const char *s2, size_t n);` 143 | 144 | * ```c 145 | int main(){ 146 | char s1[100] = "01234 56789", s2[100] = "abc defg"; 147 | 148 | strcat(s1, s2); 149 | 150 | printf("s1: %s\n", s1); // s1: 01234 56789abc defg 151 | printf("s2: %s\n", s2); // s2: abc defg 152 | 153 | return 0; 154 | } 155 | ``` 156 | 157 | ### Question 1 158 | 159 | Write a function that copies a source string to the destination string. You can assume that (you should ensure that) destination string is large enough. 160 | 161 | *Try using `strcpy` function from `string.h` library.* 162 | 163 | ### Question 2 164 | 165 | 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. 166 | 167 | ``` 168 | input: abz abc 169 | output: 23 170 | ``` 171 | 172 | ### Question 3 173 | 174 | Write a program to count the total number of words in a string. 175 | 176 | ``` 177 | input: count the total number of words in a string 178 | output: 9 179 | ``` 180 | 181 | ### Question 4 182 | 183 | **a.** Write a program which takes a string and an integer n as parameters and print the nth word in the string. 184 | 185 | ``` 186 | input: count the total number of words in a string 3 187 | output: total 188 | ``` 189 | 190 | **b.** Write a program which takes a string and an integer n as parameters and deletes the nth word in the string. 191 | 192 | ``` 193 | input: count the total number of words in a string. 3 194 | output: count the number of words in a string. 195 | ``` 196 | 197 |
Figure
198 | 199 | ### Question 5 200 | 201 | **a.** Write a program to find maximum occurring character in a string assume all characters in lowercase. 202 | 203 | ``` 204 | input: she borrowed the book 205 | output: o 206 | ``` 207 | 208 | **b.** Update your program so that it would work with all characters not only lowercase ones. 209 | 210 | *Hint: What's the size of ASCII table?* 211 | 212 | ### Question 6 213 | 214 | 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. 215 | 216 | ``` 217 | input: merhaba dunyali nasilsin dunyali 218 | output: merhaba nasilsin 219 | ``` 220 | 221 | ![](q6.png) 222 | 223 | ### Question 7 224 | 225 | Write a program which takes a string as parameter and sorts words of this string and prints the resulting string. Assume initial string has maximum 100 characters and the length of each word is at most 10. 226 | 227 | ``` 228 | input: the lake is a long way from here 229 | output: a from here is lake long the way 230 | ``` 231 | 232 |
Words
233 | -------------------------------------------------------------------------------- /week11/Solutions.md: -------------------------------------------------------------------------------- 1 | # Solutions 2 | 3 | ## Question 1 4 | 5 | ```c 6 | /* 7 | ============================================================================ 8 | Name : w11_q1.c 9 | Author : 10 | Version : 11 | Copyright : Your copyright notice 12 | Description : Hello World in C, Ansi-style 13 | ============================================================================ 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | void copy_string(char src[], char dest[]){ 21 | int i; 22 | for(i = 0; src[i] != '\0'; i++){ 23 | dest[i] = src[i]; 24 | } 25 | dest[i] = '\0'; 26 | } 27 | int main() { 28 | char src[101]; 29 | char dest1[101]; 30 | char dest2[101]; 31 | fgets(src, sizeof(src), stdin); 32 | copy_string(src, dest1); 33 | printf("%s", dest1); 34 | strcpy(dest2, src); 35 | printf("%s", dest2); 36 | return 0; 37 | } 38 | ``` 39 | 40 | ## Question 2 41 | 42 | ```c 43 | 44 | #include 45 | #include 46 | 47 | int compare_strings(char s1[], char s2[]){ 48 | int i; 49 | for(i = 0 ; s1[i] != '\0' && s2[i] != '\0'; i++) 50 | if(s1[i] != s2[i]) 51 | return s1[i] - s2[i]; 52 | return 0; 53 | } 54 | 55 | int main(){ 56 | char s1[100], s2[100]; 57 | fgets(s1, sizeof(s1), stdin); 58 | fgets(s2, sizeof(s2), stdin); 59 | 60 | printf("%d\n", compare_strings(s1, s2)); 61 | printf("%d\n", strcmp(s1, s2)); 62 | return 0; 63 | } 64 | ``` 65 | 66 | ## Question 3 67 | 68 | ```c 69 | #include 70 | #include 71 | 72 | int main() { 73 | char input[101]; 74 | fgets(input, 101, stdin); 75 | int i, counter=1; 76 | for(i=0; input[i] != '\0'; i++){ 77 | if(input[i] == ' '){ 78 | counter++; 79 | } 80 | } 81 | printf("%d", counter); 82 | return 0; 83 | } 84 | ``` 85 | 86 | ## Question 4 87 | 88 | ### a. 89 | 90 | ```c 91 | #include 92 | #include 93 | 94 | int main() { 95 | char input[101], nth_word[101]; 96 | int n; 97 | fgets(input, 101, stdin); 98 | scanf("%d", &n); 99 | int i, counter=1, word_ix=0; 100 | for(i=0; input[i] != '\0'; i++){ 101 | if(counter == n){ 102 | //printf("%c", input[i]); 103 | nth_word[word_ix] = input[i]; 104 | word_ix++; 105 | } 106 | if(input[i] == ' '){ 107 | counter++; 108 | } 109 | } 110 | nth_word[word_ix] = '\0'; 111 | printf("%s", nth_word); 112 | return 0; 113 | } 114 | ``` 115 | 116 | ### b. 117 | 118 | ```c 119 | #include 120 | #include 121 | 122 | int main() { 123 | char input[101]; 124 | int n; 125 | fgets(input, 100, stdin); 126 | scanf("%d", &n); 127 | int i, counter=1; 128 | int start_ix = 0; // beginning of the first word 129 | int end_ix = strlen(s1)-1; // end of the last word 130 | for(i=0; input[i] != '\0'; i++){ 131 | if(input[i] == ' '){ 132 | counter++; 133 | if(counter == n){ 134 | start_ix = i + 1; // the start index of nth word 135 | } 136 | if(counter == n+1){ 137 | end_ix = i; // the index of the space after the nth word 138 | break; 139 | } 140 | } 141 | } 142 | if(strlen(s1)-1 == end_ix){ 143 | start_ix -= 1; // for the last space before the last word 144 | } 145 | for(i = start_ix; input[i] != '\0'; i++){ 146 | input[i] = input[i + end_ix - start_ix]; 147 | } 148 | input[i+1] = '\0'; 149 | printf("%s", input); 150 | return 0; 151 | } 152 | ``` 153 | 154 | ### Alternative 155 | 156 | ```c 157 | #include 158 | #include 159 | #include 160 | void delete_nth_word(char s1[], int n){ 161 | int i, counter=1; 162 | int start_ix = 0; // beginning of the first word 163 | int end_ix = strlen(s1)-1; // end of the last word 164 | for(i=0; s1[i] != '\0'; i++){ 165 | if(s1[i] == ' '){ 166 | counter++; 167 | if(counter == n){ 168 | start_ix = i + 1; // the start index of nth word 169 | } 170 | if(counter == n+1){ 171 | end_ix = i; // the index of the space after the nth word 172 | break; 173 | } 174 | } 175 | } 176 | if(strlen(s1)-1 == end_ix){ 177 | start_ix -= 1; // for the last space before the last word 178 | } 179 | strcpy(s1+start_ix, s1+end_ix+1); 180 | } 181 | 182 | int main() { 183 | char input[101]; 184 | int n; 185 | fgets(input, 100, stdin); 186 | scanf("%d", &n); 187 | delete_nth_word(input, n); 188 | printf("%s", input); 189 | return 0; 190 | } 191 | 192 | ``` 193 | 194 | ## Question 5 195 | 196 | ### a. 197 | 198 | ```c 199 | #include 200 | #include 201 | 202 | int main() { 203 | char str[101]; 204 | int n, i; 205 | fgets(str, 101, stdin); 206 | int freq[26] = {0}; 207 | for(i=0; str[i] != '\0'; i++){ 208 | freq[str[i] - 'a']++; 209 | } 210 | int max=0; 211 | char max_char; 212 | for(i=0; i<26; i++){ 213 | if(freq[i] > max){ 214 | max = freq[i]; 215 | max_char = i + 'a'; 216 | } 217 | } 218 | printf("%c", max_char); 219 | return 0; 220 | } 221 | ``` 222 | 223 | ### b. 224 | 225 | ```c 226 | #include 227 | #include 228 | 229 | int main() { 230 | char str[101]; 231 | int n, i; 232 | fgets(str, 101, stdin); 233 | int freq[256] = {0}; 234 | for(i=0; str[i] != '\0'; i++){ 235 | freq[str[i]]++; 236 | } 237 | int max=0; 238 | char max_char; 239 | for(i=0; i<256; i++){ 240 | if(freq[i] > max){ 241 | max = freq[i]; 242 | max_char = i; 243 | } 244 | } 245 | printf("%c", max_char); 246 | return 0; 247 | } 248 | ``` 249 | 250 | ### Question 6 251 | 252 | ```c 253 | 254 | #include 255 | #include 256 | 257 | int main(){ 258 | int i; 259 | char s1[100], s2[100]; 260 | fgets(s1, 100, stdin); 261 | fgets(s2, 100, stdin); 262 | 263 | s1[strlen(s1) - 2] = '\0'; 264 | s2[strlen(s2) - 2] = '\0'; 265 | 266 | for(i = 0 ; i < strlen(s1) ; i++){ 267 | if(strncmp(s1+i, s2, strlen(s2)) == 0){ 268 | char temp[100]; 269 | strcpy(temp, s1+i+strlen(s2)+1); 270 | strcpy(s1+i, temp); 271 | break; 272 | } 273 | } 274 | 275 | puts(s1); 276 | 277 | return 0; 278 | } 279 | ``` 280 | 281 | ### Question 7 282 | 283 | ![1576784076412](1576784076412.png) 284 | 285 | ```c 286 | 287 | #include 288 | #include 289 | 290 | int main(){ 291 | char str[101]; 292 | fgets(str, 101, stdin); 293 | int length = strlen(str); 294 | str[length - 2] = '\0'; 295 | int i, j; 296 | int num_of_words = 0; 297 | char words[10][10]; 298 | int word_index = 0; 299 | for(i=0; str[i] != '\0'; i++){ 300 | if(str[i] == ' '){ 301 | words[num_of_words][word_index] = '\0'; 302 | num_of_words++; 303 | word_index = 0; 304 | }else{ 305 | words[num_of_words][word_index] = str[i]; 306 | word_index++; 307 | } 308 | } 309 | words[num_of_words][word_index] = '\0'; 310 | 311 | // Prints words to make sure we are able to split it to words. 312 | /*for(i = 0; i <= num_of_words; i++){ 313 | for(j = 0; words[i][j] != '\0'; j++){ 314 | printf("%c", words[i][j]); 315 | } 316 | printf("\n"); 317 | // puts(words[i]); // equivalent to for loop and print newline above. 318 | }*/ 319 | 320 | 321 | char temp[10]; 322 | // Bubble sort words of the sentence. 323 | for(i = 0; i <= num_of_words; i++){ 324 | for(j = i+1; j <= num_of_words; j++){ 325 | if(strcmp(words[i], words[j]) > 0){ 326 | strcpy(temp, words[i]); 327 | strcpy(words[i], words[j]); 328 | strcpy(words[j], temp); 329 | } 330 | } 331 | } 332 | 333 | for(i = 0; i <= num_of_words; i++){ 334 | for(j = 0; words[i][j] != '\0'; j++){ 335 | printf("%c", words[i][j]); 336 | } 337 | printf(" "); 338 | } 339 | 340 | return 0; 341 | } 342 | 343 | ``` 344 | -------------------------------------------------------------------------------- /week01/README.md: -------------------------------------------------------------------------------- 1 | # Welcome to CMPE150.04 Lab Session! 2 | 3 | ### Assistant: Gökçe Uludoğan 4 | 5 | ### Mail: gokce.uludogan@boun.edu.tr 6 | 7 | ### Office: BM36 8 | 9 | ## Github: https://github.com/gokceuludogan/cmpe150 10 | 11 | ## Important Announcements 12 | 13 | * You will be added to an email list for the announcements after the add/drop period. **Make sure that your e-mail in BUIS is valid! ** 14 | 15 | * We will use **Eclipse IDE** and **Teaching Codes** plug-in in labs and exams. Student accounts will be created for this plug-in. The username and password will be sent to **the e-mail in BUIS**. 16 | 17 | * There will be **weekly quizzes** during lab hours. The quiz grades **contribute** to the overall grade. 18 | 19 | * Everyone must attend to the lab session of **the section registered to**. If you have any conflict, you should change your section. 20 | 21 | * You should complete the installation as soon as possible after we send the manual. 22 | 23 | * Labs hours are dedicated to **practical programming exercises not for covering lecture materials**. I **strongly** suggest you to attend the lectures. 24 | 25 | 26 | 27 | # Programming Languages 28 | 29 | ![Image result for popular programming languages](https://www.ubuntupit.com/wp-content/uploads/2018/11/Most-Popular-Programming-Languages.jpg) 30 | 31 | Figure from: https://www.ubuntupit.com/wp-content/uploads/2018/11/Most-Popular-Programming-Languages.jpg 32 | 33 | ![1569351507071](figures/1569351507071.png) 34 | 35 | 36 | 37 | ## Installation 38 | 39 | **Manuals:** https://programming.cmpe.boun.edu.tr/welcome 40 | 41 | The installation consists of four stages. The steps you should follow for these stages are OS dependent (Windows, Linux, MacOS). 42 | 43 | * **Stage #1:** *Install Java 8u181 JDK* 44 | * **Stage #2:** *Installing the C Compiler* 45 | * **Stage #3:** *Download and Install the ZIP File* 46 | * **Stage #4:** *Run Eclipse and Set Your Workspace* 47 | 48 | 49 | 50 | ## Eclipse IDE 51 | 52 | ### What's an IDE? 53 | 54 | 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) 55 | 56 | ### Eclipse is an open source integrated development environment. 57 | 58 | It provides tools for *writing code, compiling and building the program and debugging.* 59 | 60 | 61 | 62 | ## User Interface 63 | 64 | ![1569348487515](figures/1569348487515.png) 65 | 66 | 67 | 68 | * Project Explorer 69 | 70 | ![1569348386213](figures/1569348386213.png) 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | * Code Editor 81 | 82 | ![1569348513992](figures/1569348513992.png) 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | * Teaching Codes Plug-in 91 | 92 | ![1569348537826](figures/1569348537826.png) 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | * Console 101 | 102 | ![1569348574235](figures/1569348574235.png) 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | * Toolbar 111 | 112 | ![1569348604330](figures/1569348604330.png) 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | **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)* ** 121 | 122 | 123 | 124 | ### Creating a New Project and Running 125 | 126 | * Click **New** Under **File**. 127 | * Choose **C/C++ Project**. 128 | * Choose **C Managed Builds**. 129 | * Give your project a *name* and click **Finish**. 130 | * To build and run your project for the first time, you should right click on the project on *Project Explorer*. 131 | * Click **Build Project** from the options. 132 | * After you see *Build Finished* message on the *Console*, right click on the project again. 133 | * Click **Run as Local C/C++ Application**. 134 | * After first building and running, you could use the **build** and **run** buttons on the *Toolbar*. 135 | 136 | ## Main Function 137 | 138 | * This function is "called" by the operating system when the program runs. Thus it is the first code executed when a program starts. 139 | * Statements are executed in sequential order. 140 | 141 | * **return 0;** terminates the main() function and returns the value 0 meaning that the program runned successfully. 142 | 143 | ## Comments 144 | 145 | * Multiline comments: /* */ 146 | 147 | * Single line comments: // 148 | 149 | ## Code Block 150 | 151 | * The set of statements between a pair of braces is called a *block*. 152 | 153 | * Each statement must end with a semicolon (;) 154 | 155 | ## Variables 156 | 157 | * Storing value for later use 158 | * Variable names: only letters (English), digits, and underscore ( _ ). cannot start with a digit. Case sensitive. 159 | 160 | ## Declarations 161 | 162 | * In C, every variable needs to be *declared* to be a specific type before use. (i.e. int sum;) 163 | * Several variables of the same type can be declared in a single line. Names separated with comma. (int num1, num2, num3;) 164 | 165 | ### Example 166 | 167 | ```c 168 | #include 169 | int main() 170 | { 171 | int num1; 172 | int num2; 173 | int num3, num4; 174 | return 0; 175 | } 176 | ``` 177 | 178 | ## Assignments (=) 179 | 180 | * Stores a value at the memory location reserved for the variable. **Not mathematical equality.** 181 | 182 | ### Example 183 | 184 | ```c 185 | #include 186 | int main() 187 | { 188 | int num1; 189 | num1 = 3; 190 | int num2 = 5; 191 | return 0; 192 | } 193 | ``` 194 | 195 | ## Operations 196 | 197 | * Sum, Subtraction, Multiplication, Division, Modulus. 198 | 199 | ### Example 200 | 201 | ```c 202 | #include 203 | int main() 204 | { 205 | int num1 = 3; 206 | int num2 = 5; 207 | int num3; 208 | num3 = num1 + num2; 209 | return 0; 210 | } 211 | ``` 212 | 213 | ## Conventions 214 | 215 | * Write one statement per line 216 | * Indent the code inside a block (press Tab). 217 | * Blocks inside blocks should be further indented. 218 | * Use spaces on both sides of operators. 219 | * Use a lot of comments. 220 | * Use meaningful variable names. 221 | 222 | ## Output 223 | 224 | * The #include directive copies the contents of a “header file” into source code. 225 | * stdio.h: The header file for the standard input-output library. 226 | * Required for the printf() function. 227 | * #include 228 | 229 | ### printf() 230 | 231 | * printf(“Result= %d\n”, num3); 232 | * Takes a “format string”, and any number of expressions. Expression: Anything that returns a value. 233 | * %d: “Format specifier”, used for int. 234 | * \n: Newline character (one of the “escape” characters) 235 | * printf(“%d + %d = %d\n”, num1, num2, num3);** 236 | 237 | ### Example 238 | 239 | ```c 240 | #include 241 | int main() 242 | { 243 | printf("Hello world!"); 244 | return 0; 245 | } 246 | ``` 247 | 248 | ### Example 249 | 250 | ```c 251 | #include 252 | int main() 253 | { 254 | int num1 = 3; 255 | int num2 = 5; 256 | int num3; 257 | num3 = num1 + num2; 258 | printf(“%d + %d = %d\n”, num1, num2, num3); 259 | return 0; 260 | } 261 | ``` 262 | 263 | ## Input 264 | 265 | - Format: ```scanf("%d", &number);``` *the variable **number** must be declared before.* 266 | - Requires *stdio.h*: ```#include ``` 267 | 268 | ```c 269 | #include 270 | int main(){ 271 | // Declare variables 272 | int number_1, number_2, product; 273 | //Read first value 274 | printf("Enter first number\n"); 275 | scanf("%d", &number_1); 276 | //Read second value 277 | printf("Enter second number\n"); 278 | scanf("%d", &number_2); 279 | product = number_1 * number_2; 280 | // Display output 281 | printf("The product of %d and %d is %d\n", number_1, number_2, product); 282 | return 0; 283 | } 284 | ``` 285 | 286 | ### 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | -------------------------------------------------------------------------------- /week12/Solutions.md: -------------------------------------------------------------------------------- 1 | # Question 1 2 | 3 | ```c 4 | #include 5 | #include 6 | void read_array(int arr[], int N){ 7 | int i; 8 | for(i = 0; i < N; i++){ 9 | scanf("%d", &arr[i]); 10 | } 11 | } 12 | 13 | int is_symmetric(int arr[], int N){ 14 | int i; 15 | for(i = 0; i < N/2; i++){ 16 | if(arr[i] != arr[N-i-1]){ 17 | return 0; 18 | } 19 | } 20 | return 1; 21 | } 22 | 23 | int main() { 24 | int size; 25 | scanf("%d", &size); 26 | int numbers[size]; 27 | read_array(numbers, size); 28 | if(is_symmetric(numbers, size)){ 29 | printf("Symmetric"); 30 | }else{ 31 | printf("Not symmetric"); 32 | } 33 | return 0; 34 | } 35 | ``` 36 | 37 | ## Alternative 38 | 39 | ```c 40 | int is_symmetric(int arr[], int N){ 41 | int i; 42 | for(i = 0; i < N/2; i++){ 43 | if(arr[i] == arr[N-i-1]){ 44 | continue; 45 | }else{ 46 | return 0; 47 | } 48 | } 49 | return 1; 50 | } 51 | ``` 52 | 53 | # Question 2 54 | 55 | ## a 56 | 57 | ```c 58 | #include 59 | #include 60 | 61 | void read_array(int arr[], int size){ 62 | int i; 63 | for(i = 0; i < size; i++){ 64 | scanf("%d", &arr[i]); 65 | } 66 | } 67 | 68 | int main() { 69 | int size; 70 | scanf("%d", &size); 71 | int numbers[size]; 72 | read_array(numbers, size); 73 | int prev_el = numbers[0]; 74 | int i, counter = 1; 75 | for(i=1; i 94 | #include 95 | 96 | void read_array(int arr[], int size){ 97 | int i; 98 | for(i = 0; i < size; i++){ 99 | scanf("%d", &arr[i]); 100 | } 101 | } 102 | int main() { 103 | int size; 104 | scanf("%d", &size); 105 | int numbers[size]; 106 | read_array(numbers, size); 107 | int prev_el = numbers[0]; 108 | int i, counter = 1, max_counter = 1, max_el = numbers[0]; 109 | for(i=1; i max_counter){ 114 | max_counter = counter; 115 | max_el = prev_el; 116 | } 117 | printf("%d %d\n", counter, prev_el); 118 | prev_el = numbers[i]; 119 | counter = 1; 120 | } 121 | } 122 | printf("%d %d\n", counter, prev_el); 123 | printf("Max: %d %d", max_counter, max_el); 124 | return 0; 125 | } 126 | ``` 127 | 128 | # Question 3 129 | 130 | ```c 131 | #include 132 | 133 | int main(){ 134 | int x[10] = {1,2,3,4,5}; 135 | int i; 136 | 137 | for(i = 0; i < 5; i++){ 138 | printf("%d %p ",*(x+i), (x+i)); 139 | } 140 | return 0; 141 | } 142 | ``` 143 | 144 | # Question 4 145 | 146 | ```c 147 | #include 148 | 149 | void rightShiftA(int N, int arr[N], int T){ 150 | int i; 151 | int temp_arr[N]; 152 | 153 | T %= N; 154 | for(i = 0 ; i < N ; i++) 155 | temp_arr[(i+T)%N] = arr[i]; 156 | 157 | for(i = 0 ; i < N ; i++) 158 | arr[i] = temp_arr[i]; 159 | } 160 | 161 | 162 | int main() 163 | { 164 | int i; 165 | 166 | int N; 167 | scanf("%d", &N); 168 | 169 | int arr[N]; 170 | for(i = 0 ; i < N ; i++) 171 | scanf("%d", &arr[i]); 172 | 173 | int T; 174 | scanf("%d", &T); 175 | 176 | rightShiftA(N, arr, T); 177 | 178 | for(i = 0 ; i < N ; i++) 179 | printf("%d ", arr[i]); 180 | 181 | return 0; 182 | } 183 | ``` 184 | 185 | # Question 5 186 | 187 | ```c 188 | #include 189 | 190 | int main() { 191 | int R=4, C=4; 192 | int i, j, k, l; 193 | char arr[6][6] = {{0}}; // initialize the matrix with 0's 194 | 195 | for(i = 1 ; i <= R ; i++){ 196 | scanf("%s", arr[i]+1); // read the input line by line and put it inside 0's 197 | } 198 | 199 | /*for(i = 0 ; i <= R+1 ; i++){ 200 | for(j = 0 ; j <= C+1 ; j++){ 201 | printf("%c", arr[i][j]); // or print with %d 202 | } 203 | printf("\n"); 204 | }*/ 205 | char max_char = '*'; 206 | int max_counter = 0; 207 | for(i = 1; i <= R; i++){ 208 | for(j = 1; j <= C; j++){ 209 | if(arr[i][j] >= 'a' && arr[i][j] <= 'z'){ 210 | int current_counter = 0; 211 | for(k = -1 ; k <= 1 ; k++){ 212 | for(l = -1 ; l <= 1 ; l++){ 213 | if(arr[i+k][j+l] == '*'){ 214 | current_counter++; 215 | } 216 | } 217 | } 218 | if(current_counter == max_counter && arr[i][j] < max_char){ 219 | max_char= arr[i][j]; 220 | max_counter = current_counter; 221 | } 222 | if(current_counter > max_counter){ 223 | max_char = arr[i][j]; 224 | max_counter = current_counter; 225 | } 226 | } 227 | } 228 | } 229 | 230 | printf("%c %d", max_char, max_counter); 231 | 232 | return 0; 233 | } 234 | ``` 235 | 236 | # Question 6 237 | 238 | ```c 239 | #include 240 | #include 241 | 242 | void normalizeString(char s[]){ 243 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 244 | s[strlen(s)-2] = '\0'; 245 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 246 | s[strlen(s)-1] = '\0'; 247 | } 248 | 249 | int main(){ 250 | int n; 251 | char sentence[1001], word[21]; 252 | fgets(sentence, 1000, stdin); 253 | scanf("%d %s", &n, word); 254 | 255 | normalizeString(sentence); 256 | normalizeString(word); 257 | 258 | int i, num_of_space = 1; 259 | for(i = 0; num_of_space < n && i < strlen(sentence); i++){ 260 | if(sentence[i] == ' ') 261 | num_of_space++; 262 | } 263 | 264 | // last word 265 | if(num_of_space < n){ 266 | sentence[strlen(sentence)] = ' '; 267 | sentence[strlen(sentence)+1] = '\0'; 268 | strcat(sentence, word); 269 | }else{ 270 | char temp[1000] = ""; 271 | strcpy(temp, sentence+i); 272 | strcpy(sentence+i, word); 273 | sentence[i+strlen(word)] = ' '; 274 | strcpy(sentence+i+strlen(word)+1, temp); 275 | } 276 | 277 | printf("%s", sentence); 278 | 279 | return 0; 280 | } 281 | ``` 282 | 283 | ## Alternative 284 | 285 | ```c 286 | /* 287 | ============================================================================ 288 | Name : week12_q6.c 289 | Author : 290 | Version : 291 | Copyright : Your copyright notice 292 | Description : Hello World in C, Ansi-style 293 | ============================================================================ 294 | */ 295 | 296 | #include 297 | #include 298 | 299 | void normalizeString(char s[]){ 300 | if(s[strlen(s)-2] == '\r' || s[strlen(s)-2] == '\n') 301 | s[strlen(s)-2] = '\0'; 302 | else if(s[strlen(s)-1] == '\r' || s[strlen(s)-1] == '\n') 303 | s[strlen(s)-1] = '\0'; 304 | } 305 | 306 | int main(){ 307 | int n; 308 | char sentence[1001], word[21]; 309 | fgets(sentence, 1000, stdin); 310 | scanf("%d %s", &n, word); 311 | 312 | //normalizeString(sentence); 313 | sentence[strlen(sentence)-2] = '\0'; 314 | normalizeString(word); 315 | 316 | int i, num_of_space = 1; 317 | for(i = 0; num_of_space < n && i < strlen(sentence); i++){ 318 | if(sentence[i] == ' ') 319 | num_of_space++; 320 | } 321 | 322 | //printf("%d", i); 323 | // last word 324 | if(num_of_space < n){ 325 | sentence[strlen(sentence)] = ' '; 326 | sentence[strlen(sentence)+1] = '\0'; 327 | strcat(sentence, word); 328 | }else{ 329 | char temp[1000] = ""; 330 | strcpy(temp, sentence+i); 331 | strcpy(sentence+i, word); 332 | sentence[strlen(sentence)+1] = '\0'; 333 | sentence[strlen(sentence)] = ' '; 334 | strcat(sentence, temp); 335 | } 336 | printf("%s", sentence); 337 | return 0; 338 | } 339 | ``` 340 | 341 | # Question 7 342 | 343 | ```c 344 | #include 345 | 346 | int main() { 347 | char line[151]; 348 | fgets(line, 150, stdin); 349 | int i, j; 350 | 351 | for(i = 0; line[i] != '\0'; i++){ 352 | char ch = line[i]; 353 | if(!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z')){ 354 | for(j = i; line[j] != '\0'; j++){ 355 | line[j] = line[j + 1]; 356 | } 357 | i--; 358 | } 359 | } 360 | 361 | printf("%s\n", line); 362 | return 0; 363 | } 364 | 365 | ``` 366 | 367 | -------------------------------------------------------------------------------- /week08/README.md: -------------------------------------------------------------------------------- 1 | # Week 8 2 | 3 | ### Functions (Up to now) 4 | 5 | * May or may not return a value (**only one**). 6 | * **Cannot return more than one value.** 7 | * **Called by value.** 8 | * Actual **value** of an argument is copied into the formal parameter of the function. 9 | * Changes made to the parameter inside the function have no effect on the argument 10 | * ![](https://www.geeksforgeeks.org/wp-content/uploads/Call-By-Value.png) 11 | 12 |
Call by reference 13 | 14 |
15 | 16 |
Pointers 17 |

& address of operator

18 |

* value at address operator (used for pointers)

19 | 20 | 21 | 22 |
23 | 24 | ## Question 1 (*) 25 | 26 | Write a C program that declares and initializes (to any value you like) a float, an int, and a char. Next declare and initialize a pointer to each of the three variables. Your program should then print the address of, and value stored in, and the memory size (in bytes) of each of the six variables. 27 | Use the **"%p"** formatting specifier to print addresses in hexadecimal. You should see addresses that look something like this: "0xbfe55918". The initial characters **"0x"** tell you that hexadecimal notation is being used; the remainder of the digits give the address itself. 28 | Use **"%f"** to print a floating value. Use the **sizeof** operator to determine the memory size allocated for each variable (**"%lu"** for printing sizes). 29 | 30 | **Sample Output:** 31 | 32 | *The addresses will be different when you run your program.* 33 | 34 | 35 | 36 |
Layout 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 |
AddressValueSize
char a;0x5fca9c'F'1
int b;0x5fca9864
float c;0x5fca943.5000004
char *ptr_a;0x5fca880x5fca9c8
int *ptr_b;0x5fca800x5fca988
float *ptr_c;0x5fca780x5fca948
84 |
85 | 86 | ## Question 2 87 | 88 | Write a function named **increment** which takes an integer as a parameter and increase the value of that integer and returns nothing. 89 | 90 | Write a function named **decrement** which takes an integer pointer (address of an integer) and decrease the value of the integer addressed by this pointer and returns nothing. 91 | 92 | In the main program, read an integer from the user. Then call **increment** with this integer and print the value of this integer. Then call **decrement** with this integer and print the value of this integer again. Notice the difference. 93 | 94 |
Steps
95 | 96 | ## Question 3 (TeachingCodes Lab8) 97 | 98 | Write a function named **divideWithRemainder** which takes two integer values: **number** and **divisor**, and two integer pointers (addresses of two integers): **result** and **remainder**. This function should divide the number by divisor, then write the result into the address given by **result**, and write the remainder into the address given by **remainder**. 99 | 100 | In the main program, create four integers: a,b,c,d. Then read the values of a and b from the user. Then call the function with the values of a and b, and the addresses of c and d, respectively. Your function will divide a by b, and the value of c will be the result, and the value of d will be the remainder. Then print out the values of c and d. 101 | 102 | *Note that this function doesn't need to return anything, because it is already able to give output to the given addresses.* 103 | 104 | *Also note that, before calling a function with pointer inputs in the main program, you should first create appropriate variables so that you can give proper addresses to that function.* 105 | 106 | ## Question 4 (TeachingCodes Lab8) 107 | 108 | Write a function named **swap** which takes two integer pointers (addresses of two integers) and swaps the values in those two addresses. 109 | 110 | *Note that this function doesn't need to return a value.* 111 | 112 | In the main program, create two integer variables a and b, then read their values from the user. Print their values to the screen. Then call the swap function with the addresses of these two integers, and print their values again to see if they are really swapped. 113 | 114 | ### Swap - Call by Value 115 | 116 | ```c 117 | #include 118 | 119 | /* function declaration */ 120 | void swap(int a, int b); 121 | 122 | /* function definition to swap the values */ 123 | void swap(int a, int b) { 124 | 125 | int temp; 126 | 127 | temp = a; /* save the value of a */ 128 | a = b; /* put b into a */ 129 | b = temp; /* put temp into b */ 130 | } 131 | 132 | int main () { 133 | 134 | /* local variable definition */ 135 | int a = 100; 136 | int b = 200; 137 | 138 | printf("Before swap, value of a : %d\n", a ); 139 | printf("Before swap, value of b : %d\n", b ); 140 | 141 | /* calling a function to swap the values */ 142 | swap(a, b); 143 | 144 | printf("After swap, value of a : %d\n", a ); 145 | printf("After swap, value of b : %d\n", b ); 146 | 147 | return 0; 148 | } 149 | ``` 150 | 151 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_val1.png) 152 | 153 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_val2.png) 154 | 155 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_val3.png) 156 | 157 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_val4.png) 158 | 159 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_val5.png) 160 | 161 | ### Swap - Call by Reference 162 | 163 | ```c 164 | #include 165 | 166 | /* function declaration */ 167 | void swap(int *a, int *b); 168 | 169 | /* function definition to swap the values */ 170 | void swap(int *a, int *b) { 171 | int temp; 172 | 173 | temp = *a; /* save the value of *a */ 174 | *a = *b; /* put *b into *a */ 175 | *b = temp; /* put temp into *b */ 176 | } 177 | 178 | int main () { 179 | /* local variable definition */ 180 | int a = 100; 181 | int b = 200; 182 | 183 | printf("Before swap, value of a : %d\n", a ); 184 | printf("Before swap, value of b : %d\n", b ); 185 | 186 | /* calling a function to swap the values */ 187 | swap(&a, &b); 188 | 189 | printf("After swap, value of a : %d\n", a ); 190 | printf("After swap, value of b : %d\n", b ); 191 | 192 | return 0; 193 | } 194 | ``` 195 | 196 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_ptr1.png) 197 | 198 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_ptr2.png) 199 | 200 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_ptr3.png) 201 | 202 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_ptr4.png) 203 | 204 | ![](https://github.com/suyunu/c-notes/raw/master/Week_08/figures/W08-swap_ptr5.png) 205 | 206 | ## Question 5 207 | 208 | Write a function named **local_max** which takes an integer **n** which is the number of integers to read and an integer pointer **ptr_to_greatest** to set the greatest local maximum in the sequence and returns number of local maxima of this sequence. 209 | 210 | Consider three consecutive numbers such as **a**, **b**, **c**. **b** is a local maximum if it is higher than **a** and **c**. For example, in the sequence 7 4 8 5 6 1 10 3 there are three local maxima. 211 | 212 | | Subsequence | Local Max? | Greatest Local Max | 213 | | ----------- | ---------- | ------------------ | 214 | | 7 4 8 | - | - | 215 | | 4 8 5 | 8 | 8 | 216 | | 8 5 6 | - | 8 | 217 | | 5 6 1 | 6 | 8 | 218 | | 6 1 10 | - | 8 | 219 | | 1 10 3 | 10 | 10 | 220 | 221 | So, the function is supposed to set 10 to the addressed pointed by **ptr_to_greatest** and return 3. 222 | 223 | | Sequence | Return | *ptr_to_greatest | 224 | | ---------------- | ------ | ------------------ | 225 | | 7 4 8 5 6 1 10 3 | 3 | 10 | 226 | | 1 3 5 7 9 | 0 | *Not set anything* | 227 | | 8 6 11 7 4 | 1 | 11 | 228 | | 7 8 5 7 6 1 4 2 | 3 | 8 | 229 | 230 | ## References 231 | 232 | **\* ** http://www.csc.villanova.edu/~mdamian/Past/csc2400fa13/assign/plab.pdf 233 | 234 | -------------------------------------------------------------------------------- /week13/Solutions.md: -------------------------------------------------------------------------------- 1 | # Question 1 2 | 3 | ```c 4 | #include 5 | #include 6 | #include 7 | 8 | struct point{ 9 | int x; 10 | int y; 11 | }; 12 | 13 | float distance(struct point p1, struct point p2){ 14 | return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); 15 | } 16 | 17 | struct point closest_to_origin(struct point p1, struct point p2){ 18 | struct point origin = {0, 0}; 19 | float p1_dist = distance(p1, origin); 20 | float p2_dist = distance(p2, origin); 21 | return p1_dist < p2_dist ? p1 : p2; 22 | } 23 | 24 | int main() { 25 | struct point p1; 26 | struct point p2; 27 | scanf("%d%d", &p1.x, &p1.y); 28 | scanf("%d%d", &p2.x, &p2.y); 29 | 30 | // printf("%f", distance(p1, p2)); 31 | struct point closest = closest_to_origin(p1, p2); 32 | printf("%d %d", closest.x, closest.y); 33 | 34 | return 0; 35 | } 36 | ``` 37 | 38 | # Question 2 39 | 40 | ```c 41 | #include 42 | #include 43 | 44 | struct point{ 45 | int x; 46 | int y; 47 | }; 48 | 49 | struct rectangle{ 50 | struct point lower_left; 51 | struct point upper_right; 52 | }; 53 | 54 | 55 | void move(struct rectangle* p, int moveX, int moveY){ 56 | (*p).lower_left.x += moveX; 57 | (*p).upper_right.x += moveX; 58 | (*p).lower_left.y += moveY; 59 | (*p).upper_right.y += moveY; 60 | 61 | /* 62 | p->lower_left.x += moveX; 63 | p->upper_right.x += moveX; 64 | p->lower_left.y += moveY; 65 | p->upper_right.y += moveY; 66 | */ 67 | } 68 | 69 | int main() { 70 | struct rectangle r = {{2,0}, {8,4}}; 71 | 72 | /*struct point p1 = {2, 0}; 73 | struct point p2 = {8, 4}; 74 | struct rectangle r = {p1, p2};*/ 75 | 76 | 77 | /*struct point p1 = {2, 0}; 78 | struct point p2 = {8, 4}; 79 | struct rectangle r; 80 | r.lower_left = p1; 81 | r.upper_right = p2;*/ 82 | 83 | 84 | /*struct rectangle r; 85 | r.lower_left.x = 2; 86 | r.lower_left.y = 0; 87 | r.upper_right.x = 8; 88 | r.upper_right.y = 4;*/ 89 | 90 | 91 | move(&r, -5, 2); 92 | printf("%d,%d\n", r.lower_left.x, r.lower_left.y); 93 | printf("%d,%d", r.upper_right.x, r.upper_right.y); 94 | return 0; 95 | } 96 | ``` 97 | 98 | # Question 3 99 | 100 | ```c 101 | #include 102 | #include 103 | #include 104 | 105 | struct point{ 106 | int x; 107 | int y; 108 | }; 109 | 110 | float distance(struct point p1, struct point p2){ 111 | return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); 112 | } 113 | 114 | int main(){ 115 | int N, i, j; 116 | scanf("%d", &N); 117 | struct point points[N]; 118 | for(i = 0; i < N; i++){ 119 | scanf("%d%d", &points[i].x, &points[i].y); 120 | } 121 | 122 | float max = 0; 123 | int point1_ix, point2_ix; 124 | for(i = 0; i < N; i++){ 125 | for(j = i+1; j < N; j++){ 126 | float dist = distance(points[i], points[j]); 127 | if(dist > max){ 128 | max = dist; 129 | point1_ix = i; 130 | point2_ix = j; 131 | } 132 | } 133 | } 134 | 135 | printf("%d,%d\n", points[point1_ix].x, points[point1_ix].y); 136 | printf("%d,%d", points[point2_ix].x, points[point2_ix].y); 137 | 138 | return 0; 139 | } 140 | ``` 141 | 142 | ## Alternative 143 | 144 | ```c 145 | #include 146 | #include 147 | #include 148 | 149 | typedef struct point{ 150 | int x; 151 | int y; 152 | }Point_t; 153 | 154 | float distance(Point_t p1, Point_t p2){ 155 | return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); 156 | } 157 | 158 | void print_point(Point_t p){ 159 | printf("x: %d y: %d\n", p.x, p.y); 160 | } 161 | 162 | int main(){ 163 | int N, i, j; 164 | scanf("%d", &N); 165 | Point_t points[N]; 166 | for(i = 0; i < N; i++){ 167 | scanf("%d%d", &points[i].x, &points[i].y); 168 | } 169 | float max = 0; 170 | int point1_ix, point2_ix; 171 | for(i = 0; i < N; i++){ 172 | for(j = 0; j < i; j++){ 173 | float dist = distance(points[i], points[j]); 174 | if(dist > max){ 175 | max = dist; 176 | point1_ix = i; 177 | point2_ix = j; 178 | } 179 | } 180 | } 181 | print_point(points[point1_ix]); 182 | print_point(points[point2_ix]); 183 | 184 | return 0; 185 | } 186 | ``` 187 | 188 | # Question 4 189 | 190 | ```c 191 | #include 192 | #include 193 | 194 | // Fill the definition of the struct. 195 | typedef struct{ 196 | int student_id; 197 | float mt_grade; 198 | float final_grade; 199 | char letter_grade; 200 | } Student; 201 | 202 | void set_grade(Student *p){ 203 | int total_grade = (*p).mt_grade * 0.4 + (*p).final_grade * 0.6; 204 | if(total_grade > 75){ 205 | p->letter_grade = 'A'; 206 | }else if(total_grade > 50){ 207 | p->letter_grade = 'B'; 208 | }else if(total_grade > 25){ 209 | p->letter_grade = 'C'; 210 | }else{ 211 | p->letter_grade = 'F'; 212 | } 213 | } 214 | 215 | int main(){ 216 | int N; 217 | scanf("%d", &N); 218 | Student cmpe[N]; 219 | int i; 220 | for(i=0; i < N; i++){ 221 | scanf("%f%f", &cmpe[i].mt_grade, &cmpe[i].final_grade); 222 | cmpe[i].student_id = i; 223 | // Call set grade function 224 | set_grade(&cmpe[i]); 225 | } 226 | for(i=0; i < N; i++){ 227 | printf("%c\n", cmpe[i].letter_grade); 228 | } 229 | return 0; 230 | } 231 | ``` 232 | 233 | # Question 5 234 | 235 | ```c 236 | #include 237 | #include 238 | #include 239 | 240 | typedef struct flight{ 241 | int flight_no; 242 | char orig[4]; 243 | char dest[4]; 244 | int start; 245 | int end; 246 | } Flight_t; 247 | 248 | Flight_t read_flight(){ 249 | Flight_t fl; 250 | scanf("%d %s %s %d %d", &fl.flight_no, fl.orig, fl.dest, &fl.start, &fl.end); 251 | return fl; 252 | } 253 | 254 | void print_flight(Flight_t f){ 255 | printf("%d %s %s %d %d\n", f.flight_no, f.orig, f.dest, f.start, f.end); 256 | } 257 | 258 | int main() { 259 | int N, i; 260 | scanf("%d", &N); 261 | Flight_t flights[N]; 262 | for(i = 0; i < N; i++){ 263 | /*scanf("%d %s %s %d %d", &flights[i].flight_no, 264 | flights[i].orig, flights[i].dest, 265 | &flights[i].start, &flights[i].end); 266 | */ 267 | flights[i] = read_flight(); 268 | } 269 | char orig[4], dest[4]; 270 | scanf("%s %s", orig, dest); 271 | for(i = 0; i < N; i++){ 272 | if(strcmp(flights[i].orig, orig) == 0 && strcmp(flights[i].dest, dest) == 0){ 273 | print_flight(flights[i]); 274 | } 275 | } 276 | 277 | return 0; 278 | } 279 | 280 | ``` 281 | 282 | # Question 6 283 | 284 | ```c 285 | #include 286 | #include 287 | #include 288 | 289 | typedef struct{ 290 | int flight_no; 291 | char orig[4]; 292 | char dest[4]; 293 | int start; 294 | int end; 295 | } Flight_t; 296 | 297 | Flight_t read_flight(){ 298 | Flight_t fl; 299 | scanf("%d %s %s %d %d", &fl.flight_no, fl.orig, fl.dest, &fl.start, &fl.end); 300 | return fl; 301 | } 302 | 303 | void print_flight(Flight_t f){ 304 | printf("%d %s %s %d %d\n", f.flight_no, f.orig, f.dest, f.start, f.end); 305 | } 306 | 307 | int compare_flights(Flight_t f1, Flight_t f2){ 308 | if(f1.start == f2.start){ 309 | return strcmp(f1.orig, f2.orig); 310 | }else{ 311 | return f1.start - f2.start; 312 | } 313 | } 314 | 315 | int main() { 316 | int N, i, j; 317 | scanf("%d", &N); 318 | Flight_t flights[N]; 319 | for(i = 0; i < N; i++){ 320 | flights[i] = read_flight(); 321 | } 322 | 323 | for(i = 0; i < N; i++){ 324 | for(j = i+1; j < N; j++){ 325 | if(compare_flights(flights[i], flights[j]) > 0){ 326 | Flight_t temp = flights[i]; 327 | flights[i] = flights[j]; 328 | flights[j] = temp; 329 | } 330 | } 331 | } 332 | 333 | for(i=0; i < N; i++){ 334 | print_flight(flights[i]); 335 | } 336 | 337 | return 0; 338 | } 339 | 340 | ``` 341 | 342 | # Question 7 343 | 344 | ```c 345 | #include 346 | #include 347 | 348 | struct Person{ 349 | int age; 350 | int salary; 351 | }; 352 | 353 | typedef struct Family{ 354 | struct Person persons[9]; 355 | int personCount; 356 | } Fam_t; 357 | 358 | int poorest_family(Fam_t f[], int size){ 359 | int min_salary; 360 | int i, j, index = -1; 361 | for(i = 0; i < size; i++){ 362 | int total = 0; 363 | for(j = 0; j < f[i].personCount; j++){ 364 | total += f[i].persons[j].salary; 365 | } 366 | if(i == 0){ 367 | min_salary = total; 368 | }else if(total < min_salary){ 369 | min_salary = total; 370 | index = i; 371 | } 372 | } 373 | return index; 374 | } 375 | 376 | int richest_member(struct Family f){ 377 | int i, max = 0, index = -1; 378 | for(i = 0; i < f.personCount; i++){ 379 | if(f.persons[i].salary > max){ 380 | max = f.persons[i].salary; 381 | index = i; 382 | } 383 | } 384 | return index; 385 | } 386 | 387 | /* 388 | SAMPLE INPUT 389 | 3 390 | 10 0 33 5000 34 5500 391 | 5 392 | 10 10 30 1100 45 500 67 1000 5 0 393 | 2 394 | 48 4800 50 5000 395 | */ 396 | int main() { 397 | struct Family families[3]; 398 | int i, j; 399 | for(i = 0; i < 3; i++){ 400 | scanf("%d", &families[i].personCount); 401 | for(j = 0; j < families[i].personCount; j++){ 402 | scanf("%d%d", &families[i].persons[j].age, &families[i].persons[j].salary); 403 | } 404 | } 405 | int poorest = poorest_family(families, 3); 406 | int richest = richest_member(families[poorest]); 407 | printf("Richest person in the poorest family is %d years old.\n", families[poorest].persons[richest].age); 408 | return 0; 409 | } 410 | ``` 411 | 412 | -------------------------------------------------------------------------------- /week09/README.md: -------------------------------------------------------------------------------- 1 | # Week 9 - Arrays 2 | 3 | ## Brief Summary 4 | 5 | 6 | - An array is a group of memory locations. 7 | - These locations are related by the fact that they have all the **same name** and **same type**. 8 | - We can define an array as follows: 9 | - type arrName[size]; 10 | - Where, **type** is any data type like **int**, **float** etc. 11 | - **arrName** is the name of the array variable. 12 | - The square brackets contains an integer value **size**. It is the total number of values we can store in the array variable. 13 | - 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. 14 | 15 | - arrName[3] 16 | - Indices start from 0 and size -1 17 | 18 | ### Examples: 19 | 20 | - We want to create an array named **digit** of size **10** and type as **int**. 21 | 22 | ```c 23 | #include 24 | 25 | int main() 26 | { 27 | int digit[10]; 28 | 29 | return 0; 30 | } 31 | ``` 32 | 33 | - 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. 34 | 35 | 1588599881804 36 | 37 | 38 | 39 | * We can assign values directly to each element of the array: 40 | 41 | ```c 42 | #include 43 | 44 | int main() 45 | { 46 | int digit[10]; 47 | 48 | digit[3] = 123; 49 | 50 | return 0; 51 | } 52 | ``` 53 | 54 | 1588599907830 55 | 56 | 57 | 58 | ------ 59 | 60 | - We can read values from the user and store them in the array: 61 | 62 | ```c 63 | #include 64 | 65 | int main() 66 | { 67 | int digit[10]; 68 | 69 | scanf("%d", &digit[5]); // Assume user enters 13 70 | digit[0] = digit[5] * 2; 71 | 72 | return 0; 73 | } 74 | ``` 75 | 76 | 1588599935727 77 | 78 | 79 | 80 |
81 | 82 | ------ 83 | 84 | - Now, let's initialize our digit array with digits from 0 to 9 to: 85 | 86 | ```c 87 | #include 88 | 89 | int main() 90 | { 91 | int digit[10] = {0,1,2,3,4,5,6,7,8,9}; 92 | 93 | 94 | 95 | return 0; 96 | } 97 | ``` 98 | 99 | 1588600212155 100 | 101 | ------ 102 | 103 | - What if we don't initialize all the elements of the array: 104 | 105 | ```c 106 | #include 107 | 108 | int main() 109 | { 110 | int digit[10] = {0,1,2,3,4}; 111 | 112 | return 0; 113 | } 114 | ``` 115 | 116 | - Then the uninitialized elements will be automatically initialized to 0 (zero). 117 | 118 | 1588599975298 119 | 120 |
121 | 122 | ------ 123 | 124 | - Now, let's assign digits from 0 to 9 to the corresponding locations in our digit array with a loop: 125 | 126 | ```c 127 | #include 128 | 129 | int main() 130 | { 131 | int i; 132 | int digit[10]; 133 | 134 | for(i = 0; i < 10; i++) 135 | digit[i] = i; 136 | 137 | return 0; 138 | } 139 | ``` 140 | 141 | 1588600071903 142 | 143 |
144 | 145 | ------ 146 | 147 | - We can assign values to an array form the user with a for loop: 148 | 149 | ```c 150 | #include 151 | 152 | int main() 153 | { 154 | int i; 155 | int digit[10]; 156 | 157 | for(i = 0; i < 10; i++) 158 | scanf("%d", &digit[i]); // Assume the user enters 1 3 5 7 9 11 13 15 17 19 159 | 160 | return 0; 161 | } 162 | ``` 163 | 164 | 1588600093625 165 | 166 | 167 | 168 | ## Question 1 (Teaching Codes PS11) 169 | 170 | **a**. 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 and average of the elements. 171 | 172 | | Input | Output | 173 | | ----------------------------- | ----------- | 174 | | 10
1 2 3 4 5 6 7 8 9 10 | 55 5.500000 | 175 | | 4
4 1 3 8 | 16 4.000000 | 176 | 177 | **b**. Write a program that reads 10 elements from the user and store them in an array. Then prints elements larger than the average of numbers held in the array. 178 | 179 | | Input | Output | 180 | | ------------------------------ | ---------- | 181 | | 10
1 2 3 4 5 6 7 8 9 10 | 6 7 8 9 10 | 182 | | 4
4 1 3 8 | 8 | 183 | 184 | ## Question 2 (Teaching Codes Lab7) 185 | 186 | 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). 187 | 188 | Then, encrypt this sentence by adding +1 to the value of each character and print the encrypted version to the screen. 189 | 190 | Then decrypt this sentence by adding -1 to each character, and print the decrypted version to the screen. 191 | 192 | | Input | Output | 193 | | -------------- | --------------------------------- | 194 | | Hello Bogazici | Ifmmp!Cphb{jdj
Hello Bogazici | 195 | | Stay at home | Tubz!bu!ipnf
Stay at home | 196 | 197 | ## Question 3 (Teaching Codes Lab7) 198 | 199 | **a**. Write a program which reads an integer N from the user, then reads N integers from the user and store them in an array. Then print them to the screen in reverse order. Assume N is smaller than 50. 200 | 201 | **b.** Write a program which reads an integer N from the user, then reads N integers from the user and store them in an array. Then reverse the order of these integers in the array and print the array. 202 | 203 | | Input | Output | 204 | | --------------------- | -------------- | 205 | | 5
3 1 -4 5 2 | 2 5 -4 1 3 | 206 | | 6
15 7 2 89 8 12 | 12 8 89 2 7 15 | 207 | 208 |
Procedure 209 |

Let's say we have a array with length 7. Indices 0, 1, 2, 3, 4, 5, 6

210 |

We should swap the elements at index

211 |

0, 6

212 |

1, 5

213 |

2, 4

214 |

Pattern: i, size - i - 1

215 |
216 | 217 | ## Question 4 (Teaching Codes Lab7) 218 | 219 | 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. 220 | 221 | 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. 222 | 223 | | Input | Output | 224 | | ---------------------------------------------- | ------ | 225 | | 4 12
4 5 6 5
1 2 1 2 99 99 4 5 6 5 9 9 | 6 | 226 | | 3 5
2 2 2
1 2 2 2 5 | 1 | 227 | | 3 5
2 3 2
1 2 2 2 5 | | 228 | 229 |
Procedure 230 |

We must traverse the second array for the first array.

231 |

Starting from each element of the second array, traverse the first array.

232 |

First array variable: i, Second array variable j

233 |

j = 0, i = 0, 1, ..., N: compare i vs j+i: 0, 1, ..., N of second array

234 |

j = 1, i = 0, 1, ..., N: compare i vs j+i: 1, ..., N+1 of second array

235 |

...

236 |

j = M -N, i = 0, 1, ..., N: compare i vs j+i

237 |
238 | 239 | ## Question 5 240 | 241 | 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). 242 | 243 | | Input | Output | 244 | | ----------------------------- | ------------------------------------------ | 245 | | 10
5 10 2 5 50 5 10 1 2 2 | 1: 1
2: 3
5: 3
10: 2
50: 1 | 246 | 247 | ## Question 6 248 | 249 | **a.** Write a program reads two integers N1, N2 from the user, then reads N1 and N2 integers from the user to create two arrays. Then print common (included in both arrays) elements and their indices in the first array. 250 | 251 | | Input | Output | 252 | | ---------------------------------------------- | -------------------------- | 253 | | 6 8
2 5 10 53 17 8
-1 6 5 87 2 17 49 7 | 2 0
5 1
17 4
| 254 | 255 | **b.** Update or rewrite your program so that if the common element is included twice or more in the second array, print the common element and its index in the first array once. 256 | 257 | | Input | Output | 258 | | ----------------------------------------------- | -------------------- | 259 | | 6 8
2 5 10 53 17 8
-1 6 5 87 2 17 49 17 | 2 0
5 1
17 4 | 260 | 261 | ## Question 7 262 | 263 | Write a program which reads an integer N from the user, then reads N integers from the user to an array. Then modify the array so that it contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately 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. 264 | 265 | | Input | Output | 266 | | --------------------- | ------------- | 267 | | 4
1 3 1 4 | 1 3 4 1 | 268 | | 7
1 3 1 4 4 3 1 | 1 3 4 1 1 3 4 | 269 | | 4
3 2 2 4 | 3 4 2 2 | 270 | 271 | --------------------------------------------------------------------------------