Sei sulla pagina 1di 25

[2014-15]

Computer science
Programming through c++.
Aditi Johari
SUBMITTED TO:
NISHANT AGARWAL

CLASS : XI A(PCM)

CERTIFICATE
This is to certify that this project has been made by
Aditi johari of class 11 on the topic coordinate
geometry of triangles under the guidence of our
Computer teacher Nishant Agarwal) and have been
completed it sucessfully.
Yours truely
Aditi johari

Acknowlegement
I would like to express my special thanks of
gratitude to my teacher Nishant mishra as well as
our principal Pinky joshi who gave me the golden
opportunity to do this wonderful project on the
topic triangles through c++ , which also helped me
in doing a lot of Research and i came to know
about so many new things I am really thankful to
them.
Secondly i would also like to thank my parents and
friends who helped me a lot in finalizing this
project within the limited time frame.

Cotents

Sr.no.

TOPIC

1.
2.
3.
4.

Introduction

Page no.

programming
output discripton
bibliography

Introduction
C++ (pronounced cee plus plus) is a general-purpose programming language. It has imperative,
object-oriented and generic programming features, while also providing the facilities for lowlevel memory manipulation.
It is designed with a bias toward system programming (e.g., for use in embedded systems or
operating system kernels), with performance, efficiency and flexibility of use as its design
requirements. C++ has also been found useful in many other contexts, including desktop
applications, servers (e.g. e-commerce, web search or SQL servers), performance-critical
applications (e.g. telephone switches or space probes), and entertainment software.C++ is a

compiled language, with implementations of it available on many platforms and provided by


various organizations, including the FSF, LLVM, Microsoft and Intel.
C++ is standardized by the International Organization for Standardization (ISO), with the latest
(and current) standard version ratified and published by ISO in September 2011 as ISO/IEC
14882:2011 (informally known as C++11).[4] The C++ programming language was initially
standardised in 1998 as ISO/IEC 14882:1998, which was then amended by the C++03, ISO/IEC
14882:2003, standard. The current C++11 standard supersedes these, with new features and an
enlarged standard library. Before the initial standardization in 1998, C++ was developed by
Bjarne Stroustrup at Bell Labs, starting in 1979, who wanted an efficient flexible language (like
the C language), which also provided high-level features for program organization.
Many other programming languages have been influenced by C++, including C#, Java, and
newer versions of C (after 1998).

Standard library
The C++ standard consists of two parts: the core language and the C++ Standard Library. C++
programmers expect the latter on every major implementation of C++; it includes vectors, lists,
maps, algorithms (find, for_each, binary_search, random_shuffle, etc.), sets, queues, stacks,
arrays, tuples, input/output facilities (iostream, for reading from and writing to the console and
files), smart pointers for automatic memory management, regular expression support, multithreading library, atomics support (allowing a variable to be read or written to be at most one
thread at a time without any external synchronisation), time utilities (measurement, getting
current time, etc.), a system for converting error reporting that doesn't use C++ exceptions into
C++ exceptions, a random number generator and a slightly modified version of the C standard
library (to make it comply with the C++ type system).
A large part of the C++ library is based on the STL. This provides useful tools as containers (for
example vectors and lists), iterators to provide these containers with array-like access and
algorithms to perform operations such as searching and sorting. Furthermore (multi)maps
(associative arrays) and (multi)sets are provided, all of which export compatible interfaces.
Therefore it is possible, using templates, to write generic algorithms that work with any container
or on any sequence defined by iterators. As in C, the features of the library are accessed by using
the #include directive to include a standard header. C++ provides 105 standard headers, of
which 27 are deprecated.
The standard incorporates the STL that was originally designed by Alexander Stepanov, who
experimented with generic algorithms and containers for many years. When he started with C++,
he finally found a language where it was possible to create generic algorithms (e.g., STL sort)
that perform even better than, for example, the C standard library qsort, thanks to C++ features
like using inlining and compile-time binding instead of function pointers. The standard does not
refer to it as "STL", as it is merely a part of the standard library, but the term is still widely used
to distinguish it from the rest of the standard library (input/output streams, internationalization,
diagnostics, the C library subset, etc.).

Most C++ compilers, and all major ones, provide a standards conforming implementation of the
C++ standard library.

Functions and Procedural Abstraction


The Need for Sub-programs
A natural way to solve large problems is to break them down into a series of subproblems, which can be solved more-or-less independently and them combined to
arrive at a complete solution. In programming, this methodology reflects itself in the
use of sub-programs, and in C++ all sub-programs are called functions
(corresponding to both "functions" and "procedures" in Pascal and some other
programming languages).

We have already been using sub-programs. For example, in the program which generated a table
of square roots, we used the following "for loop":
...
#include<math.h>
...
...
for (number = 1 ; number <= 10 ; number = number + 1) {
cout.width(20);
cout << number << sqrt(number) << "\n";
}
...
...

The function "sqrt(...)" is defined in a sub-program accessed via the library file
"math.h". The sub-program takes "number", uses a particular algorithm to compute its
square root, and then returns the computed value back to the program. We don't care
what the algorithm is as long as it gives the correct result. It would be ridiculous to
have to explicitly (and perhaps repeatedly) include this algorithm in the "main"
program.

