c structure & algorithm

16
Computer Programming BTI 10202

Upload: nurul-zakiah-zamri-tan

Post on 25-May-2015

197 views

Category:

Education


3 download

TRANSCRIPT

Page 1: C structure & algorithm

Computer Programming

BTI 10202

Page 2: C structure & algorithm

q A programming language

qothers like àUser can create instructionfor computers to follow

A calculatorGameProgram for ATM machine Source: internet

Page 3: C structure & algorithm

/* Example of c programming: print a massage on the screen*/

#include <stdio.h>

main ()

{printf(“My First C Programming");

}

My First C Programming OUTPUT

Simple C programming

Page 4: C structure & algorithm

/* Example of c programming: print a massage on the screen*/ comment

#include <stdio.h> pre processor command : To access the standard functions that comes with the compiler, its need to be to include a header with the #include directive

main () function name :every c program must begin with this function

{ begin of programming symbolprintf(“My First C Programming"); the body of programming

} end of programming symbol

; -- is part of the c syntax. Tells the program that it is at the end of the command

Page 5: C structure & algorithm

Variable

vA way of storing data in your computervTell the computer what type of variable that going to be used.

#include <stdio.h>#include<conio.h>

int main (void){

int num1;int num2;int sum;

printf( “Enter Your Number”);scanf(“%d, num1);scanf(“%d,num2);

Sum = num1 + num2

printf(“The sum is %d, sum)”

getch();}

Declaring variables

§ 1st letter must be an alphabet§Can be a combination of alphabet &

digits§Special character allows is only (_)§Can be write in both uppercase or

lowercase or in the combination§Case sensitive§No space between character§Not allow to use the reserved keywords

in C

Page 6: C structure & algorithm

Data types Format string

int %d

Unsigned int %u

Float % f

Long %ld

Unsigned long %lu

double %lf

Long double %lf

char %c /%s

Unsigned char %c/%s

Page 7: C structure & algorithm

The process of giving something to the computer is known as Input. The input is mostly given by keyboard

some important functions are for inputs are as follows:scanf()gets()getch()getche()

Format string for the scanf() function isscanf(“format string”, arguments)

ExampleUser input Output

Scanf(“%d,num1”); 12

Page 8: C structure & algorithm

The process of getting something from the computer is known as output. The output is mostly displayed on monitor

Some important functions for output are as follows:

printf();puts();

Example

printf(“My First C Programming”);

My First C Programming

Page 9: C structure & algorithm

vTo assign value to a variablevC provides a assignment operators

vThe syntax of the assignment expression:Variable = constant/variable/expression

v ex:vX=4vA=BvB= x+5

vThe expression could be :vAn arithmetic expressionvA relational expressionvA logical expressionvA mixed expression

Page 10: C structure & algorithm

Software development:

q Specification of requirementsqProblem analysisqDesign

qCoding and executionqVerification

qMaintenance

Understand the problem

Making assumption

•Understand the problem•Know the environment of the problem•Making assumption

•Problem input•Problem output•process

•Algorithm•Choosing the right structure to store data

•Apply algorithm to programming language (C)

•To test the program works properly

Page 11: C structure & algorithm

To solve a problem using computer, an algorithm is needed

AlgorithmLike a recipe

Without algorithm there is no a programming

Needs:•Input•Output•Not confusing•Simple•Understandable. Anyone can read•Precise to solve the problem

Few methods to present the algorithm:•Language statement•Pseudo code•Flowchart•N-S Figure

Page 12: C structure & algorithm

Banana Fritters / Cekodok Pisang

3 ripe bananas, peeled1/2 cup flour1/3 tsp baking powderPinch of saltOil for frying

1. In a bowl, mash the bananas well. 2.Add in the flour, baking powder and salt. 3.Mix well. You will get a slightly thick batter.4. In a wok or deep pan, put in the oil about 1.5"-2" high and preheat at medium heat.5. Scoop the batter using a tablespoon and slowly let it 'slide' into the hot oil.6. Fry until golden brown. 7. Flip once or twice during frying.8. Repeat step 3 until you have used all the batter.

Page 13: C structure & algorithm

Pseudo code

• line by line of action •One life is for one action•Written using understandable language (BI BM)•Have keyword

Example of pseudo code

Frying a Cekodok

1. Begin2. Mix banana, flour, sugar and pinch of salt3. Put oil in wok4. Scoop the mix ingredient to the hot oil5. Fry until it change to a golden color6. End

Page 14: C structure & algorithm

The other way to write a pseudo code:

A sequence structure

Startprocess 1process 2

…..process n

End

A decision structure

If condition thentrue

Elsefalse

End if

A repetition structure

Repeatpart that is going tobe repeated

Until condition

@

While condition dopart that is going tobe repeated

End while

Page 15: C structure & algorithm

Create pseudo code & flowchart for a solution to find the smallest of two numbers

Pseudo code

1. Prompt user for two integer values2. Save input to num 1 & num 23. Check to see if num 1 ==num2

If it is print “num 1 & num 2 are equalIf it is not check to see if num 1 is < num 2

if it is print “num 1 is the smallest”if it is not print “num 2 is the smallest”

Page 16: C structure & algorithm

START

1

2

3

5

7 6

4

END

F

F T

T

Flowchart