Join to C language Session 2 By Reza Gholamzadeh - 1387 1st Semester
We have an overview to: Anatomy of a C program What is a function? Declaration of variables Data types Choose correct names Initialize Assignment Blocks Comments Input and output functions Conditional statements if switch Loops while do – while for By Reza Gholamzadeh - 1387 1st Semester
Preprocess instructions Anatomy of a C program Preprocess instructions Typical C program Prototypes Global variables main() function function A() function B() statements Declarations Assignment Function Control null By Reza Gholamzadeh - 1387 1st Semester
What is a function? A function is a self-contained unit of program code designed to accomplish a particular task a function can both produce actions (such as procedures) and provide values they save you from repetitious programming it makes a program more modular, hence easier to read and easier to change or fix By Reza Gholamzadeh - 1387 1st Semester
Declaration of variables Declaration structure: DataType Var1Name = [initialization],[Vare2Name] = [initialization],…; For example: int sum = 2; float avg = 0, var = 1.0; char mark = ‘A’; Available data types: int , char , float , double , long , short , ... Available names for variables: The characters at your disposal are lowercase letters, uppercase letters, digits, and the underscore (_) The first character must be a letter or an underscore firstName , _sumOf , taxi1 , ... By Reza Gholamzadeh - 1387 1st Semester
Assignment By value By expressions Example: X = 12; sum = 1298.998; ch = ‘q’; By expressions avg = ( a + b + c ) / 3; x = sin(y); var1 = var2; By Reza Gholamzadeh - 1387 1st Semester
Blocks A block starts with ‘{‘ and ends with ‘}’ Variables defined in a block , can`t be used outside of the block Each function have a block Blocks used for grouping statements Blocks can be used in blocks A block: { statement1; statement2; … } By Reza Gholamzadeh - 1387 1st Semester
Comments The parts of the program enclosed in the /* */ symbols are comments Using comments makes it easier for someone (including yourself) to understand your program they can be placed anywhere Everything between the opening /* and the closing */ is ignored by the compiler use the symbols // to create comments that are confined to a single line By Reza Gholamzadeh - 1387 1st Semester
Input and output functions Output function printf() Examples: printf(“Hello C language!”); printf(“My name is:\nReza Gholamzadeh”); printf(“your code is %d”,code); printf(“\t a + b = %f”,a+b); Input function scanf() scanf(“%d”,&input); scanf(“%f%f%f”,&number1,&number2,&number3); scanf(“(%d,%d)”,&pointX,&pointY); By Reza Gholamzadeh - 1387 1st Semester
Conditional statements if statement structures if (condition) statement; if (condition1) { statement1; ... } else if(condition2) else if(condition3) . else if (condition) { statement1; ... } if (condition) statement1; else statement2; if (condition) { statement1; ... } else By Reza Gholamzadeh - 1387 1st Semester
Conditional statements switch statement structure switch (variable) { case value1: statement1; ... break; case value2: case value3: . [default:] } By Reza Gholamzadeh - 1387 1st Semester
Loops while loop structures do – while loop structures while (condition) statement; while (condition) { statement1; statement2; ... } do statement; while (condition); do { statement1; statement2; ... } while (condition); By Reza Gholamzadeh - 1387 1st Semester
Loops for loop structures for (initializeCounter; condition; step) statement; for (initializeCounter; condition; step) { statement1; statement2; ... } By Reza Gholamzadeh - 1387 1st Semester
Now ready to write some programs By Reza Gholamzadeh - 1387 1st Semester
Example 1 A program which gets n number and calculates the average. Output: Enter value of n: 5 Enter number 1: 6 Enter number 2: 7 Enter number 3: -1 Enter number 4: 16 Enter number 5: -3 Average is: 5.00 By Reza Gholamzadeh - 1387 1st Semester
Example 1 #include <stdio.h> int main() { int n,input,sum = 0,i; float avg = 0.0; for(i=0; i<n; i++) printf(“enter number %d: “,i+1); scanf(“%d”,&input); sum+=input; } avg = sum/n; printf(“average is %.2f”,avg); return 0; By Reza Gholamzadeh - 1387 1st Semester
Example 2 A program which gets natural numbers of n and r and calculates the c(n,r).This should include a function to calculate the factorial. Output: Enter n and r in format (n,r): (5,3) Result of c(5,3) is: 10 By Reza Gholamzadeh - 1387 1st Semester
Example 2 #include <stdio.h> int fact(int); int main() { int r,n; float c; printf(“enter n and c in (c,r) format: “); scanf(“(%d,%d)”,&n,&r); if(n<r) printf(“incorrect values.”); return 1; } c = fact(n)/(fact(r)*fact(n-r)); printf(“Result of c(%d,%d) is: %f“,n,r,c); return 0; By Reza Gholamzadeh - 1387 1st Semester
Example 2 int fact(int input) { int output = 1 , i; if(input == 0) return 1; for(i = input; i>=1; i--) output *= i; return output; } By Reza Gholamzadeh - 1387 1st Semester
Example 3 A program which gets float point numbers and calculates the minimum & maximum of them. Output: Enter value of n: 3 Enter number 1: -1 Enter number 2: 2.5 Enter number 3: 2 Max is 2.50 and min is -1.00 By Reza Gholamzadeh - 1387 1st Semester
Example 3 #include <stdio.h> int main() { int n,i; float input,Min,Max; printf(“Enter value of n: “); scanf(“%d”,&n); if(n == 0) return 1; printf(“Enter number 1: “); scanf(“%f”,&input); Max = input; Min = input; for(i = 2; i<=n; i++) if(input < Min) else if(input > Max) } printf(“\nMax is &f and Min is %f”,Max,Min); return 0; By Reza Gholamzadeh - 1387 1st Semester
Example 4 A program which gets three numbers and shows us that numbers can be the lengths of a triangle or not. Output: Enter a,b,c in {a,b,c} format: {2,4,3} Yes. By Reza Gholamzadeh - 1387 1st Semester
Example 4 #include <stdio.h> int main() { int a,b,c; printf(“enter a,b,c in {a,b,c} format: “); scanf(“{%d,%d,%d}”,&a,&b,&c); if(a+b>c && a+c>b && b+c>a) printf(“\nYes.”); else printf(“\nNo.”); return 0; } By Reza Gholamzadeh - 1387 1st Semester
Example 5 A program which gets the coordinatios of vector A & B, and calculate the A*B. Output: Enter A in (a,b,c) format: (2,2,3) Enter A in (d,e,f) format: (1,4,-2) A*b is: (-16,7,6) By Reza Gholamzadeh - 1387 1st Semester
Example 5 #include <stdio.h> int main() { int a,b,c,d,e,f; printf(“enter A in (a,b,c) format: “); scanf(“(%d,%d,%d)”,&a,&b,&c); printf(“enter B in (d,e,f) format: “); scanf(“(%d,%d,%d)”,&d,&e,&f); printf(“\nA*B is: (%d,%d,%d)”,(b*f-c*e),(c*d-a*f),(a*e-b*d)); return 0; } By Reza Gholamzadeh - 1387 1st Semester
Exercise 1 A program which gets 4 numbers and shows the maximum and minimum of them . This program should include two functions for calculation of maximum and minimum of two numbers. Output: Enter numbers: -1 , 12.05 , 11 , 0 Minimum is -4.00 and maximum is 12.05) By Reza Gholamzadeh - 1387 1st Semester
Exercise 2 A program which gets a special character and a number (n).It will get other inputs(in character mode) until the nth character of that special character is inputted. Output: Enter your character: C Enter number of repeating: 2 1.Enter character: D 2.Enter character: C 3.Enter character: A 4.Enter character: 5 5.Enter character: C Program finished after 5 inputs. By Reza Gholamzadeh - 1387 1st Semester
Exercise 3 A program which gets 2 numbers and will show all the prime numbers between these two number .The program should include a function to calculate the prime number. Output: Enter A and B: 25 4 Result is: 5,7,11,13,17,19,23 I find 7 prime number between 4 and 25. By Reza Gholamzadeh - 1387 1st Semester
Exercise 4 A program which reads a number in binary base and converts it into decimal base. Output: Enter your binary number: 10011 Your number in decimal: 19 By Reza Gholamzadeh - 1387 1st Semester
Notes Codes must be executable Use less variables as you can (the less memory) Codes should be readable (similar to example’s codes) Codes should give the right answer (by having bugs, it gets point by the amount of errors) Must be mailed in the time given( Till 1:00 PM of next Saturday) By Reza Gholamzadeh - 1387 1st Semester