C++ Programming Code Examples
C++ > Computer Graphics Code Examples
Quick Sort Program with Text Graphics
/* Quick Sort Program with Text Graphics */
#include <iostream.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#define MAX 15
#define ValueOf( x ) ( x.value() )
#define Exchange( x , y ) ( x.exchange(y) )
class element
{
int _value;
int _color;
public:
element() { _color = 15; }
void get()
{
scanf ( "%d", &_value );
}
int value (){ return _value;}
void exchange ( element &e )
{
element temp;
temp = e;
e = *this;
*this = temp;
}
void setcolor ( int col ) { _color = col; }
void show()
{
textbackground ( _color );
if ( _color == 15 )
{
textcolor ( 0 );
}
else
{
textcolor ( 15 );
}
cprintf ( " %d " , _value );
printf ( " " );
}
};
/*
int element :: value ()
{
return _value;
}
*/
void QuickSort ( element * , int , int );
int partition ( element * , int , int );
void Display ( element *A , int p , int r );
void main()
{
element array[MAX];
int i = 1;
textbackground ( 0 );
textcolor ( 15 );
clrscr ();
printf ( "
Enter %d elements:-
> ", MAX - 1 );
for ( i = 1; i < MAX; i++ )
array[i].get();
printf ( "
" );
for ( i = 1; i < MAX; i++ )
array[i].show();
getch();
printf ( "
" );
QuickSort ( array , 1 , MAX - 1 );
printf ( "
" );
for ( i = 1; i < MAX; i++ )
array[i].show();
getch();
}
void QuickSort ( element *A , int p , int r )
{
int q;
if ( p < r )
{
q = partition ( A , p , r );
QuickSort ( A , 1 , q - 1 );
QuickSort ( A , q + 1 , r );
}
}
int partition ( element *A , int p , int r )
{
int key , i = 1 , j = 1;
key = ValueOf ( A[r] );
A[r].setcolor ( RED );
i = p - 1;
for ( j = p ; j <= r; j++ )
{
if ( ValueOf ( A[j] ) <= key )
{
i = i + 1;
Exchange ( A[j] , A[i] );
}
else
{
// A[j].setcolor ( BLUE );
}
Display ( A , 1 , MAX );
delay ( 100 );
}
A[i].setcolor ( GREEN );
Display ( A , 1 , MAX );
printf ( "
> %d at correct position. ", ValueOf ( A[i] ) );
return i ;
}
void Display ( element *A , int p , int r )
{
if ( wherey () > 23 )
{
getch();
textbackground ( 0 );
textcolor ( 15 );
clrscr();
}
printf ( "
" );
for ( int i = p; i < r; i++ )
{
A[i].show();
}
}
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.
Partition range in two. Rearranges the elements from the range [first,last), in such a way that all the elements for which pred returns true precede all those for which it returns false. The iterator returned points to the first element of the second group. The relative ordering within each group is not necessarily the same as before the call. See stable_partition for a function with a similar behavior but with stable ordering within each group. The function shall not modify its argument. This can either be a function pointer or a function object. Function returns an iterator that points to the first element of the second group of elements (those for which pred returns false), or last if this group is empty.
An array is defined as the collection of similar type of data items stored at contiguous memory locations. Arrays are the derived data type in C++ programming language which can store the primitive type of data such as int, char, double, float, etc. It also has the capability to store the collection of derived data types, such as pointers, structure, etc. The array is the simplest data structure where each data element can be randomly accessed by using its index number. C++ array is beneficial if you have to store similar elements. For example, if we want to store the marks of a student in 6 subjects, then we don't need to define different variables for the marks in the different subject. Instead of that, we can define an array which can store the marks in each subject at the contiguous memory locations.
Check whether eofbit is set. Returns true if the eofbit error state flag is set for the stream. This flag is set by all standard input operations when the End-of-File is reached in the sequence associated with the stream. Note that the value returned by this function depends on the last operation performed on the stream (and not on the next). Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.
setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.
Function textbackground is used to change current background color in text mode. To use the textbackground() function all you need to do is before printing any text call this function with a parameter defining the color in capital letters. That will be enough to change the background color of the text.
Use the textcolor function to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral. Now, if you want your text to blink then while calling the textcolor() function pass the color and also say BLINK. This will like this: textcolor(BLUE+BLINK).
#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.
The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value. In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type. You should see a pointer as a symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also create and manipulate dynamic data structures. In C++, a pointer variable refers to a variable pointing to a specific address in a memory pointed by another variable.
Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer. In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++: • It can be used to pass current object as a parameter to another method. • It can be used to refer current class instance variable. • It can be used to declare indexers. To understand 'this' pointer, it is important to know how objects look at functions and data members of a class.
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.
Get characters. Extracts characters from the stream, as unformatted input. The get() function is used to read a character(at a time) from a file. The classes istream and ostream define two member functions get(), put() respectively to handle the single character input/output operations. There are two types of get() functions. Both get(char *) and get(void) prototype can be used to fetch a character including the blank space,tab and newline character. The get(char *) version assigns the input character to its argument and the get(void) version returns the input character. Since these functions are members of input/output Stream classes, these must be invoked using appropriate objects.
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.
delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
In the C++ Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions. The syntax for creating a constant using #define in the C++ is: #define token value
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.