Problem Solving Techniques

29
FLOW CHARTS PSEUDO CODE

description

 

Transcript of Problem Solving Techniques

Page 1: Problem Solving Techniques

FLOW CHARTS PSEUDO CODE

Page 2: Problem Solving Techniques

They are very precise. They represent our thoughts exactly.

Page 3: Problem Solving Techniques

FUNDAMENTAL CONDITIONAL AND CONTROL STRUCTURES: Branching: Two way branching: Eg: if A > B, then Print A, otherwise Print B

Multiway branching: Eg: if n is 0, then Print ‘ zero’ 1, then Print ‘ one’ 2, then Print ‘ two’ 3, then Print ‘ three’

Page 4: Problem Solving Techniques

ITERATION: The third fundamental technique is Iteration. It means repeating a set of actions again and again.

Page 5: Problem Solving Techniques

Pseudo code can be used to represent a procedure for doing something.

These are in between the English and the high level computer languages.

The flow chart fundamental control structures

for branching and iteration correspond to the following pseudo code. if…then…else if…then For…to…do… While…do…

Page 6: Problem Solving Techniques
Page 7: Problem Solving Techniques
Page 8: Problem Solving Techniques

Consider the statement: number = number + 1;The tokens are, number - identifer (variable) = - operator + - operator 1 - constant ; - punctuation

Page 9: Problem Solving Techniques

A constant is of numeric or non-numeric type. It can be a number, a character or a character string that can be used as a value in a program.Numeric constants are of three types:• integer constant• floating-point constant• character constantA non-numeric data can be called as a literal.String Literal: A string literal or a string constant is a sequence of characters from the system’s character set, enclosed in double quotes.

Page 10: Problem Solving Techniques

Integer Constant: An integer constant is a decimal

number (base 10) that represents an integral value (the whole number). It comprises of the

digits 0 to 9. Floating - point Constant A floating-point constant is a

signed real number. It includes integer portion, a decimal point, fractional portion and an exponent. Eg. 5.864E1

Character ConstantA character is a letter, numeral or special

symbol, which can be handled by the computer system. These available symbols define the system’s character set.

Eg. ‘1’, ‘a’, ‘+’, and ‘-‘ are the valid character constants.

Page 11: Problem Solving Techniques

Identifiers are the names that are to be given to the variables, functions, data types and labels in a program.

The name of a variable can consist of alphabets (letters) and numbers. Other characters are not allowed in the name of a variable except an

underscore character. The variable name starts with an alphabet and its length may vary from

one character to 32 characters. The first character in a variable’s name should be an alphabet, Keywords (which have special meaning in C) cannot be used as identifiers

The valid variable names are:xlengthx_valuey_valueA123

Page 12: Problem Solving Techniques

POINTER VARIABLES The variables in C are classified into ordinary variables and pointer variables.

int x;

x = 10;

int *y;

A pointer variable assumes only address as its value. Each

variable takes some locations in the main memory according to its type. Every location in the main memory is addressable.

y=&x;

• y represents the address of the variable x (&x)

• *y represents the value of the variable x (x)

Page 13: Problem Solving Techniques
Page 14: Problem Solving Techniques

Variable = Expression;

i++ postfix form ++i prefix formc = a+b; arithmetic expression

c = a > b; relational expression

f = d = e; assignment expressionx = i++; /*postfix increment

expression on the right side*/

printf()scanf()

Page 15: Problem Solving Techniques

[ ] - represent array index { } - cover the body of the function ( ) - represent a function, to group items < > - enclose a header file in a preprocessor statement “ “ - represent string literals ‘ ‘ - represent a character constant /* */ - represent a comment ; - a statement terminator , - to separate items

KEYWORDSThey cannot be used as identifiers for the variables in a program.

auto break case char continue default do else if float for int return staticswitch whileA keyword must be specified precisely as given in the list.

Page 16: Problem Solving Techniques
Page 17: Problem Solving Techniques

Each and every line of a C program can be considered as a statement. There are generally four types of statements. They are:

• Preprocessor statement • Function header statement • Declaration statement • Executable statement

#include <stdio.h> => Preprocessor Statementmain() => Function header Statement{ int a,b,c; => Variable declaration statement int add(int,int); => Function declaration statement a = 10; => Executable statement}

#include <stdio.h> => Preprocessor Statementmain() => Function header Statement{ int a,b,c; => Variable declaration statement int add(int,int); => Function declaration statement a = 10; => Executable statement}

Page 18: Problem Solving Techniques

PRINTF() STATEMENT:PRINTF( FORMATTING STRING, CONTROL STRING) printf(“%d”, n);

Formatting character

Data type

%d int

%f float

%c char

%s char [ ]

%ld long int

%lf long float or double

control string of printf() function are:‘\n’ - new line character‘\t’ - tab character‘\b’ - backspace characterEg: printf(“the value of i = %d \n”, i);

Page 19: Problem Solving Techniques

To read a value from the keyboard (standard input), the function scanf() is used.

The prototype of scanf() is similar to the prototype of printf().

int x; scanf(“%d”, &x);

• The second parameter of the above scanf() function is &x, which represents the address of x.

• & is the address of operator and when it is being used with a variable, it provides the address of that variable.

Page 20: Problem Solving Techniques

The user or programmer can write functions to define specific tasks that may be used at many points in a program.

The function which calls another function is termed as calling function and the other is termed as called function.

A function declaration may be calledas a function prototype or a function model.

The function prototype has four components.

• Name of the function• Return value type• Number of parameters• Type of each parameter

Page 21: Problem Solving Techniques

Function protype

Function definition

Page 22: Problem Solving Techniques
Page 23: Problem Solving Techniques
Page 24: Problem Solving Techniques

Initialization while(condition){ ……..; processing stmts …….; increment}

for(initialization; condition; increment){ body of the loop;}

do{…….;} while(condition);

Page 25: Problem Solving Techniques

DEFINITION: An array is a collection of

homogeneous elements i.e. elements of similar data type.

EXAMPLE: int a[100];

DECLARATION STMT: int a[10]; /* => array declaration

statement */ a[0]=6; a[1]= 7; …..a[99]=67;

Page 26: Problem Solving Techniques
Page 27: Problem Solving Techniques

Structures are derived data types in C language.

Structures are used to create user-defined types. Structures are commonly used to define records to be stored in files.

A file is a collection of records. A record is a collection of fields of

information.

Page 28: Problem Solving Techniques

Roll number: an integer field Name: an array of characters age: an integer field Consider the following structure

definition:Eg: struct student

{ int rollno; char name[24]; init age; } x, y;

Page 29: Problem Solving Techniques

Accessing the members of Accessing the members of the Structures:the Structures:x.rollno = 1000;x.rollno = 1000;

y.rollno = 1001;y.rollno = 1001;

Pointers to Structures:Pointers to Structures:struct student *ptr;

struct student s1;ptr = &s1;

An Array of Structures:struct student x[5];

x[0].rollno, x[0].name, uctures