200 likes | 385 Views
Programming Training. Main Points: - Working with Functions - Problems with Numbers. - Discuss some important algorithms - Primality - Digits - Greatest Common Divisor. C Repetitions. C repetitions execute repeatealy a statement while a condition holds or for a number of times.
E N D
Programming Training Main Points: - Working with Functions - Problems with Numbers. - Discuss some important algorithms - Primality - Digits - Greatest Common Divisor
C Repetitions. C repetitions execute repeatealy a statement while a condition holds or for a number of times. While loop while(test){statement1;statement2; …} repeats statements zero or more times while test holds. Do while loop do{statement1;statement2; …}while(test); repeats statements once or more times while test holds. For loop for(exp1;exp2;exp3){statement1;statemet2;…} Foor loop is used when we know some patterns in repetition.
#include<stdio.h> #include<conio.h> int main (int args,char ** argc){ long n, d, pow=0; printf(”n=");scanf("%ld",&n); for(d=2;d<=n;d++) { if (n%d==0) { pow=0; while(n % d==0) { n = n / d; pow ++; } printf(“ %ld ^ %ld * ”, d, pow); } } return1; }
Functions in C A C program is a sequence of a functions which contains a single function main(). Function = Routine = Procedure = Method = Solution for a subproblem. A C function is written once and used/called as many times as needed. returning_type function_name(type 1 arg1, type2 arg2,…) { declarations of the function variables; statements to find the result res; return res; } 1. returning_type is the nature/type of the result res. 2. function_name is the name of the function 3. type 1 arg1, type2 arg2,…are the declarations of the arguments 4. the function body must find the output result and return it. 5. return_type function_name(type 1 arg1, type2 arg2,…); is the declaration of the function or the function prototype.
Steps to work with functions 1. Declare all the functions of the program after the header declarations. - After these declarations the program must know the functions, their arguments, and their returning values. 2. Construct the bodies of these functions after the body of main. - Start from the prototype. - Declare all the variables of the function. - Give the sequence of statements to construct the output res. - Return the output res. 3. Call a function at any time you need. - The call is function_name(a1,a2,…); and returns a value that can be used. - a1, a2,… are the actual/current arguments that must have the same types as the formal arguments.
Example 1. 1. Find the minimum and maximum of two integers a and b. int min2nrs(int a, int b){ int min; // declare a variable min if(a<b) min=a; // find the minimum else min=b; return min; // return min } a=min2nrs(x,y) a is the min value of x and y. x=min2nrs(x,0) x is the min value of x and 0. min=min2nrs(a,b) min is the min value of a and b.
#include<stdio.h> #include<conio.h> int min2nrs(int a, int b); int main (int args,char ** argc){ long a,b,c,d,min1,min2,min; //reading the variables printf("a=");scanf("%ld",&a); printf("b=");scanf("%ld",&b); printf("c=");scanf("%ld",&c); printf("d=");scanf("%ld",&d); min1=min2nrs(a,b); min2=min2nrs(c,d); min=min2nrs(min1,min2); printf(“min = %ld”, min); return 1; } int min2nrs(int a, int b){ int min; if(a<b) min=a; else min =b; return min; }
Find pow so that d^pow | n. Counting problems use a counting counter cont: - initial value is 0. - count increases when the event occurs. Any time when a divisor is found we remove it from the number. Example: n=72 and k=2. pow: 0 | 1 | 2 | 3 n : 72 | 36 | 18 | 9 d : 2 | 2 | 2 | The repetition takes place while n is divisible by d: n % d ==0
Find pow so that d^pow | n. Inputs: n, d – long The inputs are the function arguments Output: pow – long The output must be calculated and returned. long power(long n, long d) { long pow=0; while(n % d==0) { n = n / d; pow ++; } returnpow; }
#include<stdio.h> // declaration of the function power long power(long n, long d); // body of main int main (int args,char ** argc){ long n, d, pow=0; printf(”n=");scanf("%ld",&n); for(d=2;d<=n;d++) { if(n%d==0) { pow = power(n, d); printf("\n The power is %ld”, pow); } } return1; } //body of power long power(long n, long d) { long pow=0; while(n % d==0) { n = n / d; pow ++; } returnpow; }
Prime numbers If a number n is integer then find whether is prime or not. n is prime when n has only 2 divisors 1 and n. Example: - n =2, 3, 5, 7 are all prime - even numbers are not prime. - 12345678910987654321 is prime -1234567891010987654321 is prime too
Prime numbers (1) Some Remarks: n==2 n is prime. Even numbers: n%2==0 n is not prime Odd numbers have only odd divisors d=3,5,7,… Repeat for d=3,5,7,…sqrt(n) - if d is divisor then n is not prime.
int isPrime(long n) { long d; if (n==2) { return 1; } if (n%2==0) { return 0; } for(d=3;d<=sqrt(n);d+=2) { if (n%d==0) { return 0; } } return1; }
Problems with Prime numbers • Write a program to list all primes up to a threshold. • Write a program to list all palyndrom primes. • Write a program to list all primes whose mirror is prime too. • Given an even number n then write a program to decompose n in a sum of 2 primes e.g. n = p1+p2 with p1 and p2 primes [Goldbach’s conjecture] • Etc.
Greatest Common Divisor If a, b are integer then the greatest common divisor Example: - a=24, b=36 gcd=12 - a=25, b=33 gcd=1 How to solve it? - the Euclidian algorithm
The Euclidean Algorithm Sequence of divisions until the remainder is 0. Repeat the following: - divide the numbers - if remainder is 0 then break - change the numbers: first=second; second=remainder
long euclid (long a, long b) { long first, last, remainder, gcd; do { // divide first to second result=first / second; remainder=first % second; // change values for first and second first=second; second=remainder; } while(remainder!=0); gcd=second; returngcd; }
Find the reverse / mirror of a long. If n is a long number then last=n % 10 is the last digit. n = n/10 is the new number from which we remove last. n = n*10 +digit is the new number to which digit is added. If we repeat removing the last digit then we can find: - all the digits of the number. - number of digits. - the mirror of a number We repeat while the number is not fully done.
long reverse(long n){ long last, new_n = 0; while(n!=0) { // find the last digit last = n % 10; n = n / 10; // add the last digit to new_n new_n = new_n*10 + digit; } returnnew_n; }
To do List • Read more about working with functions. • Solve the HW problems.