Вы находитесь на странице: 1из 36

By MUKESH KUMAR (Roll no - 0593)

HINDU COLLEGE,
UNIVERSITY OF DELHI
C++ LANGUAGE

MUKESH KUMAR
Student of The Hindu college
University of Delhi

College PUBLICATION PVT.LTD


Delhi

2
Dedicated to my best teacher
Mr. S.K.Chauhan and Mr. Amit sir

3
Contents

1 C++ Fundamentals 3
1.1 Character set . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 C++ Token . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Identifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6.1 Integer . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6.2 Float . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6.3 character . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6.4 void . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.7 Varables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8 operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.8.1 Assignment operators . . . . . . . . . . . . . . . . . . . 6
1.8.2 Logical operator . . . . . . . . . . . . . . . . . . . . . . 7
1.8.3 Relational operator . . . . . . . . . . . . . . . . . . . . 7
1.8.4 Arithmetic operators . . . . . . . . . . . . . . . . . . . 7
1.8.5 Auto increment / Decrement operators . . . . . . . . . 8
1.9 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

2 structure of C++ program 10


2.1 explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

3 Control Stetements 13
3.1 Conditional Structure . . . . . . . . . . . . . . . . . . . . . . . 14
3.1.1 If and else . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.2 Itration Structures(Loop) . . . . . . . . . . . . . . . . . . . . 15
3.2.1 While Loop . . . . . . . . . . . . . . . . . . . . . . . . 15

1
3.2.2 The do-while Loop . . . . . . . . . . . . . . . . . . . . 17
3.2.3 for loop . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.3 Jumping Statement . . . . . . . . . . . . . . . . . . . . . . . . 20
3.3.1 The break Statement . . . . . . . . . . . . . . . . . . . 20
3.3.2 The Continue Statement . . . . . . . . . . . . . . . . . 20
3.3.3 The goto Statement . . . . . . . . . . . . . . . . . . . . 21
3.3.4 The exit function . . . . . . . . . . . . . . . . . . . . . 22
3.4 The Selective Structure . . . . . . . . . . . . . . . . . . . . . . 22
3.4.1 Switch . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2
Chapter 1

C++ Fundamentals

Like all programming language c++ has its own vocabulary and grammar.
The sequence instruction that are given to execute a specific task is called a
program. The instructions are formed of certain words, symbols and num-
bers which strictly follow that c++ grammar or sentence.

In this chapter we shall discuss this C++ grammer consisting of


character sets ,variables , constant, data types ,operators and expressions in
the way they are related to the C++ programming language

1.1 Character set


The chacters are used to form word ,number, and expressions,in any pro-
gramming language .these depend individual style of each programming lan-
guage.in c++ are grouped

Letters(A to Z uppercase letters and lowercase a to z.c++ and c is


case sensitive language),Digits(0 to 9),special characters(+,-,()).

1.2 C++ Token


There are five kind of tokens in C++ and they are identifier,keywords,literals,operators
and white spaces(blank space).

3
1.3 Identifier
The identifier maybe the name of a variable or a constant of a function. usu-
ally for all programming languages, the first character of an identifier will
be an alphabet followed by either by numbers or alphabets. unlike, C++
does not impose any restriction on identifier length. Also not that all C++
compilers are case sensitive; underscores are allowed for battery readability,
and no is special characters are allowed while defining an identifier.

1.4 keywords
Keywords are words that have specific purpose in c++ language. So , we
can not use them as our variable name or function name. there are about 63
keywords in C++ .

example- float,int ,break,do,if,else,void,return,while,switch,goto,char


etc.

1.5 constants
The value of a quantity that does not vary throughout, the program is called
a constant .

1.6 Data Types


The basic data types in C++ are:Integer,float,character,and void(or value
less)

1.6.1 Integer
These are numbers without decimal part, which may or maynot be prefixed
with minus(-) sign.

4
e.g.

102 -234 231

