C programming 5613 Lecture 5 Marina Krstic Marinkovic mmarina@maths.tcd.ie School of Mathematics Trinity College Dublin Marina Krstic Marinkovic 1 / 10 5613 - C programming
Assignment 0 - Fibonacci numbers: recursion // Program to find k-th number in a Fibonacci sequence // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int fib(int k) if (k==0) return 1; /*O-th element of Fib. seq.*/ if (k==1) return 1; /*1-th element of Fib. seq.*/ return fib(k-1)+fib(k-2); int main() int i; for (i=0;i<50;i++) printf("%d %d\n",i,fib(i)); /*printing all Fibonacci numbers from F_0 to F_50 */ Marina Krstic Marinkovic 2 / 10 5613 - C programming
Assignment 0 - Fibonacci numbers: for loop // Program to find k-th number in a Fibonacci sequence using FOR loop // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int main() int i,f0,f1,fk,n; printf("enter how many elements of Fibonacci sequence you want to compute\n"); scanf("%d",&n); f0=1; /* 0-th element: F_0=1 */ f1=1; /* 1-th element: F_1=1 */ for (i=2;i<=n;i++) fk=f0+f1; /*computing Fib. term fk*/ f0=f1; /*f0 captures f_k-1*/ f1=fk; /*f0 captures fk*/ /*both will be used in the next step: f_k+1=f_k-1+f_k*/ printf("f_%d = %d\n",i,fk); return 0; Marina Krstic Marinkovic 3 / 10 5613 - C programming
Assignment 0 - Fibonacci array: for loop // Program to find k-th number in a Fibonacci sequence using FOR loop // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int main() int i,n,a[50]; printf("enter how many elements of Fibonacci sequence you want to compute (n<50) \n"); scanf("%d",&n); a[0]=1; /* 0-th element: F_0=1 */ a[1]=1; /* 1-th element: F_1=1 */ if (i<2) printf("f_%d = %d\n",i,a[i]); else for (i=2;i<=n;i++) /* completing array containing F_0,F_1... F_n */ a[i]=a[i-1]+a[i-2]; /*filling in array a[] with the element F_i*/ printf("f_%d = %d\n",i,a[i]); Which value would your code give for f46? Marina Krstic Marinkovic 4 / 10 5613 - C programming
Assignment 0 - Fibonacci array: while loop // Program to find k-th number in a Fibonacci sequence using WHILE loop // For a non-negative integer k, F_k=F_k-1+F_k-2, F_0=F_1=1 #include<stdio.h> int main() int i; long a[50]; a[0]=1; /* 0-th element: F_0=1 */ a[1]=1; /* 1-th element: F_1=1 */ i=2; while (i<50) /* we will only construct elements F_0... F_45 */ a[i]=a[i-1]+a[i-2]; /*filling in array a[] with the element F_i*/ printf("%d %ld\n",i,a[i]); i++; Which value would your code give for f46? Marina Krstic Marinkovic 5 / 10 5613 - C programming
Control Statements: shorthand for if else Branching if/ if else/ else if/?/ switch //? as ternary operator: (condition)? expression1 : expression2; //? as binary operator: (expression1)? : expression2; GNU C only! Ternary operator: returns value of expression1 if condition is true, and otherwise evaluates and returns expression2 Binary (Elvis) operator:returns value of expression1 if that value is true, and otherwise evaluates and returns its second operand Marina Krstic Marinkovic 6 / 10 5613 - C programming
Control Statements: shorthand for if else As ternary operator: // c gets the value of the larger of two numbers: a,b c = (a > b)? a : b //shortand for: if (a>b) c=a; else c=b; As binary operator: // sets x equal to the result of f() if that result is a true value // othewise its sets x to the result of g() x = f()?: g() //equivalent to x = f()? f() : g() Marina Krstic Marinkovic 7 / 10 5613 - C programming
Control Statements: switch case Branching if/ if else/ else if/?/ switch case switch (variable or an integer expression) case constant1: //codes ; case constant2: //codes ; case constant3: //codes ; default: //codes ; Marina Krstic Marinkovic 8 / 10 5613 - C programming
Control Statements: switch case // Performs addition, subtraction, multiplication or division depending the input from user # include <stdio.h> int main() char operator; double firstnumber,secondnumber; printf("enter an operator (+, -, *,): "); scanf("%c", &operator); printf("enter two operands: "); scanf("%lf %lf",&firstnumber, &secondnumber); switch(operator) case '+': printf("%.1lf + %.1lf = %.1lf\n,firstNumber, secondnumber, firstnumber + secondnumber); break; case '-': printf("%.1lf - %.1lf = %.1lf\n",firstNumber, secondnumber, firstnumber - secondnumber); break; case '*': printf("%.1lf * %.1lf = %.1lf\n,firstNumber, secondnumber, firstnumber * secondnumber); break; case '/': printf("%.1lf / %.1lf = %.1lf\n",firstNumber, secondnumber, firstnumber / firstnumber); break; // operator doesn't match any case constant (+, -, *, /) default: printf("error! operator is not correct\n ); return 0; Marina Krstic Marinkovic 9 / 10 5613 - C programming
Exercises 1. Write a line of C-code that evaluates the expression z = ( min(x, y) for y 0, max(x 2,y 2 ) for y<0 for given x and y, withouth using if operator. 2. Write a C program that writes the number of days in a given month, if a user enters as the input the number of the month (e.g. 1=January, 2=February, etc.). In case the entered value is 2, the program should ask the user whether a year is a leap year or not. 3. For a given integer K between 1 and 2697, determine which digit is on the K-th place of an array 101102103104 998999 that contains the digits of all numbers from 101 to 999 in an ascending order. Marina Krstic Marinkovic 10 / 10 5613 - C programming