Algorithmization and Programming Examples from the 4-rd lecture ################################### ### ### ### C ### ### ### ################################### some syntax differences from Matlab: - every variable must be DECLARED in advance - every variable has some TYPE assigned, different types of variables are treated differently - every command has to be finished by a semicolon - a sign for 'not' is '!' instead of '~' - for print on screen use 'printf' instead of 'fprintf' - for format strings use double-quotes " instead of single quotes ' ============================================= ====== elementary input and output ========== ============================================= --- the first program in C: writes 'Hello.' on the screen -- #include main() { printf("Hello.\n"); } --- the same program, now formally correct --- (convenient for a compiler option -Wall) #include int main() { printf("Hello.\n"); return 0; } ### Example 1.1 --- reads an integer and a real number from keyboard --- --- writes these numbers on the screen --- #include int main() /* this is a commentary */ // this is also a commentary { /* declarations: variable n for integer values variable p for floating point values */ int n; float p; // read an integer number printf("Give some integer number: "); scanf("%d",&n); // read a floating point number printf("Give some decimal number: "); scanf("%f",&p); // writing both numbers on screen printf("\nYour inputs:\n\n the first number %d \n the second number %f\n\n", n, p); // formatting the float number printf("the second number in different formats:\n\n"); printf("%5.0f %10.2f %10.3e\n\n", p, p, p); return 0; } ============================================= ========= using a math library ========== ============================================= ### Example 1.2 --- computes sinus of a given x --- --- and the length and the area of a circle of a given r --- #include #include int main() /* for math library, use the -lm switch: gcc -Wall test.c -o test -lm */ { float x, y, r; // computing sinus x printf("give a number: "); scanf("%f",&x); y = sin(x); printf("\nsin(%.3f) = %f\n\n", x, y); // computing the length and the area of a circle printf("give a radius of a circle: "); scanf("%f",&r); printf("\nthe length: %f \nthe area: %f\n\n", 2*M_PI*r, M_PI*pow(r,2) ); return 0; } ######################################### ### Examples from previous lectures ### ### rewritten from Matlab to C ### ######################################### Algorithm 1.1: -------------- Input: Deposit, Rate and N_Years (numbers) Output: Gross_interest 1. Get Deposit, Rate, N_Years 2. Set q = 1 + Rate/100 3. Set Gross_interest = Deposit * q^ N_Years - Deposit #include #include int main() // Computing of the gross interest { float Deposit, Rate, q, Gross_interest; int N_Years; // input data printf("Give the deposit: "); scanf("%f",&Deposit); printf("Give the interest rate (percent): "); scanf("%f",&Rate); printf("Give the number of years: "); scanf("%d",&N_Years); // computations q = 1 + Rate/100; Gross_interest = Deposit * pow(q,N_Years) - Deposit; // print result printf("\nThe gross interest is %.2f\n", Gross_interest); return 0; } ============================================= =========== conditional control ============= ============================================= example: read two numbers and find if they are equal -------- #include int main() { float a,b; printf("Give two numbers:\n"); scanf("%f",&a); scanf("%f",&b); if (a == b) { printf("equal\n"); } else { printf("not equal\n"); } return 0; } ============================================= ========== the 'for' loop =================== ============================================= Algorithm 1.2: -------------- Input: Deposit, Rate and N_Years Output: Table of years' deposits 1. Get Deposit, Rate, N_Years 2. Set q = 1 + Rate/100 3. Repeat N_Years times: 3.1 Set Deposit = Deposit * q 3.2 Print Deposit #include int main() // Table of years' deposits: { float Deposit, Rate, q; int N_Years, k; // input data printf("Give the deposit: "); scanf("%f",&Deposit); printf("Give the interest rate (percent): "); scanf("%f",&Rate); printf("Give the number of years: "); scanf("%d",&N_Years); q = 1 + Rate/100; printf("\n Year Deposit \n"); printf(" ---- ------- \n"); for (k=1; k <= N_Years; k++) { Deposit = Deposit * q; printf("%4d %10.2f\n",k, Deposit); } return 0; } ============================================= =========== the 'while' loop ================ ============================================= example: print the first negative number in a sequence #include int main() { float a; printf("Give the first number:\n"); scanf("%f",&a); while (a >= 0) { printf("Give the next number:\n"); scanf("%f",&a); } printf("The first negative number is %f.\n", a); return 0; } ------------- Ex1_1c.c ------------- #include #include int main() /* script for simple iterations, given the precision, with prevention of infinite cycle f(x) = 0.2 * sin(x+0.5) */ { float x_old, x_new, p; int c_max, c; // input printf("give the starting point: \n"); scanf("%f",&x_old); printf("give the precision: \n"); scanf("%f",&p); printf("give the maximal number of iterations: \n"); scanf("%d",&c_max); x_new = 0.2 * sin(x_old + 0.5); c = 1; // count of iterations while ((fabs(x_old - x_new) > p) && (c <= c_max)) { x_old = x_new; x_new = 0.2 * sin(x_old + 0.5); c = c + 1; // or c++; } if (c > c_max) { printf("number of iterations exceeded, "); printf("the previous iteration is %f \n", x_old); } printf("the result is %f \n", x_new); return 0; } ============================================= =============================================