The modifiers when applied to integers and integer range are as fol-
lows.
short integer range is -32768 to +32767 and occupies 2 bytes.
integer range is from -2147483648 to 2147483647and occupies 4 bytes.
long integer range is -9223372036854775808L to 9223372036854775807 and
occupies 8 byte.

1.6.2 Float
float are numbers with decimal part. In C++, floats are stored with and
digits of decimal precision.

The modifier and their range is given blow:


float - stores up to 7 decimal places and occupies 4 bytes of memory
double- stores up to 15 decimal places and occupies 8 bytes of memory
long double- stores up to 19 decimal places and occupies 10 bytes of memory

1.6.3 character
Any single letter enclosed within single quotes, can be defined as a character
type data.

(e.g.) a,@ etc.

Like integer, we can also specify a variable of char using the mod-
ifiers signed or unsigned, which will affect the range of integers that can be
represented.

1.6.4 void
It is valueless. It has two uses: One to explicitly declare a function as
returning no value: second to create generic pointers.

5
1.7 Varables
A variable is the storehouse for constants. (variables are seperated by comma).All
C++ variable must be declared They are used. The general form of variable
declaration is ,

Datatype variablename;
declaration statement must end with a semicolon.

example:- int a,r;


C++ also permit us to initialize values along with the declaration as follows
float pie=3.14;

1.8 operators
c++ is very rich in built-in operators . An operator is a symbol that tells
the compiler to perform specific mathematical or logical manipulation.lke C
,C++ also has three general classes of operators: airthmetic,relational and
logical etc.

1.8.1 Assignment operators


These operators are used to assign the result of an expression to a variable.
The usual assignment operator is = the assignment operator, again has got
three
(i) simple assignment
(ii) multiple assignment
(iii) compound assignment

(e.g.)
a=15; simple
a=b=c=2; multiple
a+=3; compound
In the third example += means add 15 to a (or) increment a by 15

6
1.8.2 Logical operator
C++ has three logical operators as
&& means Logical AND
|| means Logical OR
! means Logical NOT

These are used when we want to test more than one condition and
make dicisions.
(e.g.) a>b&& x==10;
this expression is true only if a>b is true and x==10 is also true. if either(or
both) of them is false, the expression is also false.

1.8.3 Relational operator


These operators are used to compare two quantities; and depending on their
relation, to take decesions.
(e.g.)
10<20 is true
10>20 is false

C++ support six relational operators


<lessthan
<=lessthen or equal to
>greater then
>=greater than or equal to
==equal to
!= not equal to

it is important to remeber, that all expression using relation and


logical operators, will produce a result of either 0 or 1.

1.8.4 Arithmetic operators


C++ is provided with all basic arithmetic operators. These can create any
built -in data allowed in C++. The arithmetic operators are;
+ addtion

7
*multiplication
- subtraction
/ division

% modulo division (remainder after division ,e.g. 10%3=1 and


3%10=3) Again you are reminded, that modulo division (%) operator yield
the remainder of an integer division.

1.8.5 Auto increment / Decrement operators


These are very useful operators, generally not found in other languages. They
are ++ and , The operator ++ adds 1 while the operator sb-
tracts 1.

These are again of two types,viz.,prefix auto increment /decrement.


++a a

A postfix operator first assigns the value to the variable on the left
and then assigned to the variable on the left. (e.g.)
a=5
b=++a
then the result will be
a=b=6

A postfix operator first assigns the value to tjhe variable on the left
and then increment/ decrement the oprend.
(e.g.)
a=5
b=a++
here b=5 and a=6

1.9 Comments
C++ provides two styles of comments. The first comment style supports
traditional c-language comments. All text appearing between /*and */ is
treated as a comment. comments of this type can span multiple lines

8
e.g.
/*multiline comment*/
The second comment style supports single line C++ comments. All text
follow // untile the end of the line is treated as a comment
e.g.
//single line comment

9
Chapter 2

structure of C++ program

before writing a program in any language one must know about its structure
for basic it is line number and FORTRAN it is format statements likewise
on C++ program must adhere to the structure it is designed for. in simple
C++ program should have the following details

//first c++ program


