Académique Documents
Professionnel Documents
Culture Documents
c
A program is a precise sequence of steps to solve a particular problem.
Programming a computer simply means telling it what to do.
1. Analytical thinking 2.Critical thinking 3.Attention to details
4. Creative synthesis
('))*
'+
(In which we write the code
c (
Translates source code to machine code. Compiler is
efficient and stops on finding error. Interpreter reads line by line so it is slow and it
executes before error.
',To debug the program%%to correct logical errors.
It is a tool which checks our program and includes all those routines or
functions which are used in our program while running.
(It loads program into memory and instructs the processor to start execution
from first instruction.
-
Write a program for printing ͞Welcome͟
.( Preprocessor directive Î Instruction to compiler to include contents of a
system file.
å%/0 Library definition file for all input and output streams.
*+ This is function which runs when the program is used.
Not using ͚main͛ Î error void (void)
12 Curly brackets to group together pieces of program
åå Output stream 00 Input stream to input data
åå indication of direction of data
3 All statements in C++ end with it. Not using it will be a syntax error. Only
semicolon ( ; ) will be a null statement.
4 . . . . 5 Thing in double quotes is called ͚Character String͛ It is displayed on screen
as it is.
6
data is stored in variable It starts with character, ͚78 not recommended
Variable is name of a location in memory. &9 x=2;
In a program every variable has %Name %Type %Size %Value
'& A variable must have data type Difference: due to size in memory
Reserved words can͛t be used as name of variable
/(syntax: char x=͛a͛;) (4 bytes) /(2 bytes +32768)
#(4 bytes) (,(size double of float)
'int x;
:x=5; '(
:int x=5;
z=x+4 (right) x+4=y+4 (wrong) x=x+1
/ + - * / % (modulus)
( Highest () Next * / % Lowest + -
Write programs calculating sum and averages etc.
;
<&$(#c main if else while do for
!(=Algebra y=ax2+bx+c In C y = a*x*x + b*x + c
# Statement(s);
#(condition) }
{ statement(s); }
{ statement(s); } å0å å ?
@ AcB CD And && Or ||
H
'$/ While loop executes zero or more times do while loop executes
one or more times syntax do { statement(s); } while (condition);
Example: Guessing game Relational operator while(trynum<=maxtries&& ͙͙ )
@ for (initialization condition ; termination condition ; increment condition)
{ statement(s) } Example: Calculate Table
, Always think reuse Don͛t use explicit constants
I
$&(using if if (x==͛a͛) { cout<<͟Excellent͟; }
If(x==͛b͛) { cout<<͟Good͟ ; }
$/ switch(variable name) { case ͚a͛: statements;
case ͚b͛: statements; case ͚c͛: statements; . . . . }
D3 switch(grade) { case ͚A͛: cout<<͟excellent͟; break;
case ͚B͛: cout<<͟Good͟; break; . . . . }
(# default: cout<<͟Please enter grade from A to F͟;
Switch can only be whole numbers. This is limitation.
c3 while(trynum<=5) { ͙͙ ͙͙. Continue; }
@'C
J
c'
include͟iostream.h͟
char nm[15]; string variables or 15 characters
const float p=3.14; To declare a constant value
define p 3.14 identifier constant The define directive
The escape sequence \a alert \b back space \t \n
\r carriage return = to move cursor to beginning of current line \\
\͟ \͛ To print ͞ or ͚ on screen \f feed=to leave one blank page on printer.
/$
include<iomanip.h> cout<<setw(5)<<62<<setw(5)<<8<<endl;
To specify width of output (will be printed left justified touched to right)
ASSIGNMENT OPERATOR (=) A character can be assigned but string cannot be
assigned to a string variable.
Compound Assignment Statement x=y=16;
Compound Assignment Expression c*=a+b;
Pre/Post Fixing sum=a+b+c--; Î sum=a+b+c; c=c-1;
Nested If if(avg>33) if(avg>50) if(avg>80) grade=͛A͛;
else grade=͛B͛; else grade=͛C͛; else grade=͛F͛;
Conditional operator res=(a>b)?a:b Î if(a>b) res=a; else res=b;
!(a>b)? cout<<͟1st is larger͟:cout<<͟2nd is larger͟;
STRING VARIABLE
*are declared in the same way as a character variable *In fact a string is an array of
character type *The length of string is total number of elements of array
char variable-name [n]; *The last character of every string variable is a null
character, represented by ͚\0͛ *Thus a string variable can store characters one less
than mentioned b/c last character is null. e.g. char city[5]; can store 4
characters. If ony two characters are entered, third is null. *The null character is
automatically added at the end of the data in string.
Initializing string Variable a) char str[10] = ͞Pakistan͟;
b) char str[10] = { ͚P͛ , ͚a͛ , ͚k͛ , ͚i͛ , ͚s͛ , ͚t͛ , ͚a͛ , ͚n͛ , ͚\0͛}; In (a) null character
͚z0͛ is automatically appended at the end of the word ͞Pakistan͟ In(b) fi null
character is not added at the end of string character then the variable of char type is
handled as an array (not as a string)
WRITE A PROGRAM IN C++ TO INITIALOZE A STRING AND THEN TO MOVE THE STRING
TO LINE NO. 22 BY MOVING ONE CHARACTER AT A TIME. ALSO USE THE DELAY
FUNCTION TO SLOW DOWN THE SPEED OF THE MOVING CHARACTER
inlcude<iostream.h>
include<conio.h>
include<dos.h>
main()
{
Char str[15]=͟Pakistan͟;
Int I,l,c;
clrscr();
gotoxy(32,1);
cout<<str;
for(i=0 ; str[;]!͛\0͛ ; i++)
{
for(l=1 ; l<=22 ; l++)
{
gotoxy(i+32,l);
cout<<str[i];
delay(50);
gotoxy(i+32,l);
cout<<͟ ͞;
}
gotoxy(i+32,l);
cout<<str[i];
}
}
'
include<iostream.h>
include<conio.h>
Main()
{
clrscr();
int a,b;
int *x,*y;
x=&a;
y=&b;
cout<<͟Memory address of a is ͞<<&a;
cout<<͟Memory address of b is ͞<<&b;
cout<<͟Value stored in memory address of x ͞<<*x;
cout<<͟Value stored in memory address of y ͞<<*y;
}
CALL BY REFERENCE
data type name (data type & variable)
{
......;
}
They contain variable location.
Variable used to hold address of another variable.
int* p; float* rep;
Pointer having value 0 or NULL means it points to nothing.
Symbolic constant is defined in <iostream.h>
1) AND (&) operator 2) * operator
Unary operator returns memory address of operand
Int y=5;
int *yptr;
yptr = &y;
cout<<*yptr;
*yptr=9; // assigns 9 to y
* and & are inverses of each other.
cB
A 'c
int M[3][3];
J
c
for (i=0 ; i<=2 ; i++)
for(j=0 ; j<=2 ; j++)
for (l=0 ; l<=2 ; l++)
c[i][i] = c[i][j] + a[i][l]*b[l][j];
'c
@ C (Easy Technique for 2-D Array)
Int c[3][3] = { {0} , {0} }
cB @ c 6
for (l=0 ; l<=2 ; l++)
{
m = a[l][l];
for(k=0 ; k<=3 ; k++)
{
a[l][k] = a[l][k] / m;
}
for(i=l+1 ; i<=2 ; i++) // $(i=0 ; i<=l-1 ; i++)
{
m = a[i][l];
for (j=0 ; j<=3 ; j++)
{
a[i][j] = a[i][j] ʹ m*a[l][j];
}
}
}