C++ Programming Code Examples
C++ > Algorithms Code Examples
Fahrenheit-Celsius converter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Fahrenheit-Celsius converter */
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
void c_to_f(void);
void f_to_c(void);
void
main(void)
{
int choice;
char again;
do
{
system("CLS");
cout << setw(10) <<" "<< "What conversion would you like to make?\n"; // menu
cout << setw(20) <<" "<< "1. Celsius to Fahrenheit\n\n"; // make a choice which function to use.
cout << setw(20) <<" "<< "2. Fahrenheit to celsius\n\n";
cin >> choice;
switch(choice) // go to chosen function.
{
case 1 :
{
c_to_f();
break;
}
case 2 :
{
f_to_c();
break;
}
default :
{
cout << setw(10) <<" "<< "you must enter 1 or 2 "<< endl ; // validate and correct input of function choice.
}
}
cout << setw(10) <<" "<< "Do you wish to do another conversion? y for yes, n for no "; // rerun loop on y for yes
cin >> again;
}while (again == 'Y' || again =='y');
}
void c_to_f(void)
{
system("CLS"); // clear screen for function data.
int temp,fahrenheit;
cout << "\n\n\n";
cout << setw(10) <<" "<< "Enter the temperature in whole degrees celsius. \a";
cin >> temp;
fahrenheit = ((temp * 9) / 5) + 32;
cout << endl << setw(10) <<" "<< temp << " degrees celsius is " << fahrenheit << " degrees fahrenheit \a\n\n\n";
}
void f_to_c(void)
{
system("CLS"); // clear screen for function data.
int temp,celsius;
cout << "\n\n\n";
cout << setw(10) <<" "<< "Enter the temperature in whole degrees fahrenheit. \a";
cin >> temp;
celsius = ((temp - 32) * 5) / 9;
cout << endl <<setw(10) <<" "<< temp << " degrees fahrenheit is " << celsius << " degrees celsius \a\n\n\n";
}
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.
Execute system command. Invokes the command processor to execute a command. If command is a null pointer, the function only checks whether a command processor is available through this function, without invoking any command. The effects of invoking a command depend on the system and library implementation, and may cause a program to behave in a non-standard manner or to terminate.
#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.
Set field width. Sets the field width to be used on output operations. The C++ function std::setw behaves as if member width were called with n as argument on the stream on which it is inserted/extracted as a manipulator (it can be inserted/extracted on input streams or output streams). It is used to sets the field width to be used on output operations. This manipulator is declared in header <iomanip>. This method accepts n as a parameter which is the integer argument corresponding to which the field width is to be set. This function returns an object of unspecified type. The setw function should only be used as a stream manipulator.
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block. • The expression can be integer expression or a character expression. • Value-1, 2, n are case labels which are used to identify each case individually. Remember that case labels should not be same as it may create a problem while executing a program. Suppose we have two cases with the same label as '1'. Then while executing the program, the case that appears first will be executed even though you want the program to execute a second case. This creates problems in the program and
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.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
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.
Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:
Program sample takes the values of two large numbers as input and displays the computed value node with node in the resultant Linked List. Result of "subtraction" for two numbers
In C++ language, The mode is the maximum of the "count of occurrence" of the different data element. This algorithm is beneficial for large dataset with high repetition frequency