#include<iostream>
using namespace std;
int main()
{
cout<<All is well;
return 0;
}

2.1 explanation
The program lines have been indicated for explanation purpose only and they
do not form part of the program.

line 1. comment line to explain the purpose of the program and


usually the compiler ignores it.

line 2. It is customary in all C++ programs to start with a #in-


clude file inclusion in our program. This statement states that the header file

10
<iostream> include in this program. Also note that there are no space be-
tween the angled brackets and iostream. If we insert any blank inside, most
compliers (which is compile our program in machine language ) consider it
as a serious error and the program wont compile.

Line 3. The main difference between old style C++ program and
new style C++ program is inclusion of this statement, using namespace
std;. According to ANSI C++ definition, all the classes, objects and func-
tions of the standard C++ library are defined within namespace std. Thus,
this statement used to indicate all the functions used in the program belongs
to the standard library. This using directive indicates that we want to refer
to things within the namespace called std.

Line 4. This program consists of only one function called main().


As we know from other languages a function is a self-contained block of code
that is refrenced by a name. Here the name of the function is main. A pro-
gram can have any number of functions ,but it must have only one function
named main().

Line 5. The opening brace { is used to indicate the beginning of


the function of main().
Line 6. The first executable statement in this program is

cout<<All is well;
this statement displayed massage on the output screen.

Line 7. The second executable statement in this program is


return 0;
This statement indicates the end of the function main() and it also returns
the value zero to operating system. Value other than zero may also be used
but they are not used for normal exit.

Line 8. the closing brace } is used to indicate the end of the func-
tion main().

The standard input and output statement avialable in c++ are cin
and cout. The cin refers keyword and cout refer display screen respactivly.In
C++ we use two operators namely extraction operator (>>) and insertion

11
operator(<<) for input and output purposes. The header file iostream con-
sists of all necessary definition for the operators, cin and cout. Hence it is
essential to include the header file iostream in all the programs requiring
input and output operations.

For example, if we want to enter 5,2,3 for a,b,c and all belong to int
type, then the declaration and input statements might look like this ,
int a, b, c;
cin>>a>>b>>c;
and this way of giving input is called cascading of input,then we have to
enter data follows
523
note that there is space between two values.

similarly,when we want to display message we use the following


form;
cout<<All is well;
to display the message along with its value we use another form as follows,
cout<<area<<a;

12
Chapter 3

Control Stetements

A program is usually not limited to a linear sequence of instructions. During


its process it may bifurcate, repeat code or take decisions. For that purpose,
C++ provides control structures that serve to specify what has to be done
by our program, when and under which circumstances.

With the introduction of control structures we are going to have


to introduce a new concept: the compound-statement or block. A block is
a group of statements which are separated by semicolons (;) like all C++
statements, but grouped together in a block enclosed in braces: { }:

{ statement1; statement2; statement3; }

Most of the control structures that we will see in this section re-
quire a generic statement as part of its syntax. A statement can be either a
simple statement (a simple instruction ending with a semicolon) or a com-
pound statement (several instructions grouped in a block), like the one just
described. In the case that we want the statement to be a simple statement,
we do not need to enclose it in braces ({ }). But in the case that we want the
statement to be a compound statement it must be enclosed between braces
({ }), forming a block.

13
3.1 Conditional Structure
3.1.1 If and else
The if keyword is used to execute a statement or block only if a condition is
fulfilled. Its form is:
if (condition) statement

Where condition is the expression that is being evaluated. If this


condition is true, statement is executed. If it is false, statement is ignored
(not executed) and the program continues right after this conditional struc-
ture. For example, the following code fragment prints x is 100 only if the
value stored in the x variable is indeed 100:

if (x == 100)
cout << x is 100;
If we want more than a single statement to be executed in case that the
condition is true we can specify a block using braces :
if (x == 100)
{
cout<< x is ;
cout << x;
}

We can additionally specify what we want to happen if the condi-


tion is not fulfilled by using the keyword else. Its form used in conjunction
with if is:

