join to c language session 2 by reza gholamzadeh - 1387 1st semester
Post on 17-Dec-2015
221 Views
Preview:
TRANSCRIPT
We have an overview to: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
Anatomy of a C Anatomy of a C programprogram
By Reza Gholamzadeh - 1387 1st Semester
Preprocess instructions
Typical C program
Prototypes
Global variables
main() function
function A()
function B()
statements
statements
statements
Declarations
Assignment
Function
Control
null
What is a function?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 variablesDeclaration 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:1. The characters at your disposal are lowercase letters, uppercase letters,
digits, and the underscore (_)
2. The first character must be a letter or an underscore
For example:
firstName , _sumOf , taxi1 , ...
By Reza Gholamzadeh - 1387 1st Semester
AssignmentAssignmentBy value
Example:
X = 12;
sum = 1298.998;
ch = ‘q’;
By expressionsExample:
avg = ( a + b + c ) / 3;
x = sin(y);
var1 = var2;
By Reza Gholamzadeh - 1387 1st Semester
BlocksBlocksA block starts with ‘{‘ and ends with
‘}’Variables defined in a block , can`t
be used outside of the blockEach function have a blockBlocks used for grouping statementsBlocks can be used in blocksA block:{
statement1;statement2;…
}
By Reza Gholamzadeh - 1387 1st Semester
CommentsCommentsThe 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 functionsInput and output functionsOutput 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()Examples:
scanf(“%d”,&input);
scanf(“%f%f%f”,&number1,&number2,&number3);
scanf(“(%d,%d)”,&pointX,&pointY);
By Reza Gholamzadeh - 1387 1st Semester
Conditional statementsConditional statementsif statement structures
By Reza Gholamzadeh - 1387 1st Semester
if (condition) statement;
if (condition){ statement1; ...}
if (condition) statement1;else statement2;
if (condition){ statement1; ...}else { statement1; ...}
if (condition1){ statement1; ...}else if(condition2){ statement1; ...}else if(condition3){ statement1; ...}...else{ statement1; ...}
Conditional statementsConditional statementsswitch statement structure
By Reza Gholamzadeh - 1387 1st Semester
switch (variable){ case value1:
statement1;...break;
case value2:statement1;...break;
case value3:statement1;...break;
.
.
.
[default:]statement1;...
}
LoopsLoopswhile loop structures
do – while loop structures
By Reza Gholamzadeh - 1387 1st Semester
while (condition) statement;
while (condition){ statement1; statement2; ...}
do statement;while (condition);
do{ statement1; statement2; ...}while (condition);
LoopsLoopsfor loop structures
By Reza Gholamzadeh - 1387 1st Semester
for (initializeCounter; condition; step){ statement1; statement2; ...}
for (initializeCounter; condition; step) statement;
Now Now ready to write some ready to write some
programsprograms
By Reza Gholamzadeh - 1387 1st Semester
Example 1Example 1
A program which gets n number and calculates the average.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter value of n: 5Enter number 1: 6Enter number 2: 7Enter number 3: -1Enter number 4: 16Enter number 5: -3
Average is: 5.00
Example 1Example 1
By Reza Gholamzadeh - 1387 1st Semester
#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;
}
Example 2Example 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.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter n and r in format (n,r): (5,3)
Result of c(5,3) is: 10
Example 2Example 2
By Reza Gholamzadeh - 1387 1st Semester
#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;
}
Example 2Example 2
By Reza Gholamzadeh - 1387 1st Semester
int fact(int input)
{
int output = 1 , i;
if(input == 0)
return 1;
for(i = input; i>=1; i--)
output *= i;
return output;
}
Example 3Example 3
A program which gets float point numbers and calculates the minimum & maximum of them.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter value of n: 3Enter number 1: -1Enter number 2: 2.5Enter number 3: 2
Max is 2.50 and min is -1.00
Example 3Example 3
By Reza Gholamzadeh - 1387 1st Semester
#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++) {
printf(“Enter number 1: “);scanf(“%f”,&input);
if(input < Min)Min = input;
else if(input > Max)Max = input;
}printf(“\nMax is &f and Min is %f”,Max,Min);
return 0;
}
Example 4Example 4
A program which gets three numbers and shows us that numbers can be the lengths of a triangle or not.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter a,b,c in {a,b,c} format: {2,4,3}
Yes.
Example 4Example 4
By Reza Gholamzadeh - 1387 1st Semester
#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;
}
Example 5Example 5
A program which gets the coordinatios of vector A & B, and calculate the A*B.
By Reza Gholamzadeh - 1387 1st Semester
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)
Example 5Example 5
By Reza Gholamzadeh - 1387 1st Semester
#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;
}
Exercise 1Exercise 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.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter numbers: -1 , 12.05 , 11 , 0
Minimum is -4.00 and maximum is 12.05)
Exercise 2Exercise 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.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter your character: CEnter number of repeating: 21.Enter character: D2.Enter character: C3.Enter character: A4.Enter character: 55.Enter character: C
Program finished after 5 inputs.
Exercise 3Exercise 3A 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.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter A and B: 25 4
Result is: 5,7,11,13,17,19,23I find 7 prime number between 4 and 25.
Exercise 4Exercise 4A program which reads a number
in binary base and converts it into decimal base.
By Reza Gholamzadeh - 1387 1st Semester
Output:
Enter your binary number: 10011
Your number in decimal: 19
NotesNotes1. Codes must be executable2. Use less variables as you can (the
less memory)3. Codes should be readable (similar
to example’s codes)4. Codes should give the right answer
(by having bugs, it gets point by the amount of errors)
5. Must be mailed in the time given( Till 1:00 PM of next Saturday)
By Reza Gholamzadeh - 1387 1st Semester
top related