Review Part II
Starting New Lines in Output
To start a new output line, you can include \n in a quoted string, as in the
following example:
cout << "You have definitely won\n"
<< "one of the following prizes:\n";
Recall that \n is typed as two symbols with no space in-between the two
symbols.
Alternatively, you can start a new line by outputting endl. An
equivalent way to write the above cout statement is as follows:
cout << "You have definitely won" << endl
<< "one of the following prizes:" << endl;
Outputting Values of Type double
If you insert the following “magic formula” in your program, then all
numbers of type double (or any other type that allows for digits after the
decimal point) will be output in ordinary everyday notation with two digits
after the decimal point:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
You can use any other nonnegative whole number in place of the 2 to
specify a different number of digits after the decimal point. You can even
use a variable of type int in place of the 2
cin Statements
A cin statement sets variables equal to values typed in at the keyboard.
SYNTAX cin >> Variable_1 >> Variable_2 >> . . . ;
EXAMPLE cin >> number >> size;
cin >> time_to_go
>> points_needed;
The “and” Operator &&
You can form a more elaborate Boolean expression by combining two
simple tests using the “and” operator &&.
SYNTAX (FOR A BOOLEAN EXPRESSION USING &&)
(Comparison_1) && (Comparison_2)
EXAMPLE
(WITHIN AN if-else STATEMENT)
if ( (score > 0) && (score < 10) )
cout << "score is between 0 and 10\n";
else
cout << "score is not between 0 and 10.\n";
If the value of score is greater than 0 and the value of score is also less
than 10, then the first cout statement will be executed; otherwise, the
second cout statement will be executed.
The “or” Operator ||
You can form a more elaborate Boolean expression by combining two
simple tests using the “or” operator ||.
SYNTAX (FOR A BOOLEAN EXPRESSION USING ||)
(Comparison_1) || (Comparison_2)
EXAMPLE
(WITHIN AN if-else STATEMENT)
if ( (x == 1) || (x == y) )
cout << "x is 1 or x equals y.\n";
else
cout << "x is neither 1 nor equal to y.\n";
If the value of x is equal to 1 or the value of x is equal to the value of y (or
both), then the first cout statement will be executed; otherwise, the
second cout statement will be executed.
Navid Plus is a Tech blog focusing on Science material that I learn day through my College and job. Most likely, blog posts contain C++ Programming, Security, Networking, and Math. Let's have some fun.
Wednesday, March 22, 2017
C++ Part 38
Review Part I
Identifiers
Identifiers are used as names for variables and other items in a C++
program. An identifier must start with either a letter or the underscore
symbol, and the remaining characters must all be letters, digits, or the
underscore symbol.
Variable Declarations
All variables must be declared before they are used. The syntax for variable
declarations is as follows:
SYNTAX Type_Name Variable_Name_1, Variable_Name_2, . . .;
EXAMPLES:
int count, number_of_dragons, number_of_trolls;
double distance;
Syntax
The syntax for a programming language (or any other kind of language) is
the set of grammar rules for that language. For example, when we talk
about the syntax for a variable declaration (as in the box labeled “Variable
Declarations”), we are talking about the rules for writing down a well-formed
variable declaration. If you follow all the syntax rules for C++, then
the compiler will accept your program. Of course, this only guarantees that
what you write is legal. It guarantees that your program will do something,
but it does not guarantee that your program will do what you want it to do.
Assignment Statements
In an assignment statement, first the expression on the right-hand side of
the equal sign is evaluated, and then the variable on the left-hand side of
the equal sign is set equal to this value.
SYNTAX Variable = Expression;
EXAMPLES distance = rate * time;
count = count + 2;
Initializing Variables in Declarations
You can initialize a variable (that is, give it a value) at the time that you
declare the variable.
SYNTAX
Type_Name Variable_Name_1 = Expression_ for_Value_1,
Variable_Name_2 = Expresssion_ for_Value_2, . . .;
EXAMPLES
int count = 0, limit = 10, fudge_factor = 2;
double distance = 999.99;
Alternative Syntax for Initializing in Declarations
Type_Name Variable_Name_1 (Expression_ for_Value_1),
Variable_Name_2 (Expression_ for_Value_2), . . .;
Identifiers
Identifiers are used as names for variables and other items in a C++
program. An identifier must start with either a letter or the underscore
symbol, and the remaining characters must all be letters, digits, or the
underscore symbol.
Variable Declarations
All variables must be declared before they are used. The syntax for variable
declarations is as follows:
SYNTAX Type_Name Variable_Name_1, Variable_Name_2, . . .;
EXAMPLES:
int count, number_of_dragons, number_of_trolls;
double distance;
Syntax
The syntax for a programming language (or any other kind of language) is
the set of grammar rules for that language. For example, when we talk
about the syntax for a variable declaration (as in the box labeled “Variable
Declarations”), we are talking about the rules for writing down a well-formed
variable declaration. If you follow all the syntax rules for C++, then
the compiler will accept your program. Of course, this only guarantees that
what you write is legal. It guarantees that your program will do something,
but it does not guarantee that your program will do what you want it to do.
Assignment Statements
In an assignment statement, first the expression on the right-hand side of
the equal sign is evaluated, and then the variable on the left-hand side of
the equal sign is set equal to this value.
SYNTAX Variable = Expression;
EXAMPLES distance = rate * time;
count = count + 2;
Initializing Variables in Declarations
You can initialize a variable (that is, give it a value) at the time that you
declare the variable.
SYNTAX
Type_Name Variable_Name_1 = Expression_ for_Value_1,
Variable_Name_2 = Expresssion_ for_Value_2, . . .;
EXAMPLES
int count = 0, limit = 10, fudge_factor = 2;
double distance = 999.99;
Alternative Syntax for Initializing in Declarations
Type_Name Variable_Name_1 (Expression_ for_Value_1),
Variable_Name_2 (Expression_ for_Value_2), . . .;
C++ Part 37
I/O Stream Example :
//Reads three numbers from the file infile.txt, sums the numbers,
//and writes the sum to the file outfile.txt.
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::endl;
int main( )
{
ifstream inStream;
ofstream outStream;
inStream.open("infile.txt");
outStream.open("outfile.txt");
int first, second, third;
inStream >> first >> second >> third;
outStream << "The sum of the first 3\n"
<< "numbers in infile.txt\n"
<< "is " << (first + second + third)
<< endl;
inStream.close( );
outStream.close( );
return 0;
}
//Reads three numbers from the file infile.txt, sums the numbers,
//and writes the sum to the file outfile.txt.
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::endl;
int main( )
{
ifstream inStream;
ofstream outStream;
inStream.open("infile.txt");
outStream.open("outfile.txt");
int first, second, third;
inStream >> first >> second >> third;
outStream << "The sum of the first 3\n"
<< "numbers in infile.txt\n"
<< "is " << (first + second + third)
<< endl;
inStream.close( );
outStream.close( );
return 0;
}
C++ Part 36
4. Register Storage Class
Register storage assigns a variable's storage in the CPU registers rather than primary memory. It has its lifetime and visibility same as automatic variable. The purpose of creating register variable is to increase access speed and makes program run faster. If there is no space available in register, these variables are stored in main memory and act similar to variables of automatic storage class. So only those variables which requires fast access should be made register.Syntax of Register Storage Class Declaration
register datatype var_name1 [= value];For example,
register int id; register char a;
Example of Storage Class
Example 2: C++ program to create automatic, global, static and register variables.#include<iostream>
using namespace std;
int g; //global variable, initially holds 0
void test_function()
{
static int s; //static variable, initially holds 0
register int r; //register variable
r=5;
s=s+r*2;
cout<<"Inside test_function"<<endl;
cout<<"g = "<<g<<endl;
cout<<"s = "<<s<<endl;
cout<<"r = "<<r<<endl;
}
int main()
{
int a; //automatic variable
g=25;
a=17;
test_function();
cout<<"Inside main"<<endl;
cout<<"a = "<<a<<endl;
cout<<"g = "<<g<<endl;
test_function();
return 0;
}
In the above program, g is a global variable, s is static, r is register and a is automatic variable. We have defined two function, first is main() and another is test_function(). Since g is global variable, it can be used in both function. Variables r and s are declared inside test_function() so can only be used inside that function. However, s being static isn't destroyed until the program ends. When test_function() is called for the first time, r is initialized to 5 and the value of s is 10 which is calculated from the statement,s=s+r*2;After the termination of test_function(), r is destroyed but s still holds 10. When it is called second time, r is created and initialized to 5 again. Now, the value of s becomes 20 since s initially held 10. Variable a is declared inside main() and can only be used inside main().
Output
Inside test_function g = 25 s = 10 r = 5 Inside main a = 17 g = 25 Inside test_function g = 25 s = 20 r = 5
5. Mutable Storage Class
In C++, a class object can be kept constant using keyword const. This doesn't allow the data members of the class object to be modified during program execution. But, there are cases when some data members of this constant object must be changed. For example, during a bank transfer, a money transaction has to be locked such that no information could be changed but even then, its state has be changed from - started to processing to completed. In those cases, we can make these variables modifiable using a mutable storage class.Syntax for Mutable Storage Class Declaration
mutable datatype var_name1;For example,
mutable int x; mutable char y;
Example of Mutable Storage Class
Example 3: C++ program to create mutable variable.#include<iostream>
using namespace std;
class test
{
mutable int a;
int b;
public:
test(int x,int y)
{
a=x;
b=y;
}
void square_a() const
{
a=a*a;
}
void display() const
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
};
int main()
{
const test x(2,3);
cout<<"Initial value"<<endl;
x.display();
x.square_a();
cout<<"Final value"<<endl;
x.display();
return 0;
}
A class test is defined in the program. It consists of a mutable data member a. A constant object x of class test is created and the value of data members are initialized using user-defined constructor. Since, b is a normal data member, its value can't be changed after initialization. However a being mutable, its value can be changed which is done by invoking square_a() method. display() method is used to display the value the data members.Output
Initial value a = 2 b = 3 Final value a = 4
__________________________________Source: www.programtopia.net, submitted by:Sagun Shrestha
C++ Part 35
Typical Program Organization
# include <iostream> //Setup section used by compiler
storage class of a variable defines the lifetime and visibility of a variable.
Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible.
There are five types of storage classes in C++.
They are:
______________________________
This variable is visible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.
Note: Run the main.cpp program
Output
# include <iostream> //Setup section used by compiler
storage class of a variable defines the lifetime and visibility of a variable.
Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible.
There are five types of storage classes in C++.
They are:
______________________________
1. Automatic Storage Class
Automatic storage class assigns a variable to its default storage type. auto keyword is used to declare automatic variables. However, if a variable is declared without any keyword inside a function, it is automatic by default.This variable is visible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.
Syntax of Automatic Storage Class Declaration
datatype var_name1 [= value]; or auto datatype var_name1 [= value];
Example of Automatic Storage Class
auto int x; float y = 5.67;
2. External Storage Class
External storage class assigns variable a reference to a global variable declared outside the given program. extern keyword is used to declare external variables. They are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared. This visible to all the functions present in the program.Syntax of External Storage Class Declaration
extern datatype var_name1;For example,
extern float var1;
Example of External Storage Class
Example 1: C++ program to create and use external storage.File: sub.cpp
int test=100; // assigning value to test
void multiply(int n)
{
test=test*n;
}
File: main.cpp
#include<iostream>
#include "sub.cpp" // includes the content of sub.cpp
using namespace std;
extern int test; // declaring test
int main()
{
cout<<test<<endl;
multiply(5);
cout<<test<<endl;
return 0;
}
A variable test is declared as external in main.cpp. It is a global variable and it is assigned to 100 in sub.cpp. It can be accessed in both files. The function multiply() multiplies the value of test with the parameter passed to it while invoking it. The program performs the multiplication and changes the global variable test to 500.Note: Run the main.cpp program
Output
100 500
3. Static Storage Class
Static storage class ensures a variable has the visibility mode of a local variable but lifetime of an external variable. It can be used only within the function where it is declared but destroyed only after the program execution has finished. When a function is called, the variable defined as static inside the function retains its previous value and operates on it. This is mostly used to save values in a recursive function.Syntax of Static Storage Class Declaration
static datatype var_name1 [= value];For example,
static int x = 101; static float sum;
_________
source: www.programtopia.netTuesday, March 21, 2017
C++ Part 34
The call by reference method of passing arguments to a
function copies the reference of an argument into the formal parameter.
Inside the function, the reference is used to access the actual argument
used in the call. This means that changes made to the parameter affect
the passed argument.
To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.
To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.
// function definition to swap the values. void swap(int &x, int &y) { int temp; temp = x; /* save the value at address x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; }For now, let us call the function swap() by passing values by reference as in the following example:
#include <iostream> using namespace std; // function declaration void swap(int &x, int &y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values using variable reference.*/ swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }When the above code is put together in a file, compiled and executed, it produces the following result:
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100
Sunday, March 19, 2017
C++ Part 33
ASCII - Binary Character Table
Letter
|
ASCII Code
|
Binary
|
Letter
|
ASCII Code
|
Binary
|
a
|
097
|
01100001
|
A
|
065
|
01000001
|
b
|
098
|
01100010
|
B
|
066
|
01000010
|
c
|
099
|
01100011
|
C
|
067
|
01000011
|
d
|
100
|
01100100
|
D
|
068
|
01000100
|
e
|
101
|
01100101
|
E
|
069
|
01000101
|
f
|
102
|
01100110
|
F
|
070
|
01000110
|
g
|
103
|
01100111
|
G
|
071
|
01000111
|
h
|
104
|
01101000
|
H
|
072
|
01001000
|
i
|
105
|
01101001
|
I
|
073
|
01001001
|
j
|
106
|
01101010
|
J
|
074
|
01001010
|
k
|
107
|
01101011
|
K
|
075
|
01001011
|
l
|
108
|
01101100
|
L
|
076
|
01001100
|
m
|
109
|
01101101
|
M
|
077
|
01001101
|
n
|
110
|
01101110
|
N
|
078
|
01001110
|
o
|
111
|
01101111
|
O
|
079
|
01001111
|
p
|
112
|
01110000
|
P
|
080
|
01010000
|
q
|
113
|
01110001
|
Q
|
081
|
01010001
|
r
|
114
|
01110010
|
R
|
082
|
01010010
|
s
|
115
|
01110011
|
S
|
083
|
01010011
|
t
|
116
|
01110100
|
T
|
084
|
01010100
|
u
|
117
|
01110101
|
U
|
085
|
01010101
|
v
|
118
|
01110110
|
V
|
086
|
01010110
|
w
|
119
|
01110111
|
W
|
087
|
01010111
|
x
|
120
|
01111000
|
X
|
088
|
01011000
|
y
|
121
|
01111001
|
Y
|
089
|
01011001
|
z
|
122
|
01111010
|
Z
|
090
|
01011010
|
Subscribe to:
Posts (Atom)
Digital Design Part 3
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...
-
The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the...
-
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...