if (condition)
statement1
else statement2

For example:

if (x == 100)
cout << x is 100;
else
cout << x is not 100;

14
prints on the screen x is 100 if indeed x has a value of 100, but if it
has not -and only if not- it prints out x is not 100. The if + else structures
can be concatenated with the intention of verifying a range of values. The
following example shows its use telling if the value currently stored in x is
positive, negative or none of them (i.e. zero):

if (x >0)
cout << x is positive;
else if (x <0)
cout << x is negative;
else
cout << x is 0;

Remember that in case that we want more than a single statement


to be executed, we must group them in a block by enclosing them in braces{
}.

3.2 Itration Structures(Loop)


Loops have as purpose to repeat a statement a certain number of times or
while a condition is fulfilled.

3.2.1 While Loop


Its format is:
while (expression) statement

and its functionality is simply to repeat statement while the condi-


tion set in expression is true. For example, we are going to make a program
to countdown using a while-loop:

// custom countdown using while


#include <iostream>
using namespace std;
int main ()

15
{
int n;
cout << Enter the starting number > ;
cin >>n;
while (n>0)
{
cout << n << , ;
n;
}
cout << FIRE!;
return 0;
}

Enter the starting number 8


8, 7, 6, 5, 4, 3, 2, 1, FIRE!

When the program starts the user is prompted to insert a starting


number for the countdown. Then the while loop begins, if the value entered
by the user fulfills the condition n0 (that n is greater than zero) the block
that follows the condition will be executed and repeated while the condition
(n0) remains being true. The whole process of the previous program can be
interpreted according to the following script (beginning in main):

1. User assigns a value to n


2. The while condition is checked (n0). At this point there are two posibil-
ities:
condition is true: statement is executed (to step 3)
condition is false: ignore statement and continue after it (to step 5)
3. Execute statement:
cout << n << , ;
n;
(prints the value of n on the screen and decreases n by 1)
4. End of block. Return automatically to step 2
5. Continue the program right after the block: print FIRE! and end program.

When creating a while-loop, we must always consider that it has to


end at some point, therefore we must provide within the block some method
to force the condition to become false at some point, otherwise the loop will

16
continue looping forever. In this case we have included n; that decreases
the value of the variable that is being evaluated in the condition (n) by one -
this will eventually make the condition (n0) to become false after a certain
number of loop iterations: to be more specific, when n becomes 0, that is
where our while-loop and our countdown end.

Of course this is such a simple action for our computer that the
whole countdown is performed instantly without any practical delay between
numbers.

3.2.2 The do-while Loop


Its format is:
do statement while (condition);

Its functionality is exactly the same as the while loop, except that
condition in the do-while loop is evaluated after the execution of statement
instead of before, granting at least one execution of statement even if con-
dition is never fulfilled. For example, the following example program echoes
any number you enter until you enter 0.

// number echoer
#include <iostream>
using namespace std;
int main ()
{
unsigned long n;
do {
cout << Enter number (0 to end): ;
cin >> n;
cout << You entered: << n << ;
} while (n != 0);
return 0;
}

Enter number (0 to end): 12345


You entered: 12345

17
Enter number (0 to end): 160277
You entered: 160277
Enter number (0 to end): 0
You entered: 0

The do-while loop is usually used when the condition that has to
determine the end of the loop is determined within the loop statement itself,
like in the previous case, where the user input within the block is what is
used to determine if the loop has to end. In fact if you never enter the value
0 in the previous example you can be prompted for more numbers forever.

3.2.3 for loop


Its format is:
for (initialization; condition; increase)
statement;

and its main function is to repeat statement while condition remains


true, like the while loop. But in addition, the for loop provides specific lo-
cations to contain an initialization statement and an increase statement. So
this loop is specially designed to perform a repetitive action with a counter
which is initialized and increased on each iteration. It works in the following
way:
1. initialization is executed. Generally it is an initial value setting for a
counter variable. This is executed only once.
2. condition is checked. If it is true the loop continues, otherwise the loop
ends and statement is skipped (not executed).
3. statement is executed. As usual, it can be either a single statement or a
block enclosed in braces { }.
4. finally, whatever is specified in the increase fieldis executed and the loop
gets back to step 2.
Here is an example of countdown using a for loop:

