├── W02 ├── w02-bit.jpg ├── w02-datatypes.png ├── E1.c ├── Q0.c ├── Q1.c └── README.md ├── W09 ├── W09-ptr1.png ├── W09-ptr2.png ├── W09-ptr3.png ├── Q0.123.c └── README.md ├── W10 ├── W10_01.PNG ├── W10_02.PNG ├── W10_03.PNG └── README.md ├── W03 ├── w03-ascii.png ├── Q02.c ├── Q1.c ├── Q2.c ├── Q01.c └── README.md ├── W04 ├── w04-ifelse.png ├── Q01.c ├── Q02.c ├── Q1.c ├── Q3.c ├── Q2.c └── README.md ├── W05 ├── w05-loops.png ├── do-while.c ├── while.c ├── for.c ├── Q1.c ├── Q02.c ├── Q01.c └── README.md ├── W06 ├── HW1.c ├── Q1.c ├── break.c ├── continue.c ├── Q2.c ├── HW2.c ├── Q01.c └── README.md ├── W11 ├── Q0.c ├── Q2.c ├── Q1.c ├── Q3.c ├── Q4.c └── README.md ├── W07 ├── Q02.c ├── sum_void_func.c ├── Q01.c ├── function_examples.c └── README.md ├── W10Lab ├── Q3.c ├── Q1.c ├── Q4.c ├── Q2.c ├── Q5.c └── README.md ├── W08 ├── Q1.c ├── Q2.c ├── Q02.c └── README.md ├── W12 ├── Q4.c ├── Q5.c ├── Q1.c ├── Q2.c ├── Q3.c └── README.md ├── W01 ├── README.md └── E1.c ├── W13 ├── Q1.c ├── Q2.c ├── Q3.c └── README.md ├── README.md ├── MT1-exercises └── README.md └── MT2-exercises └── README.md /W02/w02-bit.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W02/w02-bit.jpg -------------------------------------------------------------------------------- /W09/W09-ptr1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W09/W09-ptr1.png -------------------------------------------------------------------------------- /W09/W09-ptr2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W09/W09-ptr2.png -------------------------------------------------------------------------------- /W09/W09-ptr3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W09/W09-ptr3.png -------------------------------------------------------------------------------- /W10/W10_01.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W10/W10_01.PNG -------------------------------------------------------------------------------- /W10/W10_02.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W10/W10_02.PNG -------------------------------------------------------------------------------- /W10/W10_03.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W10/W10_03.PNG -------------------------------------------------------------------------------- /W03/w03-ascii.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W03/w03-ascii.png -------------------------------------------------------------------------------- /W04/w04-ifelse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W04/w04-ifelse.png -------------------------------------------------------------------------------- /W05/w05-loops.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W05/w05-loops.png -------------------------------------------------------------------------------- /W02/w02-datatypes.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hkmztrk/CMPE150/HEAD/W02/w02-datatypes.png -------------------------------------------------------------------------------- /W04/Q01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int ch1; 8 | scanf("%c", &ch1); 9 | 10 | printf("%c", ch1 -'A' + 1); 11 | 12 | 13 | 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /W05/do-while.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int i=0; 8 | 9 | do{ 10 | 11 | printf("%d", i); 12 | i++; 13 | 14 | } while(i < 0); 15 | 16 | 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /W05/while.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | 8 | char c = 'd'; 9 | 10 | 11 | while( c <= 'r'){ 12 | printf("%c \n", c); 13 | c++; 14 | 15 | } 16 | 17 | 18 | return 0; 19 | } 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /W06/HW1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num, sum=0; 6 | scanf("%d", &num); 7 | 8 | while(num>0){ 9 | sum +=num; 10 | scanf("%d", &num); 11 | } 12 | 13 | printf("%d", sum); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /W11/Q0.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int n, m; 6 | scanf ("%i %i", &n, &m); 7 | int i, j; 8 | 9 | for(i = 0; i < n; i ++) { 10 | for(j = 0; j < m; j ++) { 11 | printf("%i ", i * m + j); 12 | } 13 | printf("\n"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /W03/Q02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define PI 3.14159 //this is a constant variable 5 | 6 | int main(void) { 7 | 8 | int r; 9 | 10 | scanf("%d", &r); 11 | 12 | float cir = 2 * PI *r; 13 | float are = PI * r *r; 14 | 15 | printf("Cir = %.2f, Are= %.2f", cir, are); 16 | 17 | return 0; 18 | } 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /W05/for.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int i; 8 | char c = 'd'; 9 | 10 | for (i =0; i <= 10; i++){ 11 | printf("%d ", i); 12 | } 13 | printf("\n"); 14 | 15 | for (c= 'f'; c <= 'r'; c++){ 16 | printf("%c ", c); 17 | } 18 | 19 | 20 | 21 | 22 | return 0; 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /W02/E1.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | 7 | 8 | // int a, b, c = 5; this line assigns 5 only to c 9 | 10 | // int a =5, b=5, c = 5; this line assigns 5 to all variables 11 | int a, b, c; 12 | a = b = c = 5; 13 | 14 | printf("%d %d %d", a, b, c); 15 | 16 | 17 | 18 | return EXIT_SUCCESS; 19 | } 20 | -------------------------------------------------------------------------------- /W03/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | char ch1, ch2, ch3; 6 | scanf("%c%c%c", &ch1, &ch2, &ch3); 7 | 8 | printf("%c=%d, %c=%d, %c=%d", ch1, ch1, ch2, ch2, ch3, ch3); 9 | 10 | /* 11 | * %c is used to print the character 12 | * %d is used to print the ASCII number of the character 13 | */ 14 | 15 | 16 | 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /W07/Q02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(int a, int b) { 6 | 7 | int i, num; 8 | int flag = 1; 9 | scanf("%d", &num); 10 | 11 | for(i=2; i 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | 8 | int a,b; 9 | int i,j; 10 | scanf("%d %d",&a,&b); 11 | for(i=0; i 2 | #include 3 | 4 | void toplama(float a, float b); // this is a prototype 5 | 6 | 7 | int main(void) { 8 | 9 | int num1, num2; 10 | scanf("%d%d", &num1, &num2); 11 | 12 | toplama(num1, num2); 13 | 14 | return 0; 15 | } 16 | 17 | void toplama(float a, float b){ 18 | printf("%f", a + b); 19 | } 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /W03/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | //int dif = 'A' - 'a'; this gives us the constant difference between a lower case 6 | //printf("%d", dif); and upper case character in ASCII numbers 7 | // we can use any pair to find this difference. e.g. 'X' - 'x' 8 | 9 | 10 | char ch; 11 | scanf("%c", &ch); 12 | 13 | printf("%c, %d", ch - ('f'-'F')); 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /W04/Q02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | char ch1, ch2; 8 | scanf("%c %c", &ch1, &ch2); 9 | 10 | int mul = (ch1 - '0') * (ch2 - '0'); // here we subtract the ASCII of '0' from the ASCII of ch1 and ch2 11 | // e.g. ASCII '0' : 48, ASCII '5' : 53, '5' - '0' = 5 AND 53 - 48 = 5 12 | printf("%d", mul); 13 | 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /W03/Q01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int x, y, z; 8 | 9 | scanf("%d%d%d", &x, &y, &z); 10 | 11 | int old_x =x, old_y =y, old_z =z; 12 | // these variables keep the original values of x, y, z 13 | 14 | x = (x + y) * z 15 | y = (old_x * z*z) + y 16 | z = (z * 3 + old_y) - old_x 17 | 18 | printf("%d %d %d", x, y, z); 19 | 20 | return 0; 21 | } 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /W05/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int i=1; 8 | int a, b; 9 | scanf("%d%d", &a, &b); 10 | 11 | int res = 1; 12 | 13 | /*while( i <= b){ 14 | 15 | res *= a; 16 | //printf("Iteration %d, res= %d \n",i, res); 17 | i++; 18 | }*/ 19 | 20 | for(i=1; i <= b; i++){ 21 | res *= a; 22 | } 23 | 24 | printf("%d", res); 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /W11/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int func(int n, int m, int numbers[n][m], int k) { 5 | return numbers[k / m][k % m]; 6 | } 7 | 8 | int main(void) { 9 | int n, m, k; 10 | scanf ("%i %i %i", &n, &m, &k); 11 | int i, j; 12 | 13 | int numbers[n][m]; 14 | for(i = 0; i < n; i ++) { 15 | for(j = 0; j < m; j ++) { 16 | scanf("%i", &numbers[i][j]); 17 | } 18 | } 19 | 20 | printf("%i", func(n, m, numbers, k)); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /W10Lab/Q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | int n, i; 7 | 8 | scanf("%i", &n); 9 | 10 | int numbers[n]; 11 | 12 | for (i = 0; i < n; i ++) { 13 | scanf("%i", &numbers[i]); 14 | } 15 | 16 | for (i = 0; i < n - 1; i ++) { 17 | int difference = numbers[i] - numbers[i + 1]; 18 | if (difference < 0) { 19 | printf("%i ", -1 * difference); 20 | } 21 | else { 22 | printf("%i ", difference); 23 | } 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /W08/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char myFunc(int N){ 5 | //char result; 6 | int i,sum=0; 7 | for (i=1; i 6 | int main() 7 | { 8 | int i; 9 | float number, sum = 0; 10 | for(i=1; i <= 10; ++i){ 11 | 12 | scanf("%f",&num); 13 | 14 | if(num < 0) { 15 | break; // breaks from for loop 16 | } 17 | 18 | sum += num; 19 | } 20 | 21 | printf("%.2f",sum); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /W06/continue.c: -------------------------------------------------------------------------------- 1 | // Program to calculate the sum of a maximum of 10 numbers 2 | // Negative numbers are skipped from the calculation 3 | 4 | # include 5 | int main() 6 | { 7 | int i; 8 | float num, sum = 0; 9 | for(i=1; i <= 10; ++i) 10 | { 11 | scanf("%f",&num); 12 | 13 | if(number < 0){ 14 | continue; // skips the rest if number is < 0 15 | } 16 | 17 | sum += num; 18 | } 19 | printf("%.2f",sum); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /W02/Q0.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | 7 | // Version 1 8 | printf("*********\n"); 9 | printf("* *\n"); 10 | printf("*\t*\n"); 11 | printf("*\t*\n"); 12 | printf("*********\n"); 13 | 14 | 15 | // Version 2 16 | printf("*********\n" 17 | "*\t*\n" 18 | "*\t*\n" 19 | "*\t*\n" 20 | "*********\n"); 21 | 22 | 23 | // Version 3 24 | printf("*********\n*\t*\n*\t*\n*\t*\n*********\n"); 25 | 26 | 27 | 28 | return EXIT_SUCCESS; 29 | } 30 | -------------------------------------------------------------------------------- /W04/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | 8 | if (op == 1){ // == symbol asks whether two operands are equal or not 9 | printf("%d", num1+num2); 10 | } 11 | else if(op==2) 12 | { 13 | printf("%d", num1 - num2); 14 | } 15 | else if(op==3) 16 | { 17 | printf("%d", num1 * num2); 18 | } 19 | else if(op==4) 20 | { 21 | printf("%d", num1 / num2); 22 | } 23 | else{ 24 | printf("error"); 25 | } 26 | 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /W04/Q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | char ch1; 8 | scanf("%c", &ch1); 9 | 10 | 11 | if((ch1>= '0') && (ch1 <= '9') ){ 12 | printf("%d", (ch1 - '0') * 2); // Here the difference between the ASCII number of ch1 and '0' gives 13 | } // us the integer value of ch1 14 | else // ASCII '0' : 48, ASCII '4' : 52 ->>> 52 - 48 = 4 15 | { 16 | printf("error"); 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /W02/Q1.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | 7 | 8 | int a, b; 9 | scanf("%d %d", &a, &b); 10 | /* 11 | * it is important that you perform 12 | * the operations after reading, a, b 13 | * via scanf() 14 | */ 15 | 16 | int sum = a + b; 17 | int mul = a * b; 18 | float quo = a / (float)b; // here we performed type conversion 19 | int rem = a % b; 20 | 21 | printf("sum = %d, mul= %d, quo= %.1f, rem= %d", sum, mul, quo, rem ); 22 | 23 | 24 | 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /W06/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | 8 | char ch; 9 | scanf("%c", &ch); 10 | 11 | // Solution 1 12 | 13 | while(ch!='e' && ch!= 'E') 14 | { 15 | if (ch>='0' && ch <= '9'){ 16 | printf("%c", ch); 17 | } 18 | scanf("%c", &ch); 19 | } 20 | 21 | // Solution 2 22 | 23 | while(1){ 24 | if (ch=='e' || ch == 'E') 25 | break; 26 | if (ch>='0' && ch <= '9'){ 27 | printf("%c", ch); 28 | } 29 | scanf("%c", &ch); 30 | 31 | } 32 | 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /W07/Q01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int num, i, counter=0, fact_sum=0; 8 | scanf("%d", &num); 9 | 10 | 11 | while(num > 0){ 12 | int lastdigit = num % 10; 13 | 14 | if(lastdigit %2 ==1){ 15 | counter++; 16 | 17 | int fact = 1; 18 | for(i=2; i <= lastdigit; i++){ 19 | fact *=i; 20 | 21 | } 22 | 23 | fact_sum += fact; 24 | } 25 | 26 | num = num / 10; 27 | } 28 | 29 | printf("odd num=%d, sum of factorials=%d", counter, fact_sum); 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /W10Lab/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | int n, i; 7 | 8 | scanf("%i", &n); 9 | 10 | int numbers[n]; 11 | 12 | for (i = 0; i < n; i ++) { 13 | scanf("%i", &numbers[i]); 14 | } 15 | 16 | int sum = 0; 17 | 18 | for (i = 0; i < n; i ++) { 19 | sum = sum + numbers[i]; 20 | } 21 | 22 | int average = sum / n; 23 | 24 | for (i = 0; i < n; i ++) { 25 | printf("%i ", numbers[i] * average); 26 | } 27 | 28 | printf("\n"); 29 | 30 | for (i = 0; i < n; i ++) { 31 | printf("%i ", numbers[i] / average); 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /W12/Q4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i, n; 6 | scanf("%i ", &n); 7 | char string1[n + 1], string2[n + 1]; 8 | for (i = 0; i < n; i ++) { 9 | scanf("%c", &string1[i]); 10 | } 11 | string1[n] = '\0'; 12 | 13 | scanf(" "); // Not to count newline character \n. 14 | 15 | for (i = 0; i < n; i ++) { 16 | scanf("%c", &string2[i]); 17 | } 18 | string2[n] = '\0'; 19 | 20 | int count = 0; 21 | for (i = 0; string1[i] != '\0'; i ++) { 22 | if (string1[i] == string2[i]) { 23 | count = count + 1; 24 | } 25 | } 26 | 27 | printf("%i", count); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /W06/HW2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int num, i, sum=0; 6 | scanf("%d", &num); 7 | 8 | while(num>0){ 9 | int last_digit = num % 10; // here we find the individual integers 10 | // e.g. 24 -> 2 and 4 11 | int factorial = 1; 12 | 13 | for(i=2; i <=last_digit; i++) 14 | factorial *= i; 15 | 16 | sum += factorial; 17 | 18 | num = num /10; // e.g 24 becomes 2, 789 becomes 78 and so on.. 19 | 20 | 21 | } 22 | 23 | printf("%d", sum); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /W06/Q01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | 8 | 9 | char a, b; 10 | scanf("%c %c", &a, &b); 11 | char tempa=a, tempb=b; 12 | 13 | if(a >= b) 14 | { 15 | while(a>=b){ 16 | if(a!= 'a' && a!= 'e' && a!= 'i' && a!= 'o' && a!= 'u'){ 17 | printf("%c ", a); 18 | } 19 | a--; 20 | } 21 | } 22 | else{ 23 | 24 | while(a <= b){ 25 | if(a!= 'a' && a!= 'e' && a!= 'i' && a!= 'o' && a!= 'u'){ 26 | printf("%c ", a); 27 | } 28 | a++; 29 | } 30 | } 31 | 32 | printf("\n a=%c, b= %c ", a, b); 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /W10Lab/Q4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | int n, i; 7 | 8 | scanf("%i", &n); 9 | 10 | int numbers[n]; 11 | 12 | for (i = 0; i < n; i ++) { 13 | scanf("%i", &numbers[i]); 14 | } 15 | 16 | int mask[3]; 17 | 18 | for (i = 0; i < 3; i ++) { 19 | scanf("%i", &mask[i]); 20 | } 21 | 22 | for (i = 0; i < n; i ++) { 23 | int point = 0; 24 | if (i - 1 >= 0) { 25 | point = point + numbers[i - 1] * mask[0]; 26 | } 27 | point = point + numbers[i] * mask[1]; 28 | if (i + 1 < n) { 29 | point = point + numbers[i + 1] * mask[2]; 30 | } 31 | printf("%i ", point); 32 | } 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /W08/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int power(int x, int y){ 6 | int result = 1; 7 | int i=0; 8 | for(i = 0; i< y; i++){ 9 | result *= x; 10 | } 11 | return result; 12 | } 13 | 14 | int reverse(int x, int y){ 15 | int i,mod,sum = 0; 16 | for(i = 1; i <= y; i++){ 17 | mod = x%10; 18 | x = x/10; 19 | sum += mod*power(10,y-i); 20 | } 21 | x = x*power(10,y); 22 | x += sum; 23 | return x; 24 | } 25 | 26 | int main(void) { 27 | int n,m; 28 | scanf ("%d%d", &n, &m); 29 | printf("%d",reverse(n,m)); 30 | 31 | 32 | 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /W12/Q5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i, j, n, m; 6 | scanf("%i %i ", &n, &m); 7 | char string1[n + 1], string2[n + 1]; 8 | for (i = 0; i < n; i ++) { 9 | scanf("%c", &string1[i]); 10 | } 11 | string1[n] = '\0'; 12 | 13 | scanf(" "); 14 | 15 | for (i = 0; i < m; i ++) { 16 | scanf("%c", &string2[i]); 17 | } 18 | string2[m] = '\0'; 19 | 20 | int count = 0; 21 | for (i = 0; i <= n - m; i ++) { 22 | count = count + 1; 23 | for (j = 0; j < m; j ++) { 24 | if (string1[i + j] != string2[j]) { 25 | count = count - 1; 26 | break; 27 | } 28 | } 29 | } 30 | 31 | printf("%i", count); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /W01/README.md: -------------------------------------------------------------------------------- 1 | ## Week 1 2 | 3 | 4 | ### Materials: 5 | * [CMPE150 Official Sources](https://programming.cmpe.boun.edu.tr/) 6 | * [CMPE150 Lecture Slides](https://www.dropbox.com/sh/jemfn420lir6oq1/AADoyWd0nADuovf4iEr8JTega?dl=0) 7 | * [C Tutorial](http://www.learn-c.org/) 8 | * [Online C Compiler](https://www.onlinegdb.com/online_c_compiler) 9 | 10 | ### AI-related videos 11 | 12 | * [What is Artificial Intelligence (or Machine Learning)?](https://www.youtube.com/watch?v=mJeNghZXtMo) 13 | * [The incredible inventions of intuitive AI | Maurice Conti](https://youtu.be/aR5N2Jl8k14) 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /W01/E1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This 3 | * is a 4 | * comment 5 | * block 6 | */ 7 | 8 | #include //input-output (io) library 9 | #include 10 | 11 | 12 | int main(void) { 13 | 14 | 15 | printf("Hello world"); 16 | 17 | int firstVariable; // I declared an integer variable 18 | 19 | firstVariable = 13; // I assigned a value to the variable 20 | 21 | int secondVar = 7; // I initialized a variable with a value 22 | 23 | int mul=firstVariable+secondVar; 24 | 25 | printf("%d + %d = %d",firstVariable, secondVar , mul); 26 | 27 | /* %d indicates that 28 | * it will be replaced with an integer (int) 29 | */ 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /W11/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int n, m; 6 | scanf ("%i %i", &n, &m); 7 | int i, j; 8 | 9 | int numbers[n][m]; 10 | for(i = 0; i < n; i ++) { 11 | for(j = 0; j < m; j ++) { 12 | scanf("%i", &numbers[i][j]); 13 | } 14 | } 15 | 16 | // Rows 17 | for(i = 0; i < n; i ++) { 18 | int sum = 0; 19 | for(j = 0; j < m; j ++) { 20 | sum = sum + numbers[i][j]; 21 | } 22 | printf("%i ", sum); 23 | } 24 | 25 | printf("\n"); 26 | 27 | // Columns 28 | for(j = 0; j < m; j ++) { 29 | int sum = 0; 30 | for(i = 0; i < n; i ++) { 31 | sum = sum + numbers[i][j]; 32 | } 33 | printf("%i ", sum); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /W12/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int func(char string[], int k, char c) { 5 | int i; 6 | int sum = 0; 7 | for (i = 0; string[i] != '\0'; i ++) { 8 | if (string[i] == c) { 9 | sum = sum + 1; 10 | string[i] = string[i] + k; 11 | } 12 | } 13 | return sum; 14 | } 15 | 16 | int main(void) { 17 | int i, n, k, result; 18 | char c; 19 | scanf("%i %i %c ", &n, &k, &c); 20 | char string[n + 1]; 21 | for (i = 0; i < n; i ++) { 22 | scanf("%c", &string[i]); 23 | } 24 | string[n] = '\0'; 25 | 26 | result = func(string, k, c); 27 | printf("%i\n", result); 28 | for (i = 0; string[i] != '\0'; i ++) { 29 | printf("%c", string[i]); 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /W04/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | char ch1; 8 | scanf("%c", &ch1); 9 | 10 | if( (ch1>= 'a') && (ch1 <= 'z')){ // we make sure that the character is between 'a' and 'z', and thus a lower case char 11 | printf("%c", ch1 - ('a' - 'A')); // 'a' - 'A' is equal to the difference between every lower and upper case pair 12 | } 13 | else if((ch1>= 'A') && (ch1 <= 'Z') ){ 14 | printf("%c", ch1 + ('a' - 'A')); 15 | } 16 | else if((ch1>= '0') && (ch1 <= '9') ){ 17 | printf("digit"); 18 | } 19 | else{ 20 | printf("symbol"); 21 | } 22 | 23 | 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /W05/Q02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int degree; 8 | char symbol; 9 | 10 | scanf("%c %d", &symbol, °ree); 11 | 12 | if (symbol == 'F' || symbol == 'C'){ 13 | 14 | if (symbol == 'F'){ // here I convert Fahrenheit degree to Celcius first 15 | degree = (degree - 32) * 5 / 9; 16 | } 17 | 18 | if(degree < 21){ 19 | printf("cold"); 20 | } 21 | else if(degree >= 21 && degree <= 25){ 22 | printf("warm"); 23 | } 24 | else if (degree > 25){ 25 | printf("hot"); 26 | } 27 | 28 | 29 | } 30 | else{ 31 | 32 | printf("incorrect input."); 33 | 34 | } 35 | 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /W11/Q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func(int n, int m, int numbers[n][m], int k) { 5 | int i, j; 6 | for(i = 0; i < n; i ++) { 7 | int border = (i + 1) * k; 8 | if (border > m) { 9 | border = m; 10 | } 11 | int sum = 0; 12 | for(j = 0; j < border; j ++) { 13 | printf("%i ", numbers[i][j]); 14 | sum = sum + numbers[i][j]; 15 | } 16 | printf("%i\n", sum); 17 | } 18 | } 19 | 20 | int main(void) { 21 | int n, m, k; 22 | scanf ("%i %i %i", &n, &m, &k); 23 | int i, j; 24 | 25 | int numbers[n][m]; 26 | for(i = 0; i < n; i ++) { 27 | for(j = 0; j < m; j ++) { 28 | scanf("%i", &numbers[i][j]); 29 | } 30 | } 31 | 32 | func(n, m, numbers, k); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /W08/Q02.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | 6 | int factorial(int p){ 7 | int i, res = 1; 8 | 9 | for(i=2; i <=p ;i++){ 10 | res *=i; 11 | } 12 | 13 | return res; 14 | } 15 | 16 | int fact_sum(int sayi){ 17 | 18 | int fact_sum = 0; 19 | 20 | while(sayi > 0){ 21 | 22 | int ld = sayi % 10; 23 | fact_sum += factorial(ld); 24 | 25 | sayi = sayi / 10; 26 | 27 | } 28 | return fact_sum; 29 | 30 | } 31 | 32 | 33 | 34 | int main(void) { 35 | 36 | int sayi; 37 | int fact_sum = 0; 38 | 39 | scanf("%d", &sayi); 40 | 41 | 42 | printf("%d", fact_sum); 43 | 44 | return 0; 45 | } 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /W11/Q4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func(int n, int m, int numbers[n][m], int a, int b) { 5 | int j; 6 | for(j = 0; j < m; j ++) { 7 | int temp = numbers[a][j]; 8 | numbers[a][j] = numbers[b][j]; 9 | numbers[b][j] = temp; 10 | } 11 | } 12 | 13 | int main(void) { 14 | int n, m, a, b; 15 | scanf ("%i %i %i %i", &n, &m, &a, &b); 16 | int i, j; 17 | 18 | int numbers[n][m]; 19 | for(i = 0; i < n; i ++) { 20 | for(j = 0; j < m; j ++) { 21 | scanf("%i", &numbers[i][j]); 22 | } 23 | } 24 | 25 | func(n, m, numbers, a, b); 26 | 27 | for(i = 0; i < n; i ++) { 28 | for(j = 0; j < m; j ++) { 29 | printf("%i ", numbers[i][j]); 30 | } 31 | printf("\n"); 32 | } 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /W12/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i, n; 6 | char c; 7 | scanf("%i %c ", &n, &c); 8 | char string[n + 1]; 9 | for (i = 0; i < n; i ++) { 10 | scanf("%c", &string[i]); 11 | } 12 | string[n] = '\0'; 13 | 14 | int sum = 0; 15 | for (i = 0; string[i] != '\0'; i ++) { 16 | if (string[i] == c) { 17 | sum = sum + 1; 18 | } 19 | } 20 | 21 | int j; 22 | char deleted[n - sum + 1]; 23 | for (i = 0; string[i] != '\0'; i ++) { 24 | if (string[i] != c) { 25 | deleted[j] = string[i]; 26 | j = j + 1; 27 | } 28 | } 29 | deleted[n - sum] = '\0'; 30 | 31 | for (i = 0; deleted[i] != '\0'; i ++) { 32 | printf("%c", deleted[i]); 33 | } 34 | 35 | printf("\n%i", n - sum); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /W10Lab/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | 6 | int n, i; 7 | 8 | scanf("%i", &n); 9 | 10 | int numbers[n]; 11 | 12 | for (i = 0; i < n; i ++) { 13 | scanf("%i", &numbers[i]); 14 | } 15 | 16 | int reversed[n]; 17 | 18 | for (i = n - 1; i >= 0; i --) { 19 | reversed[n - i - 1] = numbers[i]; 20 | } 21 | 22 | for (i = 0; i < n; i ++) { 23 | printf("%i ", reversed[i]); 24 | } 25 | 26 | printf("\n"); 27 | 28 | // You may read an additional number such is k to replace 3. 29 | for (i = 0; i < n; i = i + 3) { 30 | printf("%i ", reversed[i]); 31 | } 32 | 33 | /* 34 | for (i = 0; i < n; i ++) { 35 | if (i % 3 == 0) { 36 | printf("%i ", reversed[i]); 37 | } 38 | } 39 | */ 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /W09/Q0.123.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | void cubeByReference(int *n){ 6 | *n = *n * *n * *n; 7 | } 8 | 9 | void swap(int *x, int *y){ // x = 3, y = 5 10 | *x = *x + *y; // x = 8, y = 5 11 | *y = *x - *y; // x = 8, y = 3 12 | *x = *x - *y; // x = 5, y = 3 13 | } 14 | 15 | void divideWithRemainder(int m, int n, int *result, int *remainder) { 16 | *result = m / n; 17 | *remainder = m - n * *result; 18 | } 19 | 20 | int main(void) { 21 | int m, n; 22 | scanf ("%i %i", &m, &n); 23 | cubeByReference(&m); 24 | printf("m: %i n: %i\n", m, n); 25 | swap(&m, &n); 26 | printf("m: %i n: %i\n", m, n); 27 | int result, remainder; 28 | divideWithRemainder(m, n, &result, &remainder); 29 | printf("result: %i remainder: %i", result, remainder); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /W12/Q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i, n; 6 | char c; 7 | scanf("%i %c ", &n, &c); 8 | char string[n + 1]; 9 | for (i = 0; i < n; i ++) { 10 | scanf("%c", &string[i]); 11 | } 12 | string[n] = '\0'; 13 | 14 | int sum = 0; 15 | for (i = 0; string[i] != '\0'; i ++) { 16 | if (string[i] == c) { 17 | sum = sum + 1; 18 | } 19 | } 20 | 21 | int j; 22 | char duplicated[n + sum + 1]; 23 | for (i = 0; string[i] != '\0'; i ++) { 24 | if (string[i] != c) { 25 | duplicated[j] = string[i]; 26 | j = j + 1; 27 | } 28 | else { 29 | duplicated[j] = string[i]; 30 | duplicated[j + 1] = string[i]; 31 | j = j + 2; 32 | } 33 | } 34 | duplicated[n + sum] = '\0'; 35 | 36 | for (i = 0; duplicated[i] != '\0'; i ++) { 37 | printf("%c", duplicated[i]); 38 | } 39 | 40 | printf("\n%i", n + sum); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /W13/Q1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct player { 5 | char id[4]; 6 | int size; 7 | int year; 8 | }; 9 | 10 | int func(int n, int a1, int a2, int b1, int b2, struct player players[n]) { 11 | int i; 12 | int count = 0; 13 | for (i = 0; i < n; i ++) { 14 | if (players[i].size <= a1 && players[i].size >= a2 && players[i].year <= b1 && players[i].year >= b2) { 15 | printf("%s\n", players[i].id); 16 | count = count + 1; 17 | } 18 | } 19 | return count; 20 | } 21 | 22 | int main(void) { 23 | int n, i, a1, a2, b1, b2; 24 | scanf("%i %i %i %i %i", &n, &a1, &a2, &b1, &b2); 25 | 26 | struct player players[n]; 27 | 28 | for (i = 0; i < n; i ++) { 29 | scanf("%s %i %i", &players[i].id, &players[i].size, &players[i].year); 30 | } 31 | /* 32 | for (i = 0; i < n; i ++) { 33 | printf("%s %i %i\n", players[i].id, players[i].size, players[i].year); 34 | } 35 | */ 36 | 37 | printf("%i", func(n, a1, a2, b1, b2, players)); 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /W10Lab/Q5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int func(int n, int numbers[], int *most) { 5 | 6 | int i; 7 | int counter = 0; 8 | int max = 0; 9 | 10 | for (i = 0; i < n - 1; i ++) { 11 | if (numbers[i] == numbers[i + 1]) { 12 | counter = counter + 1; 13 | // With >, you will find and return the first maximum occurence if the array contains more than one maximum occurences. 14 | // Change it to >= to find and return last maximum occurence. 15 | if (counter > max) { 16 | max = counter; 17 | *most = numbers[i]; 18 | } 19 | } 20 | else { 21 | counter = 0; 22 | } 23 | } 24 | 25 | return max + 1; 26 | } 27 | 28 | int main(void) { 29 | 30 | int n, i; 31 | 32 | scanf("%i", &n); 33 | 34 | int numbers[n]; 35 | 36 | for (i = 0; i < n; i ++) { 37 | scanf("%i", &numbers[i]); 38 | } 39 | 40 | int most = 0; 41 | 42 | int max = func(n, numbers, &most); 43 | 44 | printf("%i times %i", max, most); 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /W13/Q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct item_pair { 5 | int price1; 6 | int price2; 7 | int budget; 8 | }; 9 | 10 | int func(int n, struct item_pair items[n]) { 11 | int i; 12 | int counter = 0; 13 | for (i = 0; i < n; i ++) { 14 | if (items[i].budget >= items[i].price1 + items[i].price2) { 15 | counter = counter + 2; 16 | printf("Both items\n"); 17 | } 18 | else if (items[i].budget >= items[i].price1) { 19 | counter = counter + 1; 20 | printf("Item 1\n"); 21 | } 22 | else if (items[i].budget >= items[i].price2) { 23 | counter = counter + 1; 24 | printf("Item 2\n"); 25 | } 26 | else { 27 | printf("No buy\n"); 28 | } 29 | } 30 | 31 | return counter; 32 | } 33 | 34 | int main(void) { 35 | int n, i; 36 | scanf("%i ", &n); 37 | 38 | struct item_pair items[n]; 39 | for (i = 0; i < n; i ++) { 40 | scanf("%i %i %i", &items[i].price1, &items[i].price2, &items[i].budget); 41 | } 42 | 43 | printf("%i", func(n, items)); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /W13/Q3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct point { 5 | int x; 6 | int y; 7 | }; 8 | 9 | struct square { 10 | struct point p; 11 | int length; 12 | }; 13 | 14 | int area(struct square mySquare) { 15 | return mySquare.length * mySquare.length; 16 | } 17 | 18 | int perimeter(struct square mySquare) { 19 | return mySquare.length * 4; 20 | } 21 | 22 | struct point right_down(struct square mySquare) { 23 | struct point p; 24 | p.x = mySquare.p.x + mySquare.length; 25 | p.y = mySquare.p.y - mySquare.length; 26 | return p; 27 | } 28 | 29 | int main(void) { 30 | 31 | struct point p; 32 | struct square mySquare; 33 | 34 | scanf("%i %i %i", &p.x, &p.y, &mySquare.length); 35 | mySquare.p = p; 36 | 37 | printf("%i %i %i\n", mySquare.p.x, mySquare.p.y, mySquare.length); 38 | 39 | printf("Area: %i Perimeter: %i\n", area(mySquare), perimeter(mySquare)); 40 | 41 | struct point p_right_down = right_down(mySquare); 42 | 43 | printf("Right Down: %i %i", p_right_down.x, p_right_down.y); 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /W05/Q01.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | int main(void) { 6 | 7 | int num1, num2, result; 8 | char op1, op2; 9 | char choosen_op; 10 | scanf("%d%d %c %c", &num1, &num2, &op1, &op2); 11 | 12 | if (num1< 0 || num2 < 0){ 13 | printf("incorrect input."); 14 | } 15 | else{ 16 | /* here we decided which operator we will choose*/ 17 | /* and then we saved that operator to a new variable*/ 18 | if (num1 % num2 == 0){ 19 | choosen_op = op1; 20 | } 21 | else{ 22 | choosen_op = op2; 23 | } 24 | 25 | /*and now we can perform the math operations*/ 26 | /* based on the value in choosen_op */ 27 | 28 | if(choosen_op == '+'){ 29 | result = num1 + num2; 30 | } 31 | else if (choosen_op == '-'){ 32 | result = num1 - num2; 33 | } 34 | else if (choosen_op == '*'){ 35 | result = num1 * num2; 36 | } 37 | else if (choosen_op == '/'){ 38 | result = num1 / num2; 39 | } 40 | 41 | printf("%d", result); 42 | 43 | } 44 | 45 | 46 | return 0; 47 | } 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CMPE150 2 | Bogazici University, CMPE150 (Introduction to Computing C) lab exercises will be shared weekly. 3 | 4 | **Email:** hakime.ozturk@boun.edu.tr 5 | **Email:** emre.bilgili@boun.edu.tr 6 | 7 | ### Other CMPE150 repos 8 | * [CMPE150.02](https://github.com/suyunu/c-notes) 9 | * [CMPE150.03](https://github.com/gokceuludogan/cmpe150-fall19) 10 | 11 | ## Week02 12 | 13 | * printf() 14 | * scanf() 15 | * int, float 16 | 17 | 18 | ## Week03 19 | 20 | * char 21 | 22 | ## Week04 23 | * **control structures:** if ... else 24 | * **control structures:** switch ... case 25 | 26 | ## Week05 27 | 28 | * **loops:** while, do-while, for 29 | 30 | 31 | ## Week06 32 | 33 | * **loops:** while, do-while, for 34 | 35 | 36 | 37 | ## Week07 38 | 39 | * **functions:** 40 | 41 | ## Week08 42 | 43 | * **functions and pointers:** 44 | 45 | ## Week09 46 | 47 | * **pointers and arrays:** 48 | 49 | ## Week10 50 | 51 | * **arrays:** 52 | 53 | ## Week12 54 | 55 | * **string:** 56 | 57 | ## Week13 58 | 59 | * **multi-dimensional arrays and struct:** 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /W07/function_examples.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //// version1 5 | void isPrime_void(int sayi){ 6 | 7 | int i; 8 | int flag = 1; 9 | 10 | for(i=2; i 9 | 10 | ### Updating the value of the variable pointer [ points to ] 11 | 12 | 13 | 14 | ### Updating the address (the variable) pointer [ points to ] 15 | 16 | 17 | 18 | ### Q0.1 19 | 20 | 21 | Write a function named **cubeByReference()** 22 | 23 | **cubeByReference** takes an integer pointer and computes the cube of the integer. (does not return anything.) 24 | 25 | 26 | ### Q0.2 27 | 28 | Write a function named **swap** which takes two integer pointers (addresses of two integers) and swaps the values in those two addresses. 29 | 30 | 31 | In the main program, create two integer variables a and b, then read their values from the user. 32 | 33 | 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. 34 | 35 | 36 | ### Q0.3 37 | 38 | Write a function named **divideWithRemainder** which takes two integer values: 39 | 40 | number and divisor, and 41 | two integer pointers (addresses of two integers): result and remainder. 42 | 43 | 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. 44 | 45 | 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. 46 | 47 | 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. 48 | 49 | Note that this function doesn't need to return anything, because it is already able to give output to the given addresses. 50 | 51 | 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. 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /W10/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## W10 // 27.11.2019 3 | 4 | 5 | ## Arrays 6 | 7 | 8 | 9 | ### Inserting and element 10 | 11 | 12 | 13 | ### Inserting the result of an operation 14 | 15 | 16 | 17 | ### Q1 18 | 19 | Read 10 elements from the user and store them in an array. Then find largest and smallest elements of the array. 20 | 21 | input: 7 90 2 4 12 63 11 29 33 1 22 | output: Max:90 Min:1 23 | 24 | 25 | ### Q2 26 | 27 | 28 | Write a program which reads an integer N from the user, then reads N integers from the user. Then your program should put these N integers into reverse order and print them to the screen. Assume N is smaller than 50. 29 | 30 | 31 | input: 8 32 | 1 2 4 3 5 6 8 7 33 | output: 7 8 6 5 3 4 2 1 34 | 35 | 36 | ### Q3 37 | 38 | 39 | 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). 40 | 41 | Encrypt this sentence by adding +1 to the value of each character. Then print the encrypted version to the screen. 42 | 43 | Decrypt this sentence by adding -1 to each character, and print the decrypted version again to the screen. 44 | 45 | | INPUT | OUTPUT | 46 | | --------- |:---------:| 47 | | Hello Bogazici | Ifmmp!Cphb{jdj Hello Bogazici | 48 | | Programming is fun | Qsphsbnnjoh!jt!gvo Programming is fun | 49 | 50 | 51 | ### Q4 52 | 53 | Write a program which reads an integer N from the user, then reads N integers from the user. Compute the sum of the absolute value of the difference between each array element and its index. 54 | 55 | input: 10 56 | -3 -2 -1 0 1 2 3 4 5 6 57 | output: 30 58 | 59 | 60 | ### Homework-3 61 | 62 | Write a program which reads an integer N from the user, then reads N integers from the user. Then program reads two more integers, low and high. Write a program that finds the largest element in a sub-array of an integer array. The sub-array consists of the array cells indexed between indices low and high, **inclusive**. 63 | 64 | input: 12 65 | 34 59 69 19 83 25 36 23 36 33 17 99 66 | 3 8 67 | 68 | output: 83 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /MT1-exercises/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## MT1 Exercises 3 | 4 | ### Q1 5 | 6 | Write a program that takes three numbers as input from the user and finds out whether one of the three numbers is the arithmetic mean of the other two. For example: 7 | 8 | input: 7,15,11 9 | output: 11 is the mean of 7 and 15 10 | 11 | input: 27,23,31 12 | output: 27 is the mean of 23 and 31 13 | 14 | input: 7,8,10 15 | output: No number is mean of the other two 16 | 17 | 18 | ### Q2 19 | 20 | Write a program that takes two integer inputs: hour (integer 0–23) and minute (integer 0–59). 21 | 22 | - Give an error message and stop if either integer is outside the required range. 23 | - Print the 24-hour time along with the equivalent 12-hour (AM/PM) time. 24 | - The times should include leading zeros as necessary, so that it prints 03:08 rather than 3:8 for 3:08 AM. 25 | 26 | 27 | input: 11 15 28 | output: 11:15 is 11:15 AM 29 | 30 | input: 17 58 31 | output: 17:58 is 05:58 PM 32 | 33 | input: -5 12 34 | output: "Error: hour out of range." 35 | 36 | input: 6 70 37 | output: "Error: minute out of range." 38 | 39 | 40 | 41 | ### Q3 42 | 43 | Write a program that reads a positive integer K and write a sequence using the rules given follow: 44 | 45 | set the first element of sequence to K. 46 | compute the next element as: 47 | half of the previous if previous is even 48 | three times the previous, plus one if previous is odd. 49 | 50 | Program terminates when the sequence reaches 1. 51 | 52 | input: 50 53 | output: 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 54 | 55 | 56 | 57 | ### Q4 58 | 59 | Write a program which reads a character C, if the character C is not a digit your program should read one more integer (A) and print its square (A^2). 60 | 61 | If the character C is a digit, your program should convert it to integer and print 2*C. 62 | 63 | | INPUT | OUTPUT | 64 | | --------- |:---------:| 65 | | h 5 | 25 | 66 | | 3 | 6 | 67 | | + 7 6 | 49| 68 | | 2 c | 4| 69 | 70 | 71 | Notice that in some of the example inputs there are some extra characters or integers. If entered in one line, your program should not take the extra input into account. 72 | 73 | 74 | 75 | 76 | 77 | ### Q5 78 | 79 | Write a program which reads characters until the user enters two consecutive letters 'xy'. After reading each character your program should print it. Reading 'y' after 'x' should stop printing and also terminate your program. 80 | 81 | 82 | | INPUT | OUTPUT | 83 | | --------- |:---------:| 84 | | Hello how are 123 yxy | Hello how are 123 yx | 85 | | yyyyyyxy | yyyyyyx| 86 | | 12342x4533y21xy | 12342x4533y21x | 87 | 88 | 89 | -------------------------------------------------------------------------------- /MT2-exercises/README.md: -------------------------------------------------------------------------------- 1 | ### Q1 2 | Read n as the size of the array. 3 | Then, read n integers and fill the array. 4 | Then, read an integer as k and assume it is positive. 5 | Send them to a function named "func" and return the counter as the following. 6 | Inside the function, count how many different pairs' absolute difference is equals k. 7 | For any i and j: |numbers[i] - numbers[j]| = k 8 | 9 | Input: 10 | 7 11 | 0 1 2 3 4 5 6 12 | 3 13 | Output: 14 | 4 15 | 16 | Input: 17 | 8 18 | 4 6 8 9 11 1 2 5 19 | 7 20 | Output: 21 | 3 22 | 23 | Input: 24 | 6 25 | 5 8 6 9 2 3 26 | 8 27 | Output: 28 | 0 29 | 30 | Input: 31 | 4 32 | 1 9 1 9 33 | 8 34 | Output: 35 | 4 36 | 37 | ### Q2 38 | Read n as the size of the array. 39 | Then, read n integers and fill the array. 40 | Print sum of the first n elements. 41 | Print sum of the first n - 1 elements. 42 | Print sum of the first n - 2 elements. 43 | ... 44 | Print sum of the first 3 elements. 45 | Print sum of the first 2 elements. 46 | Print sum of the first 1 elements. 47 | Then, print like above but in reverse order. 48 | 49 | Input: 50 | 10 51 | 0 1 2 3 4 5 6 7 8 9 52 | 53 | Output: 54 | 45 36 28 21 15 10 6 3 1 0 55 | 0 1 3 6 10 15 21 28 36 45 56 | 57 | Input: 58 | 10 59 | 3 -2 -8 7 6 5 0 1 -1 9 60 | 61 | Output: 62 | 20 11 12 11 11 6 0 -7 1 3 63 | 3 1 -7 0 6 11 11 12 11 20 64 | 65 | ### Q3 66 | Read n as the size of the array. 67 | Then, read n integers and fill the array. 68 | Then, read an integer as k and assume it is positive. 69 | Send them to a function named "func" and return the sum as the following. 70 | Divide the array into some parts sized k. 71 | Print multiplication of the elements inside current part and do so for all parts. 72 | Return the sum of these multiplications. 73 | 74 | Input: 75 | 7 76 | 0 1 2 3 4 5 6 77 | 3 78 | Output: 79 | 0 60 6 80 | 66 81 | 82 | Input: 83 | 12 84 | 5 3 2 6 4 1 8 0 5 6 8 9 85 | 4 86 | Output: 87 | 180 0 2160 88 | 2340 89 | 90 | Input: 91 | 8 92 | 9 1 5 -4 -2 7 -1 7 93 | 5 94 | Output: 95 | 360 -49 96 | 311 97 | 98 | ### Q4 99 | Read n as the size of the array. 100 | Then, read n integers and fill the array. 101 | Then, read an integer as m as starting index. 102 | Then, read an integer as k and assume it is positive. 103 | Send them to a void function named "func". 104 | You will modify the array. 105 | Perform following operation k times. 106 | Starting direction is right. 107 | Check the current element in the index m. 108 | Take module of the current element with 3. 109 | If it is 1, set direction to right, else if it is 2, set direction to left and in both cases, set value to 0 of the current index. 110 | In the next operation, m must be m + 1 or m - 1 according to direction. 111 | If you go out of the array, jump other end of the array. 112 | 113 | Input: 114 | 7 115 | 0 1 2 3 4 5 6 116 | 6 117 | 8 118 | Output: 119 | 0003406 120 | 121 | Input: 122 | 16 123 | 5 2 6 8 9 15 23 0 16 43 8 7 1 64 32 17 124 | 10 125 | 33 126 | Output: 127 | 5 2 6 0 9 15 0 0 0 0 0 0 0 0 0 0 128 | -------------------------------------------------------------------------------- /W06/README.md: -------------------------------------------------------------------------------- 1 | 2 | # W06 Notes // 30.10.19 3 | 4 | ### Warm Up 5 | 6 | ### Q0.1 7 | 8 | Write a program that reads two characters and displays from first character to second **excluding vowels**. 9 | 10 | input: c p 11 | output: c d f g h j k l m n p 12 | 13 | input: p c 14 | output: p n m l k j h g f d c 15 | 16 | #### Continue and break 17 | 18 | ```c 19 | #include 20 | 21 | int main(){ 22 | 23 | /* Continue */ 24 | int i = 0; 25 | while ( i <= 10) 26 | { 27 | if (condition ) // do NOT forget the indendation, it makes your code readable 28 | continue; // skips the rest of the code and continues the next iteration 29 | 30 | i +=1; 31 | } 32 | 33 | /* Break */ 34 | int i =0; 35 | while ( i <= 10) 36 | { 37 | if (condition ) // do NOT forget the indendation, it makes your code readable 38 | break; // terminates the loop and you are outside of while 39 | 40 | i +=1; 41 | } 42 | } 43 | ``` 44 | 45 | 46 | ### Q1 47 | Write a program which reads two integers N and M (both smaller than 15, greater than 1) 48 | 49 | then prints a rectangle with size NxM which has stars (*) at borders, and lines (-) inside. 50 | 51 | 52 | input: 3 3 53 | output: *** 54 | *-* 55 | *** 56 | 57 | input: 4 8 58 | output: ******** 59 | *------* 60 | *------* 61 | ******** 62 | 63 | ### Q2 64 | 65 | Write a program to read a character input in a loop. Print only numbers and exit when pressed **e** or **E**. 66 | 67 | | INPUT | OUTPUT | 68 | | --------- |:---------:| 69 | | a8bc!ge | 8 | 70 | | xyzzz123xttE | 123 | 71 | | xyz1e2rr3xttE | 1 | 72 | 73 | ### Q3 74 | 75 | Write a program which reads an integer (smaller than 1000000) then prints out how many of the digits are odd numbers. 76 | 77 | 78 | | INPUT | OUTPUT | 79 | | --------- |:---------:| 80 | | 23423 | 2 | 81 | | 24602 | 0 | 82 | | 5 | 1 | 83 | 84 | ### HW-1 85 | 86 | Write a program that will take integers as inputs until the user enters a negative number. Then shows the user the sum of all the non-negative numbers he/she entered. 87 | 88 | 89 | | INPUT | OUTPUT | 90 | | --------- |:---------:| 91 | | 4 7 45 9 2 0 0 5 8 -4 | 80 | 92 | | 1 1 0 1 -1 | 3 | 93 | 94 | ### HW-2 95 | 96 | Write a program that reads an integer value, and computes the sum of the factorial of each digit . 97 | The output will be an integer. 98 | 99 | | INPUT | OUTPUT | 100 | | --------- |:---------:| 101 | | 24 | 26 | 102 | | 122 | 5 | 103 | | 254 | 146 | 104 | 105 | ### HW-3 106 | 107 | Write a program that will take integers until the user enters an odd number. Then print the average of the even numbers. 108 | - Assume only positive numbers will be entered by the user. 109 | 110 | 111 | | INPUT | OUTPUT | 112 | | --------- |:---------:| 113 | | 464221 | 3.6 | 114 | | 22121 | 2 | 115 | | 1 | 0 | 116 | -------------------------------------------------------------------------------- /W02/README.md: -------------------------------------------------------------------------------- 1 | 2 | # W02 Notes // 02.10.19 3 | 4 | ## Warm up 5 | 6 | ### Q0 7 | 8 | Write a program to print the following rectangle using only ```printf()``` function (Height:5*, Width:9*). 9 | 10 | ********* 11 | * * 12 | * * 13 | * * 14 | ********* 15 | 16 | 17 | ## Bits and Bytes??? 18 | 19 | 20 | 21 | ## Data Types 22 | 23 | 24 | 25 | #### If an integer type is stored in N bits: 26 | #### * Signed range: -2N-1 and 2N-1 -1 27 | #### * Unsigned range: 0 to 2 N - 1. 28 | 29 | #### e.g. int 30 | #### 4 bytes = 32 bits 31 | #### - int: -2 31 and 2 31-1 (-1 because of 0) 32 | #### - unsigned int: 0 to 2 32 -1 33 | 34 | ## The overflow 35 | 36 | ```c 37 | #include 38 | int main() 39 | { 40 | // First, let's take a look at the sizes of data types 41 | printf("size of char = %d\n", sizeof(int)); 42 | printf("size of char = %d\n", sizeof(long)); 43 | printf("size of char = %d\n", sizeof(long long)); 44 | 45 | 46 | int a; // declaration 47 | a = 2147483647; // assignment of largest int 48 | printf("%d\n", a+1); 49 | return 0; 50 | } 51 | ``` 52 | 53 | ## Type conversion 54 | 55 | ```c 56 | #include 57 | int main() 58 | { 59 | int i=3; 60 | float f; 61 | 62 | f = i; 63 | printf("i=%d f=%f\n", i, f); 64 | 65 | f=7.69; 66 | i = f; 67 | printf("i=%d f=%f\n", i, f); 68 | 69 | i = 4.5; 70 | f = i; 71 | printf("i=%d f=%f", i, f); 72 | 73 | return 0; 74 | } 75 | ``` 76 | 77 | ## Reading input: scanf() 78 | 79 | ```c 80 | #include 81 | int main() 82 | { 83 | // Declare variables 84 | int number_1, number_2, number_3; 85 | 86 | //Read first value 87 | scanf("%d", &number_1); 88 | //Read second value 89 | scanf("%d", &number_2); 90 | 91 | // OR 92 | scanf("%d%d", &number_1, &number_2); 93 | 94 | 95 | number_3 = number_1 + number_2; 96 | // Display output 97 | printf("%d + %d = %d\n", number_1, number_2, number_3); 98 | return 0; 99 | } 100 | ``` 101 | 102 | ### Q1 103 | 104 | Write a program that **reads** two integers and then calculates and prints: 105 | 106 | sum 107 | multiplication 108 | quotient 109 | remainder 110 | 111 | input: 23 12 112 | output: sum = 35, mul = 276, quo = 1.9, rem = 11 113 | 114 | ### Homework-1 115 | 116 | Write a program to find the area and the circumference of a circle. Read $r$ from the user and define $pi$ as a constant variable. 117 | 118 | pi = 3.14159 119 | 120 | input: 5 121 | output: Cir= 31. 41, Are= 49.34 122 | 123 | Print only two decimal points. 124 | 125 | ### Homework-2 126 | 127 | Write a program to read two floating numbers a and b, and print the result of the following operation: 128 | 129 | (3*a+b)*2 130 | 131 | input: 3.8 1.2 132 | output: 25.20 133 | 134 | Print only two decimal points. 135 | 136 | 137 | ### Homework-3 138 | 139 | Write a program that reads an integer from the user and then prints the following rectangle using only ```printf()``` function (Height:5*, Width:9*). 140 | 141 | 142 | 143 | input: 9 144 | 145 | 999999999 146 | 9 9 147 | 9 9 148 | 9 9 149 | 999999999 150 | 151 | 152 | -------------------------------------------------------------------------------- /W10Lab/README.md: -------------------------------------------------------------------------------- 1 | ## W10 // 27.11.2019 2 | 3 | ### Q1 4 | 5 | Read n as the size of the array. 6 | Then, read n integers and fill the array. 7 | Find average of the array. Perform integer division. 8 | Print each element multiplication with average. 9 | Print each element division with average. 10 | 11 | Input: 12 | 10 13 | 0 1 2 3 4 5 6 7 8 9 14 | 15 | Output: 16 | 0 4 8 12 16 20 24 28 32 36 17 | 0 0 0 0 1 1 1 1 2 2 18 | 19 | 20 | Input: 21 | 15 22 | 2 6 7 45 10 25 9 7 81 50 31 26 3 14 51 23 | 24 | Output: 25 | 48 144 168 1080 240 600 216 168 1944 1200 744 624 72 336 1224 26 | 0 0 0 1 0 1 0 0 3 2 1 1 0 0 2 27 | 28 | 29 | ### Q2 30 | 31 | Read n as the size of the array. 32 | Then, read n integers and fill the array. 33 | Create a new array named reversed and fill it as reverse of the numbers. 34 | Print the reversed array. 35 | Print every 3rd element in the reversed array starts from first element. 36 | 37 | Input: 38 | 10 39 | 0 1 2 3 4 5 6 7 8 9 40 | 41 | Output: 42 | 9 8 7 6 5 4 3 2 1 0 43 | 9 6 3 0 44 | 45 | 46 | Input: 47 | 12 48 | 81 52 16 8 0 11 2 83 9 61 43 20 49 | 50 | Output: 51 | 20 43 61 9 83 2 11 0 8 16 52 81 52 | 20 9 11 16 53 | 54 | 55 | ### Q3 56 | 57 | Read n as the size of the array. 58 | Then, read n integers and fill the array. 59 | Print absolute difference between each consecutive numbers. 60 | So, you will print (n - 1) positive numbers. 61 | 62 | Input: 63 | 10 64 | -5 5 0 1 2 3 -3 -2 -1 10 65 | 66 | Output: 67 | 10 5 1 1 1 6 1 1 11 68 | 69 | 70 | Input: 71 | 14 72 | 9 15 -6 4 95 66 54 -22 31 17 48 -23 86 74 73 | 74 | Output: 75 | 6 21 10 91 29 12 76 53 14 31 71 109 12 76 | 77 | 78 | ### Q4 79 | 80 | Read n as the size of the array. 81 | Then, read n integers and fill the array. 82 | Then, read a 3-sized new array named mask. 83 | Print element-wise multiplication of the array and the mask. (This is applying the mask.) 84 | Consider value 0 for indices those are out of array. 85 | Middle element of the mask must overlap with the current index. 86 | For example: 87 | 3 88 | 5 6 7 89 | 1 2 3 90 | Print a b c 91 | a = 0x1 + 5x2 + 6x3 92 | b = 5x1 + 6x2 + 7x3 93 | c = 6x1 + 7x2 + 0x3 94 | So, print 28 38 20 95 | 96 | Input: 97 | 10 98 | 0 1 2 3 4 5 6 7 8 9 99 | -1 2 -1 100 | 101 | Output: 102 | -1 0 0 0 0 0 0 0 0 10 103 | 104 | 105 | Input: 106 | 10 107 | 3 -2 -8 7 6 5 0 1 -1 9 108 | 1 -2 3 109 | 110 | Output: 111 | -12 -17 35 -4 10 -4 8 -5 30 -19 112 | 113 | 114 | 115 | ### Q5 116 | 117 | Read n as the size of the array. 118 | Then, read n integers and fill the array. 119 | Write a function named func as the following. 120 | Count occurrences and find the element that occurs the most time consecutively. 121 | The function takes int n, int numbers[] and int *most 122 | *most indicates the number itself and the function returns how many times the most number occurs. 123 | 124 | Input: 125 | 10 126 | 3 3 3 4 4 4 4 2 2 1 127 | 128 | Output: 129 | 4 times 4 130 | 131 | 132 | Input: 133 | 10 134 | 1 1 2 3 4 5 6 7 8 9 135 | 136 | Output: 137 | 2 times 1 138 | 139 | 140 | Input: 141 | 17 142 | 25 25 12 12 12 0 0 0 0 0 25 25 25 25 25 25 25 143 | 144 | Output: 145 | 7 times 25 146 | -------------------------------------------------------------------------------- /W07/README.md: -------------------------------------------------------------------------------- 1 | 2 | # W07 Notes // 06.11.19 3 | 4 | ### Warm Up 5 | 6 | ### Q0.1 7 | 8 | Write a program which reads an integer (smaller than 1000000) then prints out how many of the digits are odd numbers. 9 | 10 | 11 | | INPUT | OUTPUT | 12 | | --------- |:---------:| 13 | | 23423 | 2 | 14 | | 24602 | 0 | 15 | | 5 | 1 | 16 | 17 | ### Q0.2 18 | 19 | Write a program that checks whether the given number is prime. 20 | 21 | | INPUT | OUTPUT | 22 | | --------- |:---------:| 23 | | 11 | 1 | 24 | | 4 | 0 | 25 | | 13 | 1 | 26 | 27 | 28 | ## Functions 29 | [source here](https://www.codeanalogies.com/javascript-functions-explained) 30 | 31 | ### What is a function? 32 | Let's think about the general concept of cooking with a recipe first. Using a recipe means that: 33 | 34 | You start with a specific set of ingredients 35 | You perform a specific procedure using those ingredients 36 | You will get a reliable product at the end 37 | 38 | **A function is also a reusable recipe that performs the same set of actions over and over again on a set of ingredients.** 39 | 40 | Those **ingredients** are called **parameters**. 41 | 42 | ### Function types 43 | 44 | functions with RETURN -> create a new value 45 | 46 | functions WITHOUT return -> create a new format 47 | 48 | 49 | ### Void function 50 | 51 | #### Example-1 52 | ```c 53 | #include 54 | 55 | // I am **declaring** a function. 56 | void boilWater() { // no parameters 57 | printf("Boiling water."); 58 | } 59 | 60 | int main(){ 61 | boilWater(); // I am **calling** the function. 62 | 63 | return 0; 64 | } 65 | ``` 66 | 67 | #### Example-2 68 | 69 | ```c 70 | #include 71 | void boilWater(int water, float degree){ // I have two parameters 72 | printf("Boiling %d liters of water at %f C degree.", water, degree); 73 | } 74 | 75 | int main(){ 76 | int water = 1; 77 | float deg = 120; 78 | boilWater(water, degree); // I am **sending arguments**. 79 | 80 | return 0; 81 | } 82 | 83 | ``` 84 | 85 | ### Functions with return 86 | 87 | #### Example-3 88 | 89 | ```c 90 | #include 91 | int divide(float num1, float num2){ // I have two parameters 92 | return num1/num2; 93 | } 94 | 95 | int main(){ 96 | float a,b; 97 | scanf("%f%f", &a, &b); 98 | float result = divide(a, b); // I am **sending arguments**. 99 | printf("%f", result); 100 | 101 | return 0; 102 | } 103 | 104 | ``` 105 | 106 | ### Q1 107 | 108 | Write a function **isPrime(int num)** that checks whether the given number is prime. Function takes a single parameter that is the integer we want to check. 109 | 110 | Functions returns: 111 | 1 if prime 112 | 0 if not 113 | 114 | ### HW-1 115 | 116 | Write a function named **printAsTime** which reads hours, minutes, and seconds as three integers, 117 | then prints it to the screen as shown in the examples. 118 | 119 | This function shouldn't return anything. 120 | Assume no invalid input will be given (like 92 minutes or -2 hours). 121 | 122 | | INPUT | OUTPUT | 123 | | --------- |:---------:| 124 | | 5 12 3 | 05:12:03 | 125 | | 3 0 22 | 03:00:22 | 126 | 127 | ### HW-2 128 | 129 | Write a function named **factorial** that takes an integer as a parameter and computes and **returns** the factorial of that integer. 130 | 131 | Write a program to compute the summation of each digits' factorial using factorial function. 132 | 133 | 134 | | INPUT | OUTPUT | 135 | | --------- |:---------:| 136 | | 4 | 24 | 137 | | 3 | 6 | 138 | | 43 | 30| 139 | 140 | -------------------------------------------------------------------------------- /W03/README.md: -------------------------------------------------------------------------------- 1 | 2 | # W03 Notes // 09.10.19 3 | 4 | ## Warm up 5 | 6 | ### Q0.1 7 | 8 | Write a program that reads three integers x, y and z. Then, the values of x, y and z are updated as follows: 9 | 10 | x = (x + y) * z 11 | y = (x * z^2) + y 12 | z = (z * 3 + y) - x 13 | 14 | **Be careful about using the initial values of x, y, and z while calculating their new values.** 15 | Print the new values of x, y, z. 16 | 17 | input: 2 3 5 18 | output: 25 53 16 19 | 20 | ### Q0.2 21 | 22 | Write a program to find the area and the circumference of a circle. Read $r$ from the user and define $pi$ as a constant variable. 23 | 24 | pi = 3.14159 25 | 26 | input: 5 27 | output: Cir= 31. 41, Are= 78.53 28 | 29 | Print only two decimal points. 30 | 31 | 32 | ### Constant variable 33 | ```c 34 | #define constant_name constant_value 35 | ``` 36 | 37 | ### Operators: ( +=, -=, *=, /=) 38 | 39 | 40 | ```c 41 | #include 42 | 43 | int main(){ 44 | int x=5, y=3; 45 | x +=y /* same as x = x + y */ 46 | printf("x:%d\n",x); 47 | 48 | y *= x /* same as y = x * y */ 49 | printf("y:%d x:%d\n",y,x); 50 | 51 | return 0; 52 | } 53 | ``` 54 | 55 | ### Operators: (++, --) 56 | 57 | ```c 58 | #include 59 | 60 | int main(){ 61 | int n = 5, x; 62 | n++; /* n = 6 */ 63 | printf("n:%d\n",n); 64 | // n = n+1 OR n += 1 OR n++ 65 | 66 | x=n++; 67 | printf("x:%d n:%d\n",x,n); 68 | /* equals to following two commands: 69 | * x = n; 70 | * n++; 71 | */ 72 | 73 | x=n--; 74 | printf("x:%d n:%d\n",x,n); 75 | /* equals to following two commands: 76 | * x = n; 77 | * n--; 78 | */ 79 | 80 | x=++n; 81 | printf("x:%d n:%d\n",x,n 82 | /* equals to following two commands: 83 | * n++; 84 | * x = n; 85 | */ 86 | 87 | x=--n; 88 | printf("x:%d n:%d\n",x,n); 89 | /* equals to following two commands: 90 | * n--; 91 | * x = n; 92 | */ 93 | 94 | 95 | return 0; 96 | } 97 | ``` 98 | 99 | ## Char Data Type (%c) 100 | 101 | ### [W3Schools](http://www.w3schools.com) is a good resource for programming! 102 | 103 | 104 | 105 | 106 | ## Char is special!! It is a character and an integer at the same time! 107 | 108 | ```c 109 | #include 110 | 111 | int main(){ 112 | int a=5002; 113 | 114 | char ch='0'; /* ASCII code of '0' is 48 */ 115 | printf("ch:%c ascii of ch:%d\n",ch,ch); 116 | 117 | ch=ch+3; /* ch = '3', ASCII code becomes 51 */ 118 | printf("ch:%c ascii of ch:%d\n",ch,ch); 119 | 120 | a+=ch; /* a = 5053 */ 121 | printf("a:%d\n",a); 122 | 123 | ch+=a; /* ch = ?, overflow and undefined behavior occurs */ 124 | printf("ch:%c ascii of ch:%d\n",ch,ch); 125 | 126 | return 0; 127 | } 128 | ``` 129 | 130 | ### Q1 131 | Write a program that reads a character variable from the user then prints the character and its ASCII code. 132 | 133 | input: x y z 134 | output: x 120, y 121 , z 122 135 | 136 | ### Q2 137 | 138 | Write a program which reads a lower-case char (character) from the user, then prints its upper-case version on the screen. 139 | 140 | e.g. 141 | input: b 142 | output: B 143 | 144 | ### HW-1 145 | 146 | Write a program that reads an upper case letter, then prints its position in the alphabet. 147 | 148 | | INPUT | OUTPUT | 149 | | --------- |:---------:| 150 | | A | 1| 151 | | H | 8| 152 | 153 | 154 | ### HW-2 155 | 156 | Write a program that reads two numeric **characters** and prints their multiplication. 157 | 158 | | INPUT | OUTPUT | 159 | | --------- |:---------:| 160 | | 9 8 | 72| 161 | | 1 3 | 3| 162 | 163 | 164 | ```c 165 | #include 166 | 167 | int main(){ 168 | 169 | char ch1, ch2; 170 | scanf("%c %c", &ch1, &ch2); 171 | 172 | /* 173 | * YOUR SOLUTION HERE 174 | * 175 | */ 176 | 177 | return 0; 178 | } 179 | ``` 180 | 181 | -------------------------------------------------------------------------------- /W05/README.md: -------------------------------------------------------------------------------- 1 | 2 | # W05 Notes // 23.10.19 3 | 4 | ## Warm up 5 | 6 | ### Q0.1 7 | 8 | 9 | Write a program that will **read** 2 integers and 2 characters from the user: 10 | 11 | - if the first number is divisible by the second number, your should perform the operation indicated with the first operator, 12 | 13 | - if it is not divisible, then print the result of the operation indicated with the second operator. 14 | 15 | - if one of your numbers is nonpositive, print "Incorrect input" 16 | 17 | 18 | | INPUT | OUTPUT | 19 | | --------- |:---------:| 20 | |45 5 + * | 50 | 21 | |45 6 + - | 39 | 22 | |-34 17 * + | incorrect input | 23 | 24 | **Note:** Assume that for characters, only the following four operators will be used, +, -, *, /. 25 | 26 | ### Q0.2 27 | 28 | Write a program to print the state of the weather based on the following conditions: 29 | 30 | - if smaller 21 Celcius degree -> cold 31 | - if between 21 - 25 Celcius degree -> warm 32 | - if bigger than 25 Celcius degree -> hot 33 | 34 | Your program should read a character (F or C) and a float value from the user and perform a conversion if the degree is given in Fahrenheit (F). 35 | 36 | F 77 means that you have to convert Fahrenheit 77 degrees to Celcius (25) degrees. 37 | 38 | Celcius = (Fahrenheit - 32) * 5 / 9 39 | 40 | | INPUT | OUTPUT | 41 | | --------- |:---------:| 42 | |F 77 | warm | 43 | |C 32 | hot | 44 | |X 25 | incorrect input | 45 | 46 | 47 | ### Ternary operator 48 | 49 | condition ? value_if_true : value_if_false 50 | 51 | ```c 52 | #include 53 | int main(){ 54 | 55 | int a = 10, b = 20, c; 56 | c = (a < b) ? a : b; 57 | 58 | /* 59 | int a = 1, b = 2, ans; 60 | if (a == 1) { 61 | if (b == 2) { 62 | ans = 3; 63 | } else { 64 | ans = 5; 65 | } 66 | } else { 67 | ans = 0; 68 | } 69 | 70 | ans = (a == 1 ? (b == 2 ? 3 : 5) : 0); 71 | */ 72 | 73 | return 0; 74 | 75 | } 76 | ``` 77 | 78 | ## Loops 79 | 80 | 81 | 82 | ```c 83 | #include 84 | 85 | int main(){ 86 | 87 | /* WHILE LOOP */ 88 | i = 0; // i is the loop control variable, initialize before using 89 | while ( i <= 10) // executes 0 or more times, each execution is called iteration 90 | { 91 | /* statements */ 92 | i +=1; // do NOT forget to update the control variable 93 | 94 | } 95 | 96 | /* FOR LOOP */ 97 | int j; // declare the control variable 98 | for ( j = 0; j <= 10; j++) // control variable initialization, control statement, and update 99 | { // in the same line 100 | /* statements*/ 101 | 102 | } 103 | 104 | /* DO-WHILE LOOP */ 105 | int z=0; // initialize the control variable 106 | do // executes 1 or more times!! 107 | { 108 | /* statements */ 109 | z +=1; 110 | 111 | } while ( z <= 10 ); // do not forget ; 112 | 113 | } 114 | ``` 115 | 116 | 117 | ### Q1 118 | 119 | Write a program that takes 2 integers a and b, then prints the result of a^b (power). 120 | 121 | | INPUT | OUTPUT | 122 | | --------- |:---------:| 123 | | 3 4 | 81 | 124 | | 2 10 | 1024 | 125 | 126 | 127 | ### HW-1 128 | 129 | Write a program that reads two characters and displays from first character to second **excluding vowels**. 130 | 131 | input: c p 132 | output: c d f g h j k l m n p 133 | 134 | input: p c 135 | output: p n m l k j h g f d c 136 | 137 | ### HW-2 138 | 139 | Write a program that reads two numbers and prints the numbers that can be divided to 11 between them (including the numbers themselves) 140 | 141 | | INPUT | OUTPUT | 142 | | --------- |:---------:| 143 | | 11 77 | 11 22 33 44 55 66 77 | 144 | | 8 16 | 11 | 145 | | 2 7 | | 146 | 147 | ### HW-3 148 | 149 | Write a program that will take an integer N, then prints N! (N factorial). 150 | 151 | 152 | | INPUT | OUTPUT | 153 | | --------- |:---------:| 154 | | 5 | 120 | 155 | | 3 | 6 | 156 | 157 | -------------------------------------------------------------------------------- /W04/README.md: -------------------------------------------------------------------------------- 1 | 2 | # W04 Notes // 16.10.19 3 | 4 | ## Warm up 5 | 6 | ### Q0.1 7 | 8 | Write a program that reads an upper case letter, then prints its position in the alphabet. 9 | 10 | | INPUT | OUTPUT | 11 | | --------- |:---------:| 12 | | A | 1| 13 | | H | 8| 14 | 15 | 16 | ### Q0.2 17 | 18 | Write a program that reads two numeric **characters** and prints their multiplication. 19 | 20 | | INPUT | OUTPUT | 21 | | --------- |:---------:| 22 | | 9 8 | 72| 23 | | 1 3 | 3| 24 | 25 | 26 | ```c 27 | #include 28 | 29 | int main(){ 30 | 31 | char ch1, ch2; 32 | scanf("%c %c", &ch1, &ch2); 33 | 34 | /* 35 | * YOUR SOLUTION HERE 36 | * 37 | */ 38 | 39 | return 0; 40 | } 41 | ``` 42 | 43 | 44 | ## Control Statements: If-else, Switch case 45 | 46 | 47 | 48 | #### Comparison operators 49 | 50 | < :(less than) 51 | > :(greater than) 52 | <= :(less than or equal to: ≤) 53 | >= :(greater than or equal to: ≥) 54 | != :(not equal to: ≠) 55 | == :(equal to: important! writing = instead of == is a common mistake) 56 | 57 | ### Q1 58 | 59 | Write a program to that reads three integers, in which first two integers are the components of a mathematical operation and the third one indicates the operation itself. Print the result of the corresponding operation. 60 | 61 | 1: sum, 2: substract, 3: multiplication, 4: division otherwise: error 62 | 63 | | INPUT | OUTPUT | 64 | | --------- |:---------:| 65 | | 34 7 2 | 27| 66 | | 12 8 3 |96| 67 | | 113 21 1 | 134| 68 | | 113 21 9 | error| 69 | 70 | 71 | 72 | ### Switch Case 73 | 74 | ```c 75 | #include 76 | 77 | int main(){ 78 | 79 | int num1, num2, choice; 80 | scanf("%d%d%d",&num1, &num2, &choice); 81 | 82 | switch (choice){ 83 | case 1: 84 | printf("%d", num1+num2); 85 | break; 86 | case 2: 87 | printf("%d", num1-num2); 88 | break; 89 | case 3: 90 | printf("%d", num1*num2); 91 | break; 92 | case 4: 93 | printf("%d", num1/num2); 94 | break; 95 | 96 | default: printf("error"); 97 | } 98 | 99 | 100 | /* OR an alternative solution with the operator as a character: 101 | * 102 | *int num1, num2; 103 | *char choice; 104 | *scanf("%d%d %c",&num1, &num2, &choice); 105 | * 106 | *switch (choice){ 107 | * case '+': 108 | * printf("%d", num1+num2); 109 | * break; 110 | * case '-': 111 | * printf("%d", num1-num2); 112 | * break; 113 | * case '*': 114 | * printf("%d", num1*num2); 115 | * break; 116 | * case '/': 117 | * printf("%d", num1/num2); 118 | * break; 119 | * 120 | * default: printf("error"); 121 | } 122 | 123 | 124 | return 0; 125 | } 126 | ``` 127 | 128 | ### Q2 129 | 130 | Write a program to convert upper-case to lower-case and vice versa. 131 | 132 | | INPUT | OUTPUT | 133 | | --------- |:---------:| 134 | | d | D| 135 | | H | h| 136 | | 9 | digit| 137 | | * |symbol| 138 | 139 | 140 | ### Q3 141 | Read one character as input and check if it is a digit: 142 | 143 | If so, convert it to an integer and display twice that number 144 | o/w display an error message 145 | 146 | | INPUT | OUTPUT | 147 | | --------- |:---------:| 148 | | 9 | 18| 149 | | b | error| 150 | 151 | ### HW-1 152 | 153 | Write a program that converts the time, which is given in seconds, to hh:mm:ss format. 154 | 155 | Note: If a value is smaller than 10, you must print 0 next to it. 156 | i.e.: 9:3:45 is invalid, it must be 09:03:45 157 | 158 | 159 | | INPUT | OUTPUT | 160 | | --------- |:---------:| 161 | |18456 | 05:07:36 | 162 | 163 | 164 | ### HW-2 165 | 166 | 167 | Write a program that will take 2 numbers from the user: 168 | 169 | - if the first number is divisible by the second number, your code should print "it is divisible", 170 | 171 | - if it is not divisible, then print "it is not divisible". 172 | 173 | - if one of your numbers is nonpositive, print "Incorrect input" 174 | 175 | 176 | 177 | | INPUT | OUTPUT | 178 | | --------- |:---------:| 179 | |45 5 | divisible| 180 | | 45 6 | not divisible | 181 | | -34 17 | incorrect input | 182 | 183 | 184 | ### HW-3 185 | 186 | Write a program to read the three numbers from the user and find and display the largest number among them. 187 | 188 | input: 3 24 8 189 | output: 24 190 | -------------------------------------------------------------------------------- /W12/README.md: -------------------------------------------------------------------------------- 1 | ## W12 // 11.12.2019 2 | 3 | Strings or just char arrays. 4 | 5 | #### How to declare? 6 | char string1[] = {'W', 'e', 'l', 'c', 'o', 'm', 'e', '\0'}; 7 | Do not forget to put the first character as \0. 8 | 9 | char string1[] = "Welcome"; 10 | This is equivalent to the above statement. Double quote puts the first character itself. 11 | 12 | for (i = 0; string1[i] != '\0'; i ++) { 13 | printf("%c ", string1[i]); 14 | } 15 | 16 | You can also print number of the characters from {0 - 255}. 17 | 18 | for (i = 0; string1[i] != '\0'; i ++) { 19 | printf("%c (%i) ", string1[i], string1[i]); 20 | } 21 | 22 | Little insteresting thing. 23 | As you realize, we put a string inside "printf" function. 24 | You can declare this string before the "printf" and give this string to "printf". 25 | 26 | char print[] = "%i %.2f %c\n"; 27 | printf(print, 4, 5.25, 'e'); 28 | printf(print, 5, 7.25, 'f'); 29 | 30 | #### How to read from the console? 31 | Char by char... Do not forget that, spaces and pressing enter (new line character, \n) counts as a character, too. 32 | 33 | int n; 34 | scanf("%i ", &n); 35 | char string2[n + 1]; 36 | for (i = 0; i < n; i ++) { 37 | scanf("%c", &string2[i]); 38 | } 39 | string2[n] = '\0'; 40 | 41 | for (i = 0; string2[i] != '\0'; i ++) { 42 | printf("%c", string2[i]); 43 | } 44 | 45 | Or you can read and print a word. Do not forget that any space or pressing enter (new line character, \n) finishes reading. 46 | 47 | char string3[100]; // Or you can put n here and read the size from the console. 48 | scanf("%s", &string3); 49 | printf("%s", string3); 50 | return 0; 51 | 52 | Or you can use built-in functions. 53 | "gets" function fills the given char array with your input and it does not put the first character \0. 54 | "fgets" function reads 9 characters in the example and puts a \0 to the end. 55 | Parameter 10 might be smaller then 10 for example 7. At that point, it will read only 6 characters and put a \0 to the end. 56 | Both functions can read space character. 57 | "puts" function prints the given string. 58 | It is better to select one of these read and write operations from the options and get used to it. 59 | 60 | char string4[10]; 61 | // gets(string4); 62 | fgets(string4, 10, stdin); 63 | puts(string4); 64 | 65 | ### Q1 66 | Read integer n, integer k and char c. 67 | Then, read a n-sized string (char array). 68 | Send string, k and c to a function. 69 | In the function, count and return how many times character c appears in the string. 70 | And whenever you see a character c in the string, add k to that character c. 71 | 72 | Input: 73 | 12 6 c 74 | caabcacbbcac 75 | Output: 76 | 5 77 | iaabiaibbiai 78 | 79 | Input: 80 | 16 -3 d 81 | fhdgaehbacfddea 82 | Output: 83 | 3 84 | fhagaehbacfaaea 85 | 86 | ### Q2 87 | Read integer n as the size of string and read character c. 88 | Then, read a n-sized string (char array). 89 | Create a new string and fill it with the characters of old string. 90 | But, do not copy characters c (delete them). 91 | Realize that, size of the new string will be shorter than old string. 92 | Print the new string. 93 | Print size of the new string. 94 | 95 | Input: 96 | 7 a 97 | aabcaba 98 | Output: 99 | bcb 100 | 3 101 | 102 | Input: 103 | 14 a 104 | bcabcaabccbaba 105 | Output: 106 | bcbcbccbb 107 | 9 108 | 109 | ### Q3 110 | Read integer n as the size of string and read character c. 111 | Then, read a n-sized string (char array). 112 | Create a new string and fill it with the characters of old string. 113 | But, copy characters c twice (duplicate them). 114 | Realize that, size of the new string will be longer than old string. 115 | Print the new string. 116 | Print size of the new string. 117 | 118 | Input: 119 | 7 a 120 | aabcaba 121 | Output: 122 | aaaabcaabaa 123 | 11 124 | 125 | Input: 126 | 14 a 127 | bcabcaabccbaba 128 | Output: 129 | bcaabcaaaabccbaabaa 130 | 19 131 | 132 | ### Q4 133 | Read integer n as the size of strings. 134 | Then, read a n-sized string1 (char array). 135 | Then, read a n-sized string2 (char array). 136 | Go through both strings and count the same characters in the matching indices. 137 | 138 | Input: 139 | 7 140 | aabbaab 141 | aabbaab 142 | Output: 143 | 7 144 | 145 | Input: 146 | 9 147 | abcbacaab 148 | bacabcbab 149 | Output: 150 | 4 151 | 152 | ### Q5 153 | Read integer n and m as the size of strings. 154 | Then, read a n-sized string1 (char array). 155 | Then, read a m-sized string2 (char array). 156 | Count how many times string2 occurs in string1. 157 | 158 | Input: 159 | 7 2 160 | aabbaab 161 | aa 162 | Output: 163 | 2 164 | 165 | Input: 166 | 9 1 167 | aabbaaabb 168 | b 169 | Output: 170 | 4 171 | 172 | Input: 173 | 11 3 174 | aababbaabba 175 | bbb 176 | Output: 177 | 0 178 | -------------------------------------------------------------------------------- /W13/README.md: -------------------------------------------------------------------------------- 1 | ## W13 // 18.12.2019 2 | 3 | New type or combination of primitive types, struct. 4 | 5 | #### How to declare? 6 | ```c 7 | struct student { 8 | int id; 9 | char pass; 10 | float grade; 11 | }; 12 | ``` 13 | #### How to read from the console, fill the array and print it? 14 | ```c 15 | int main(void) { 16 | int i, n; 17 | 18 | scanf("%i", &n); 19 | 20 | struct student students[n]; 21 | 22 | for (i = 0; i < n; i ++) { 23 | scanf("%i %c %f", &students[i].id, &students[i].pass, &students[i].grade); 24 | } 25 | 26 | for (i = 0; i < n; i ++) { 27 | printf("%i %c %.2f\n", students[i].id, students[i].pass, students[i].grade); 28 | } 29 | 30 | return 0; 31 | } 32 | ``` 33 | #### How to give a name to our struct? 34 | 35 | You do not have to use a type definition. You may use it if you like. Type definition allows you to give a name to your struct. 36 | ```c 37 | #include 38 | #include 39 | 40 | typedef struct student { 41 | int id; 42 | char pass; 43 | float grade; 44 | } stu; 45 | 46 | int main(void) { 47 | int i, n; 48 | 49 | scanf("%i", &n); 50 | 51 | stu students[n]; 52 | 53 | for (i = 0; i < n; i ++) { 54 | scanf("%i %c %f", &students[i].id, &students[i].pass, &students[i].grade); 55 | } 56 | 57 | for (i = 0; i < n; i ++) { 58 | printf("%i %c %.2f\n", students[i].id, students[i].pass, students[i].grade); 59 | } 60 | 61 | return 0; 62 | } 63 | ``` 64 | #### How to send the struct to a function? 65 | ```c 66 | #include 67 | #include 68 | 69 | struct student { 70 | int id; 71 | char pass; 72 | float grade; 73 | }; 74 | 75 | void printVariable(struct student stu) { 76 | printf("%i %c %.2f\n", stu.id, stu.pass, stu.grade); 77 | } 78 | 79 | void printArray(int n, struct student students[n]) { 80 | int i; 81 | for (i = 0; i < n; i ++) { 82 | printf("%i %c %.2f\n", students[i].id, students[i].pass, students[i].grade); 83 | } 84 | } 85 | 86 | int main(void) { 87 | int i, n; 88 | 89 | scanf("%i", &n); 90 | 91 | struct student students[n]; 92 | 93 | for (i = 0; i < n; i ++) { 94 | scanf("%i %c %f", &students[i].id, &students[i].pass, &students[i].grade); 95 | } 96 | 97 | printArray(n, students); 98 | 99 | struct student myStudent; 100 | 101 | myStudent.id = 67; 102 | myStudent.pass = 'y'; 103 | myStudent.grade = 13.45; 104 | 105 | printVariable(myStudent); 106 | 107 | return 0; 108 | } 109 | ``` 110 | Try anything to see. 111 | 112 | Input: 113 | 3 114 | 14 c 16.97 115 | 74 f 95.13 116 | 64 t 32.84 117 | 14 c 16.97 118 | Output: 119 | 74 f 95.13 120 | 64 t 32.84 121 | 67 y 13.45 122 | 123 | ### Q1 124 | 125 | First, prepare the struct. 126 | struct player { 127 | char id[4]; 128 | int size; 129 | int year; 130 | }; 131 | Then read, n, a1, a2, b1, b2 integers. 132 | Then read n times id, size and year to fill struct array. 133 | Send them to a function. 134 | Count and return how many players are inside the bounds. 135 | Conditions are a1 >= size >= a2 and b1 >= year >= b2. 136 | Print ids of the players who are inside the bound inside the function. 137 | Print the count that function returns. 138 | 139 | Input: 140 | 6 19 16 180 165 141 | vbn 17 161 142 | dfg 18 177 143 | zxc 20 169 144 | bnm 19 181 145 | jkl 15 166 146 | wer 17 174 147 | Output: 148 | dfg 149 | wer 150 | 2 151 | 152 | ### Q2 153 | First, prepare the struct. 154 | struct item_pair { 155 | int price1; 156 | int price2; 157 | int budget; 158 | }; 159 | Then read n and fill the items array. 160 | Send n and array to a function. 161 | Count and return how many items we can buy from those item pairs. 162 | Rules: If the budget can buy both of them, buy both and print "Both items". 163 | If the budget can buy only one, buy it print "Item 1" or "Item 2". 164 | If the budget can buy only either one of them, buy the first one and print "Item 1". 165 | If the budget cannot buy anything, print "No buy". 166 | Input: 167 | 6 168 | 12 21 33 169 | 15 12 11 170 | 16 19 17 171 | 18 11 13 172 | 10 10 10 173 | 10 10 20 174 | Output: 175 | Both items 176 | No buy 177 | Item 1 178 | Item 2 179 | Item 1 180 | Both items 181 | 7 182 | 183 | ### Q3 184 | First, prepare the structs. 185 | struct point { 186 | int x; 187 | int y; 188 | }; 189 | struct square { 190 | struct point p; 191 | int length; 192 | }; 193 | Then, read three integers as the x y and length. 194 | (x, y) coordinates are left up corner of our square. 195 | Write 3 functions as area, perimeter and right_down. 196 | area takes a square and returns area of the square. 197 | perimeter takes a square and returns perimeter length of the square. 198 | right_down rakes a square and returns a point that right down corner of the square. 199 | -------------------------------------------------------------------------------- /W11/README.md: -------------------------------------------------------------------------------- 1 | ## W11 // 04.12.2019 2 | 3 | Multidimensional Arrays 4 | 5 | We are going to consider only 2D arrays. If we will prepare a question includes an array that has more than 2 dimensions, I will let you know before you face it. 6 | 7 | #### 1 dimensional array 8 | 9 | int numbers[3] = {1, 2, 3} 10 | 11 | read: 12 | for(i = 0; i < n; i ++) { 13 | printf("%i ", &numbers[i]); 14 | } 15 | 16 | print: 17 | for(i = 0; i < n; i ++) { 18 | printf("%i ", numbers[i]); 19 | } 20 | console: 21 | 1 2 3 22 | 23 | #### 2 dimensional array 24 | 25 | int numbers[3][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 0}, {5, 4, 3, 2, 1} } 26 | 27 | read: 28 | for(i = 0; i < n; i ++) { 29 | for(j = 0; j < m; j ++) { 30 | printf("%i ", &numbers[i][j]); 31 | } 32 | } 33 | 34 | print: 35 | for(i = 0; i < n; i ++) { 36 | for(j = 0; j < m; j ++) { 37 | printf("%i ", numbers[i][j]); 38 | } 39 | printf("\n"); 40 | } 41 | console: 42 | 1 2 3 4 5 43 | 6 7 8 9 0 44 | 5 4 3 2 1 45 | 46 | numbers[0] = [1, 2, 3, 4, 5] 47 | numbers[1] = [6, 7, 8, 9, 0] 48 | numbers[2] = [5, 4, 3, 2, 1] 49 | 50 | numbers[0][0] = 1 51 | numbers[0][1] = 2 52 | numbers[0][2] = 3 53 | numbers[0][3] = 4 54 | numbers[0][4] = 5 55 | numbers[1][0] = 6 56 | numbers[1][1] = 7 57 | numbers[1][2] = 8 58 | numbers[1][3] = 9 59 | numbers[1][4] = 0 60 | numbers[2][0] = 5 61 | numbers[2][1] = 4 62 | numbers[2][2] = 3 63 | numbers[2][3] = 2 64 | numbers[2][4] = 1 65 | 66 | #### Function 67 | Send: 68 | func(n, m, numbers); 69 | Receive: 70 | func(int n, int m, int numbers[n][m]) { ... } 71 | 72 | ### Q0 73 | Read n as the number of rows (column size) and read m as the number of columns (row size). 74 | Print the numbers from 0 to n * m as 2D array format. 75 | Do not forget that i and j starts from 0. So, you will print the numbers in range of [0, n * m - 1]. 76 | 77 | Input: 78 | 3 5 79 | 80 | Output: 81 | 0 1 2 3 4 82 | 5 6 7 8 9 83 | 10 11 12 13 14 84 | 85 | Input: 86 | 2 6 87 | 88 | Output: 89 | 0 1 2 3 4 5 90 | 6 7 8 9 10 11 91 | 92 | ### Q1 93 | Read n as the number of rows and read m as the number of columns. 94 | Then, read the 2D-array. 95 | Print sum of each row to the first line. 96 | Print sum of each column to the second line. 97 | 98 | Input: 99 | 3 7 100 | 1 2 3 4 5 6 7 101 | 8 9 0 1 2 3 4 102 | 5 6 7 8 9 0 1 103 | 104 | Output: 105 | 28 27 36 106 | 14 17 10 13 16 9 12 107 | 108 | Input: 109 | 10 10 110 | 0 1 2 3 4 5 6 7 8 9 111 | 10 11 12 13 14 15 16 17 18 19 112 | 20 21 22 23 24 25 26 27 28 29 113 | 30 31 32 33 34 35 36 37 38 39 114 | 40 41 42 43 44 45 46 47 48 49 115 | 50 51 52 53 54 55 56 57 58 59 116 | 60 61 62 63 64 65 66 67 68 69 117 | 70 71 72 73 74 75 76 77 78 79 118 | 80 81 82 83 84 85 86 87 88 89 119 | 90 91 92 93 94 95 96 97 98 99 120 | 121 | Output: 122 | 45 145 245 345 445 545 645 745 845 945 123 | 450 460 470 480 490 500 510 520 530 540 124 | 125 | ### Q2 126 | Read n as the number of rows, read m as the number of columns and read k. 127 | Then, read the 2D-array as numbers array. 128 | Write a function named "func" and send n, m, numbers array and k. 129 | Return and print the element that is the kth element in the array. 130 | First element is the 0th element. 131 | 132 | Input: 133 | 4 3 7 134 | 5 3 4 135 | 6 1 9 136 | 2 8 7 137 | 0 -1 10 138 | 139 | Output: 140 | 8 141 | 142 | Input: 143 | 2 8 11 144 | 5 2 3 4 6 7 9 1 145 | 8 0 11 77 66 44 55 22 146 | 147 | Output: 148 | 77 149 | 150 | Input: 151 | 10 10 43 152 | 0 1 2 3 4 5 6 7 8 9 153 | 10 11 12 13 14 15 16 17 18 19 154 | 20 21 22 23 24 25 26 27 28 29 155 | 30 31 32 33 34 35 36 37 38 39 156 | 40 41 42 43 44 45 46 47 48 49 157 | 50 51 52 53 54 55 56 57 58 59 158 | 60 61 62 63 64 65 66 67 68 69 159 | 70 71 72 73 74 75 76 77 78 79 160 | 80 81 82 83 84 85 86 87 88 89 161 | 90 91 92 93 94 95 96 97 98 99 162 | 163 | Output: 164 | 43 165 | 166 | ### Q3 167 | Read n as the number of rows, read m as the number of columns and read k. 168 | Then, read the 2D-array as numbers array. 169 | Write a function named "func" and send n, m, numbers array and k. 170 | Print the array as the following. 171 | In the first row, print first k elements and sum of them. 172 | In the second row, print first 2k elements and sum of them. 173 | In the third row, print first 3k elements and sum of them. 174 | ... 175 | Apply this for all rows. 176 | If xk becomes higher then the row size, print and sum all elements in the row. 177 | 178 | Input: 179 | 3 11 3 180 | 8 5 2 5 6 7 2 0 1 4 7 181 | 9 6 3 8 1 4 0 3 2 7 5 182 | 7 6 4 3 5 2 4 8 0 9 4 183 | 184 | Output: 185 | 8 5 2 15 186 | 9 6 3 8 1 4 31 187 | 7 6 4 3 5 2 4 8 0 39 188 | 189 | Input: 190 | 10 10 2 191 | 0 1 2 3 4 5 6 7 8 9 192 | 10 11 12 13 14 15 16 17 18 19 193 | 20 21 22 23 24 25 26 27 28 29 194 | 30 31 32 33 34 35 36 37 38 39 195 | 40 41 42 43 44 45 46 47 48 49 196 | 50 51 52 53 54 55 56 57 58 59 197 | 60 61 62 63 64 65 66 67 68 69 198 | 70 71 72 73 74 75 76 77 78 79 199 | 80 81 82 83 84 85 86 87 88 89 200 | 90 91 92 93 94 95 96 97 98 99 201 | 202 | Output: 203 | 0 1 1 204 | 10 11 12 13 46 205 | 20 21 22 23 24 25 135 206 | 30 31 32 33 34 35 36 37 268 207 | 40 41 42 43 44 45 46 47 48 49 445 208 | 50 51 52 53 54 55 56 57 58 59 545 209 | 60 61 62 63 64 65 66 67 68 69 645 210 | 70 71 72 73 74 75 76 77 78 79 745 211 | 80 81 82 83 84 85 86 87 88 89 845 212 | 90 91 92 93 94 95 96 97 98 99 945 213 | 214 | ### Q4 215 | Read n as the number of rows, read m as the number of columns, read a and b. 216 | Then, read the 2D-array as numbers. 217 | Write a function named "func" and send n, m, numbers array, a and b. 218 | Assume a differs from b and both is smaller than n. 219 | Change the ath row with the bth row. 220 | Print the array in the main function. 221 | 222 | Input: 223 | 6 3 1 4 224 | 5 4 6 225 | 2 0 3 226 | 9 1 2 227 | 7 6 1 228 | 0 5 3 229 | 8 7 8 230 | 231 | Output: 232 | 5 4 6 233 | 0 5 3 234 | 9 1 2 235 | 7 6 1 236 | 2 0 3 237 | 8 7 8 238 | 239 | Input: 240 | 10 10 4 7 241 | 0 1 2 3 4 5 6 7 8 9 242 | 10 11 12 13 14 15 16 17 18 19 243 | 20 21 22 23 24 25 26 27 28 29 244 | 30 31 32 33 34 35 36 37 38 39 245 | 40 41 42 43 44 45 46 47 48 49 246 | 50 51 52 53 54 55 56 57 58 59 247 | 60 61 62 63 64 65 66 67 68 69 248 | 70 71 72 73 74 75 76 77 78 79 249 | 80 81 82 83 84 85 86 87 88 89 250 | 90 91 92 93 94 95 96 97 98 99 251 | 252 | Output: 253 | 0 1 2 3 4 5 6 7 8 9 254 | 10 11 12 13 14 15 16 17 18 19 255 | 20 21 22 23 24 25 26 27 28 29 256 | 30 31 32 33 34 35 36 37 38 39 257 | 70 71 72 73 74 75 76 77 78 79 258 | 50 51 52 53 54 55 56 57 58 59 259 | 60 61 62 63 64 65 66 67 68 69 260 | 40 41 42 43 44 45 46 47 48 49 261 | 80 81 82 83 84 85 86 87 88 89 262 | 90 91 92 93 94 95 96 97 98 99 263 | --------------------------------------------------------------------------------