In this chapter we will discuss how the programmer can define his or her own functions. At first,
we will put these functions in the same file as "main". Later we will see how to place different
functions in different files.

User-defined Functions
Here's a trivial example of a program which includes a user defined function, in this
case called "area(...)". The program computes the area of a rectangle of given length
and width.
#include<iostream.h>
int area(int length, int width);
declaration */

/* function

/* MAIN PROGRAM: */
int main()
{
int this_length, this_width;
cout << "Enter the length: ";
cin >> this_length;
cout << "Enter the width: ";
cin >> this_width;
cout << "\n";

/* <--- line 9 */

/* <--- line 13 */

cout << "The area of a " << this_length << "x" << this_width;
cout << " rectangle is " << area(this_length, this_width);
return 0;
}
/* END OF MAIN PROGRAM */
/* FUNCTION TO CALCULATE AREA: */
int area(int length, int width)
/* start of function definition */
{
int number;
number = length * width;
return number;
}
/* END OF FUNCTION */

/* end of function definition */

Although this program is not written in the most succinct form possible, it serves to illustrate a
number of features concerning functions:

The structure of a function definition is like the structure of "main()", with its
own list of variable declarations and program statements.

A function can have a list of zero or more parameters inside its brackets, each
of which has a separate type.

A function has to be declared in a function declaration at the top of the


program, just after any global constant declarations, and before it can be called
by "main()" or in other function definitions.

Function declarations are a bit like variable declarations - they specify which
type the function will return.

A function may have more than one "return" statement, in which case the function
definition will end execution as soon as the first "return" is reached. For example:
double absolute_value(double number)
{
if (number >= 0)
return number;
else
return 0 - number;
}

Value and Reference Parameters


The parameters in the functions above are all value parameters. When the function is
called within the main program, it is passed the values currently contained in certain
variables. For example, "area(...)" is passed the current values of the variables
"this_length" and "this_width". The function "area(...)" then stores these values in
its own private variables, and uses its own private copies in its subsequent
computation.

GETTING STARTED
WITH PROGRAMMING

PROGRAM TO PRINT A TRIANGLE BY ACCEPTING


THE COORDINATES OF THE THREE RESPECTIVE
SIDES?

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include

<math.h>

void show_screen( );
void Triangle(const int, const int,const int,const int,const
int,const int);

void Line(const int,const int,const int,const int);


int main( )
{
int driver=VGA;int mode=VGAHI; int x_1=0;
int y_1=0;int x_2=0;
int y_2=0;
int x_3=0;
int y_3=0;
do
{
show_screen( );
gotoxy(8,10);
cout<<"Coordinates of Point-I (x1,y1) :";

gotoxy(8,11);
cout<<"";
gotoxy(12,13);
cout<<"Enter the value of x1 = ";
cin>>x_1;
gotoxy(12,14);
cout<<"Enter the value of y1 = ";
cin>>y_1;
gotoxy(8,18);
cout<<"Coordinates of Point-II (x2,y2) :";
gotoxy(8,19);
cout<<"";

gotoxy(12,21);
cout<<"Enter the value of x2 = ";
cin>>x_2;
gotoxy(12,22);
cout<<"Enter the value of y2 = ";
cin>>y_2;
gotoxy(8,26);
cout<<"Coordinates of Point-III (x3,y3) :";
gotoxy(8,27);
cout<<"";
gotoxy(12,29);
cout<<"Enter the value of x3 = ";

cin>>x_3;
gotoxy(12,30);
cout<<"Enter the value of y3 = ";
cin>>y_3;
initgraph(&driver,&mode,"..\\Bgi");
setcolor(15);
Triangle(x_1,y_1,x_2,y_2,x_3,y_3);
setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any
other key to exit.");
int key=int(getch( ));

if(key!=13)
break;
}
while(1);
return 0;}
void Triangle(const int x_1,const int y_1,const int
x_2,const int y_2,const int x_3,const int y_3)
{
Line(x_1,y_1,x_2,y_2);
Line(x_2,y_2,x_3,y_3);
Line(x_3,y_3,x_1,y_1);
}
void Line(const int x_1,const int y_1,const int x_2,const
int y_2)
{

int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}

int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{

x++;
if(p<0)
p+=two_dy;
else
{
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
}
}
else
{

int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else

{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
}
}
}
void show_screen( )
{
restorecrtmode( );
textmode(C4350);

cprintf("\n*****************************************
***************************************");
cprintf("*********************************-*********************************");
cprintf("*--------------------------------- ");
textbackground(1);
cprintf(" Triangle ");
textbackground(8);
cprintf(" ---------------------------------*");
cprintf("*********************************-*********************************");
cprintf("***************************************************
**************************-*");

for(int count=0;count<42;count++)
cprintf("*-*");
gotoxy(1,46);
cprintf("***************************************************
**************************-*");
cprintf("*-----------------------------------------------------------------------------*");
cprintf("*******************************************
*************************************");
gotoxy(1,2);
}

BIBLIO GRAPHY
INTERNET(icbse.com)
C++ COURSE BOOK
C++ REFRENCE BOOK

THE END
THANKYOU

Potrebbero piacerti anche