problem solving techniques
Post on 31-Oct-2014
218 Views
Preview:
DESCRIPTION
TRANSCRIPT
FLOW CHARTS PSEUDO CODE
They are very precise. They represent our thoughts exactly.
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’
ITERATION: The third fundamental technique is Iteration. It means repeating a set of actions again and again.
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…
Consider the statement: number = number + 1;The tokens are, number - identifer (variable) = - operator + - operator 1 - constant ; - punctuation
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.
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.
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
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)
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()
[ ] - 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.
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}
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);
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.
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
Function protype
Function definition
Initialization while(condition){ ……..; processing stmts …….; increment}
for(initialization; condition; increment){ body of the loop;}
do{…….;} while(condition);
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;
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.
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;
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
top related