C++ Programming Code Examples
C++ > Algorithms Code Examples
Complex nos
/* Complex nos */
#include<iostream.h>
#include<conio.h>
class complex
{
private:
float real,img;
public:
void assign(float x,float y)
{
real=x;
img=y;
}
void print()
{ if(img>=0)
cout<<real<<"+"<<img<<"i";
else
cout<<real<<img<<"i";
getch();
}
};
void add( float a,float b,float c, float d)
{
float e,f;complex g;
e=a+c;
f=b+d;
g.assign(e,f);
g.print();
}
void sub( float a,float b,float c, float d)
{
float e,f;complex g;
e=a-c;
f=b-d;
g.assign(e,f);
g.print();
}
void mul( float a,float b,float c, float d)
{
float e,f; complex g;
e=a*c-b*d;
f=b*c+a*d;
g.assign(e,f);
g.print();
}
void main()
{
float a,b,c,d;
complex x,y,z;
clrscr();
cout<<" for complex 1:";
cout<<"real part:";
cin>>a;
cout<<"imaginary part:";
cin>>b;
cout<<" for complex 2:";
cout<<"real part:";
cin>>c;
cout<<"imaginary part:";
cin>>d;
x.assign(a,b);
y.assign(c,d);
cout<<"**************original data:************\n";
cout<<"Complex 1:\n";x.print();
cout<<"\n Complex 2:\n";y.print();
cout<<"\n************=================**********\n";
cout<<"\n Addition:\n";add(a,b,c,d);
cout<<"\n Subtraction:\n";sub(a,b,c,d);
cout<<"\n Multipication:\n";mul(a,b,c,d);
}
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
In C++, constructor is a special method which is invoked automatically at the time of object creation. It is used to initialize the data members of new object generally. The constructor in C++ has the same name as class or structure. Constructors are special class functions which performs initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Whereas, Destructor on the other hand is used to destroy the class object. • Default Constructor: A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.
Complex number constructor. Constructs a complex object. It may be constructed from two values (re and im) or from another complex. We can create complex number class in C++, that can hold the real and imaginary part of the complex number as member elements. There will be some member functions that are used to handle this class.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
The cout is a predefined object of ostream class. It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console. On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. The "c" in cout refers to "character" and "out" means "output". Hence cout means "character output". The cout object is used along with the insertion operator << in order to display a stream of characters.
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.
"Operator Precedence" determines how an expression is evaluated. Some operators will have higher precedence than others. In this example, "Multiplication Operator" will have
A 'stack' is a basic "data structure" and can be defined in an 'Abstract', implementation-free manner, or it can be "Generally Defined" as a Linear List of items in which all additions and
In computer science, a "self-balancing" binary search tree is any "node-based" binary search tree that automatically keeps its height small in the face of arbitrary item insertions & item
Genereate first "Hash" and genereate second hash. Initialize table and "Find" element from the table. Function to insert element into the table. Function to rehash the table. Retrieve
Program to generate random numbers using Probability Distribution Function. Probability distribution is based on "Probability Density" function. a probability density function (pdf),