C++ Programming Code Examples
C++ > Beginners Lab Assignments Code Examples
Various data encoding techniques
/* Various data encoding techniques */
# include <graphics.h>
# include <iostream.h>
# include <conio.h>
# include <string.h>
# include <dos.h>
int main(void)
{
void drawgrid(int, int, int, int);
void encode(char *,char *, int, int, int, int);
void draw(int ,int ,char *, int);
char *message;
int len;
clrscr();
cout << "
DIGITAL ENCODING TECHNIQUES";
cout << "
Enter the data to be encoded (max 24 bits) : ";
do{
cin >> message;
len = strlen(message);
if(len>24)
cout << "Message is greater than 12 bits, please type new message:
";
}while(len>24);
char *o_mess =new char[len];
char *e_mess =new char[len*2];
strcpy(o_mess,message);
int gd=DETECT, gm, i, x, y;
initgraph(&gd,&gm,"..\BGI");
setcolor(8);
settextstyle(DEFAULT_FONT,0,2);
outtextxy(100,10,"DIGITAL ENCODING TECHNIQUES");
setcolor(YELLOW);
rectangle(0,30,630,450);
rectangle(1,31,629,449);
setcolor(WHITE);
settextstyle(DEFAULT_FONT,0,0);
drawgrid(120,60,len,20);
outtextxy(10,50, "Message");
x= 137;
for(i=0; i<len; i++, x+=20)
{
if(o_mess[i]=='1')
outtextxy(x,50,"1");
else
outtextxy(x,50,"0");
}
x=120;
y=80;
outtextxy(10,100, "NRZ - L");
encode(o_mess,e_mess,len,0,2,0);
draw(x,y,e_mess,len*2);
y+=60;
outtextxy(10,160, "NRZ - I");
encode(o_mess,e_mess,len,3,4,0);
draw(x,y,e_mess,len*2);
y+=60;
outtextxy(10,220, "Bipolar AMI");
encode(o_mess,e_mess,len,3,1,0);
draw(x,y,e_mess,len*2);
y+=60;
outtextxy(10,280, "Pseudo Tenary");
encode(o_mess,e_mess,len,4,3,0);
draw(x,y,e_mess,len*2);
y+=60;
outtextxy(10,340, "Manchester");
encode(o_mess,e_mess,len,0,2,1);
draw(x,y,e_mess,len*2);
y+=60;
outtextxy(10,400, "D-Manchester");
encode(o_mess,e_mess,len,4,3,1);
draw(x,y,e_mess,len*2);
y+=60;
getch();
closegraph();
return 0;
}
void drawgrid(int x, int y, int pulses, int width)
{
line(x,y,x+(pulses*width),y);
line(x,y,x,y+(width*12+150));
for(int i=0; i<pulses; i++)
{
setcolor(LIGHTBLUE);
line(x+width,y+1,x+width,y+(width*12+150));
setcolor(BLUE);
line(x+width/2,y+1,x+width/2,y+(width*12+150));
x+=width;
}
setcolor(WHITE);
return;
}
void draw(int x, int y, char *data, int len)
{
int prev_y, curr_y;
prev_y = y;
for(int i=0; i<len; i++, x+=10)
{
if(data[i]=='2')
curr_y = y;
if(data[i]=='1')
curr_y = y+20;
if(data[i]=='0')
curr_y = y+40;
line(x,curr_y,x+10,curr_y);
sound(70);
delay(50);
nosound();
if(prev_y != curr_y)
line(x,prev_y,x,curr_y);
sound(170);
delay(50);
nosound();
prev_y = curr_y;
delay(10);
}
return;
}
void encode(char *o_mess,char *e_mess,int len,int one,int zero, int
mid)
{
int i, j, previous=2;
for(i=0, j=0; i<len; i++, j+=2)
{
if(o_mess[i]=='1')
{
if(one==0)
e_mess[j]='0';
if(one==1)
e_mess[j]='1';
if(one==2)
e_mess[j]='2';
if(one==3)
{
if(previous==2)
{
previous=0;
e_mess[j]='0';
}
else
{
previous=2;
e_mess[j]='2';
}
}
if(one==4)
{
if(previous==2)
e_mess[j]='2';
else
e_mess[j]='0';
}
}
else
{
if(zero==0)
e_mess[j]='0';
if(zero==1)
e_mess[j]='1';
if(zero==2)
e_mess[j]='2';
if(zero==3)
{
if(previous==2)
{
previous=0;
e_mess[j]='0';
}
else
{
previous=2;
e_mess[j]='2';
}
}
if(zero==4)
{
if(previous==2)
e_mess[j]='2';
else
e_mess[j]='0';
}
}
if(mid==1)
{
if(e_mess[j]=='2')
e_mess[j+1]='0';
else
e_mess[j+1]='2';
if(previous==2)
previous=0;
else
previous=2;
}
else
e_mess[j+1]=e_mess[j];
}
return;
}
Allocate storage space. Default allocation functions (single-object form). A new operator is used to create the object while a delete operator is used to delete the object. When the object is created by using the new operator, then the object will exist until we explicitly use the delete operator to delete the object. Therefore, we can say that the lifetime of the object is not related to the block structure of the 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.
Copy string. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source. strcpy() is a standard library function in C/C++ and is used to copy one string to another. In C it is present in string.h header file and in C++ it is present in cstring header file. It copies the whole string to the destination string. It replaces the whole string instead of appending it. It won't change the source string.
Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.
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.
To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on)
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.
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.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Our system can create various sounds on different frequencies. The sound() is very useful as it can create very nice music with the help of programming and our user can enjoy music during working in out the program. Sound function produces the sound of a specified frequency. Used for adding music to a C++ program.
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.
#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.
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
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.
The header file graphics.h contains closegraph() function which closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph. closegraph() function is used to re-enter in the text mode and exit from the graphics mode. If you want to use both text mode and graphics mode in the program then you have to use both initgraph() and closegraph() function in the program. This function deallocates all memory allocated by graphics system and restores the screen to that mode in which it was presented before you called the initgraph() function.
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.
Get string length. Returns the length of the C string str. C++ strlen() is an inbuilt function that is used to calculate the length of the string. It is a beneficial method to find the length of the string. The strlen() function is defined under the string.h header file. The strlen() takes a null-terminated byte string str as its argument and returns its length. The length does not include a null character. If there is no null character in the string, the behavior of the function is undefined.
outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.
Relational operators for string. Performs the appropriate comparison operation between the string objects lhs and rhs. The functions use string::compare for the comparison. These operators are overloaded in header <string>. If strings are compared using relational operators then, their characters are compared lexicographically according to the current character traits, means it starts comparison character by character starting from the first character until the characters in both strings are equal or a NULL character is encountered.
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.
rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.
The header file graphics.h contains line() function which is used to draw a line from a point(x1, y1) to point(x2, y2) i.e. (x1, y1) and (x2, y2) are end points of the line. The function line() draws a line on the graphics screen between two specified points. So this function requires four parameters namely x1, y1, x2, and y2 to represent two points. This function draws a line from (x1, y1) coordinates to (x2, y2) coordinates on the graphics screen.
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 nosound() function in C language is used to stop the sound played by sound() function. The nosound() function is simply silent the system. The sound() and nosound() functions are very useful as they can create very nice music with the help of programming and our user can enjoy music during working in out the program.
Internal method to test if a positive number is prime. Not efficient algorithm. Using Internal method to return a prime number at least as large as n. Assumes "n > 0". Insert item x into