// countdown using a for loop


#include <iostream>
using namespace std;
int main ()

18
{
for (int n=10; n>0; n)
{
cout << n << , ;
}
cout FIRE!;
return 0;
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
FIRE!

The initialization and increase fields are optional. They can remain
empty, but in all cases the semicolon signs between them must be written.
For example we could write: for (;n<10;) if we wanted to specify no initializa-
tion and no increase; or for (;n<10;n++) if we wanted to include an increase
field but no initialization (maybe because the variable was already initialized
before). Optionally, using the comma operator (,) we can specify more than
one expression in any of the fields included in a for loop, like in initialization,
for example. The comma operator (,) is an expression separator, it serves to
separate more than one expression where only one is generally expected. For
example, suppose that we wanted to initialize more than one variable in our
loop:

for ( n=0, i=100 ; n!=i ; n++, i )


{
// whatever here...
}

This loop will execute for 50 times if neither n or i are modified


within the loop: n starts with a value of 0, and i with 100, the condition is
n!=i (that n is not equal to i). Because n is increased by one and i decreased
by one, the loops condition will become false after the 50th loop, when both
n and i will be equal to 50.

19
3.3 Jumping Statement
3.3.1 The break Statement
Using break we can leave a loop even if the condition for its end is not ful-
filled. It can be used to end an infinite loop, or to force it to end before its
natural end. For example, we are going to stop the count down before its
natural end (maybe because of an engine check failure?):

// break loop example


#include<iostream>
using namespace std;
int main ()
{
int n;
for (n=10; n>0; n)
{
cout << n << , ;
if (n==3)
{
cout << countdown aborted!;
break;
}
}
return 0;
}

10, 9, 8, 7, 6, 5, 4, 3,
countdown aborted!

3.3.2 The Continue Statement


The continue statement causes the program to skip the rest of the loop in
the current iteration as if the end of the statement block had been reached,
causing it to jump to the start of the following iteration. For example, we
are going to skip the number 5 in our countdown:

20
// continue loop example
#include <iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n)
{
if (n==5) continue;
cout << n << , ;
}
cout << FIRE!;
return 0;
}

10, 9, 8, 7, 6, 4, 3, 2, 1,
FIRE!

3.3.3 The goto Statement


goto allows to make an absolute jump to another point in the program. You
should use this feature with caution since its execution causes an uncondi-
tional jump ignoring any type of nesting limitations. The destination point
is identified by a label, which is then used as an argument for the goto state-
ment. A label is made of a valid identifier followed by a colon (:). Generally
speaking, this instruction has no concrete use in structured or object oriented
programming aside from those that low-level programming fans may find for
it. For example, here is our countdown loop using goto:

// goto loop example


#include<iostream>
using namespace std;
int main ()
{
int n=10;
loop:
cout << n << , ;

21
n;
if (n>0) goto loop;
cout << FIRE!;
return 0;
}

10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
FIRE!

3.3.4 The exit function


exit is a function defined in the cstdlib library. The purpose of exit is to
terminate the current program with a specific exit code. Its prototype is:
void exit (int exitcode); The exitcode is used by some operating systems and
may be used by calling programs. By convention, an exit code of 0 means
that the program finished normally and any other value means that some
error or unexpected results happened.

3.4 The Selective Structure


3.4.1 Switch
The syntax of the switch statement is a bit peculiar. Its objective is to check
several possible constant values for an expression. Something similar to what
we did at the beginning of this section with the concatenation of several if
and else if instructions. Its form is the following:

switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;

22
.
.
.
default:
default group of statements
}

It works in the following way: switch evaluates expression and


checks if it is equivalent to constant1, if it is, it executes group of statements
1 until it finds the break statement. When it finds this break statement the
program jumps to the end of the switch selective structure. If expression was
not equal to constant1 it will be checked against constant2. If it is equal to
this, it will execute group of statements 2 until a break keyword is found,
and then will jump to the end of the switch selective structure. Finally, if
the value of expression did not match any of the previously specified con-
stants (you can include as many case labels as values you want to check),
the program will execute the statements included after the default: label, if
it exists (since it is optional). Both of the following code fragments have the
same behavior:

switch example if-else equivalent

switch (x)
{
case 1:
cout << x is 1;
break;
case 2:
cout << x is 2;
break;
default:
cout << value of x unknown;
}

if (x == 1)
{
cout << x is 1;
}

23
else if (x == 2)
{
cout << x is 2;
}
else
{
cout << value of x unknown;
}

The switch statement is a bit peculiar within the C++ language be-
cause it uses labels instead of blocks. This forces us to put break statements
after the group of statements that we want to be executed for a specific con-
dition. Otherwise the remainder statements including those corresponding
to other labels- will also be executed until the end of the switch selective
block or a break statement is reached. For example, if we did not include a
break statement after the first group for case one, the program will not auto-
matically jump to the end of the switch selective block and it would continue
executing the rest of statements until it reaches either a break instruction
or the end of the switch selective block. This makes unnecessary to include
braces { } surrounding the statements for each of the cases, and it can also be
useful to execute the same block of instructions for different possible values
for the expression being evaluated. For example:

switch (x)
{
case 1:
case 2:
case 3:
cout << x is 1, 2 or 3;
break;
default:
cout << x is not 1, 2 nor 3;
}

Notice that switch can only be used to compare an expression


against constants. Therefore we cannot put variables as labels (for exam-
ple case n: where n is a variable) or ranges (case (1..3):) because they are
not valid C++ constants. If you need to check ranges or values that are not

24
constants, use a concatenation of if and else if statements.
some programs
//fibonacci series
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,first=0,second=1,next;
cout<<enter the no. of terms of you want<<endl;
cin>>n;
for(i=0;i<n;i++)
{
if(i<=1)
next=i;
else
{
next=first+second;
first=second;
second=next;
}
cout<<next<<endl;
}
getch();
}

// ascending order
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x)
void main()
{
clrscr();
int n,i,j,a[100],temp;
cout<<how many numbers you want to arrange in ascending order;
cin>>n;
cout >>please enter those numbers ;

25
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=0;j <n-i;j++)
{
if(a[i]>a[i+])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
cout<<your entered numbers,in ascending order,are: n;
for(i=0;i<n;i++)
{
cout<<a[i]<< t;
}
getch();
}

// descending order
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,j,a[100],temp;
cout<<how many numbers you want to arrange in descending order;
cin>>n;
cout<<please enter those numbers;
for(i=0;in;i++)
{
cin>>a[i];
}

26
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
coutyour entered number ,in descending order, are:;
for(i=0;i<n;i++)
{
cout<<a[i]<< t;
}
getch();
}

// secant method
#include<iostream.h>
#include<conio.h>
#include<math.h> #define f(x) ((x-cos(x))
void main()
{
clrscr();
float x,x2,x1,accuracy;
cout<<please input boundary :<<endl;
cout<<please input lower boundary: ;
cin>>x;
cout<<please input upper boundary: ;
cin>>x1;
cout<<please input accuracy: ;
cin>>accuracy;
do
{
x2=x1-f(x1)*(x1-x)/(f(x1)-f(x));

27
x=x1;
x1=x2;
}
while(accuracy<fabs(f(x2)));
cout<<solution is = <<x2;
getch();
}

//simpson 1/3
#include<iostream.h>
#include<conio.h>
#define f(x) (2*(x)-2)
void main()
{
clrscr();
float a,b,h,x,sum,I,n;
cout<<please enter limits : ;
cout<<please enter lower limit: ;
cin>>a;
cout<<please enter upper limit:;
cin>>b;
cout<<input number of iteration : ;
cin>>n;
h=(b-a)/n;
for(int c=1;c<=n;c++)
{
x=a+h*c;
if(c%2 == 0)
sum=sum+2*f(x);
else
sum=sum+4*f(x);
}
I=h*(sum+f(b))/3;
cout<<solution is =<<I;
getch();
}

//bisection method

28
#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) ((x)-cos(x))
void main()
{
clrscr();
float a,b,c,accuracy,solu ;
cout<<enter boundary b/w solution you want;
cin>>a>>b;
cout<<enter accuracy=;
cin>>accuracy;
if(f(a)*f(b)<0)
{
do
{
c=(a+b)/2;
if(f(c)*f(b)>0)
b=c;
else
a=c;
}
while(accuracy<fabs(f(c)));
cout<<solution is =<<c;
}
else
cout<<solution does not lies b/w boundary;
getch();
}

//larger number from a list with its location


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],max,poss,n,i;
cout<<program for select larger number from a givan list;

29
cout<<how many numbers you want to enter ;
cin>>n;
cout<<input those number n;
for(i=0;i <=n;i++)
{
cin>>a[i];
}
max=a[0];
for(i=0;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
poss=i+1;
}
}
cout<<larger number is=<<max;
cout<< n position=<<poss;
getch();
}

//newton raphson method to solve equation


#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) ((x)-cos(x))
#define f1(x) (1+sin(x))
void main()
{
clrscr();
float x1,x,accuracy;
cout<<input a number which is near to solution=;
cin>>x;
cout<input accuracy how much you want=;
cin>>accuracy;
do
{
x1=x-(f(x)/f1(x));

30
x=x1;
}
while(accuracy<fabs(f(x)));
cout<<solution is =<<x;
getch();
}

//sum and average of array


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[100],n,sum=0;v float avg;
cout<<how many number you want to manupulate;
cin>>n;
cout <<enter the numbers of array<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
sum=sum+a[i];
}
avg=sum/n;
cout<<the sum is=<<sum<<endl;
cout<<the average is=<<avg<<endl;
getch();
}

//trapazoidal method for limit to limit integration


#include<iostream.h>
#include<conio.h>
#define f(x) (2*(x)-2)
void main()
{
clrscr();
float a,b,n=10000,h,c,x,sum,I;
cout<<enter lower limit= ;
cin>>a;

31
cout<<enter upper limit=;
cin>>b;
h=(b-a)/n;
for(c=2;c<=n+1;c++)
{
x=(a+h*c);
sum=sum+2*f(x);
}
I=h/2*(f(a)+sum);
cout<<integration is =<<I;
getch();
}

//simpson 3/8 method for limit to limit integration


#include<iostream.h>
#include<conio.h>
#define f(x) (2*(x)-2)
void main()
{
clrscr();
float a,b;
cout<<please enter limits: <<endl;
cout<<input lower limit: ;
cin>>a;
cout<<enter upper limit: ;
cin>>b;
int A=(b-a)*(f(a)+3*f((2*a+b)/3)+3*f((a+2*b)/3)+f(b))/8;
cout<<integration b/w limits is =<<A;
getch();
}

32
Index

arithmatic operators, 7 itration structures, 15


ascending order(program), 25
assignment operators, 6 Jumping Statement, 20
Auto increment / Decrement opera- keywords, 4
tors, 8
larger number from a list with its lo-
bisection method (program), 29 cation(program), 29
break statement, 20 logic operator, 7
C++ token, 3
newton rapson method (program), 30
character, 5
character set , 3 operators, 6
comments, 8
conditional structure, 14 program explanation, 10
constants, 4
relational operator, 7
continue statement, 20
control statement , 13 secant method(program), 27
simpson 1/3 method (program), 28
data type, 4
simpson 3/8 method (program), 32
decending order(program), 26
sum and average of array (program),
do-while loop, 17
31
exit function, 22 switch, 22

fibonacci series (program), 25 The selective structure, 22


float, 5 trapezoidal method(program), 31
for loop, 18
variables, 6
goto statement, 21 void, 5

identifire, 4 while loop, 15


if and else, 14
integer, 4

33

Вам также может понравиться