Basic types and their use in Python
Number data types store numeric values. Number objects are created when you assign a value to them. For example:
var1 = 1
var2 = 10
var3 = var1 + var2
print(var3)
Run code -> 11
Strings in Python are identified as a contiguous set of characters represented in the quotation marks.
str = 'Hello World!'
print(str)
Run code-> Hello World!
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1.
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
print(list)
print(list[0])
Run code:
['abcd', 786, 2.23, 'john', 70.200000000000003]
abcd
A tuple consists of a number of values separated by commas. The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
print(tuple)
print(tuple[0])
Run code:
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
They work like associative arrays or hashes found in Perl and consist of key-value pairs. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
print dict['one']
print dict[2]
Run code:
This is one
This is two
Continue reading ]]>
Basic types and their use in Python
Number data types store numeric values. Number objects are created when you assign a value to them. For example:
var1 = 1
var2 = 10
var3 = var1 + var2
print(var3)
Run code -> 11
Strings in Python are identified as a contiguous set of characters represented in the quotation marks.
str = ‘Hello World!’
print(str)
Run code-> Hello World!
Lists are the most versatile of Python’s compound data types. A list contains items separated by commas and enclosed within square brackets ([]). The values stored in a list can be accessed using the slice operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list and working their way to end -1.
list = [ ‘abcd’, 786 , 2.23, ‘john’, 70.2 ]
print(list)
print(list[0])
Run code:
[‘abcd’, 786, 2.23, ‘john’, 70.200000000000003]
abcd
A tuple consists of a number of values separated by commas. The main differences between lists and tuples are: Lists are enclosed in brackets ( [ ] ) and their elements and size can be changed, while tuples are enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
tuple = ( ‘abcd’, 786 , 2.23, ‘john’, 70.2 )
print(tuple)
print(tuple[0])
Run code:
(‘abcd’, 786, 2.23, ‘john’, 70.200000000000003)
abcd
They work like associative arrays or hashes found in Perl and consist of key-value pairs. Dictionaries are enclosed by curly braces ({ }) and values can be assigned and accessed using square braces ([]).
dict = {}
dict[‘one’] = “This is one”
dict[2] = “This is two”
print dict[‘one’]
print dict[2]
Run code:
This is one
This is two
]]>
Creation and use of vectors in C++ #TC1017 #Mastery23
Vector is a template class that is a perfect replacement for the good old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help operating consistently on the contained objects.
The first step using vector is to include the appropriate header:
Here is my link
https://www.dropbox.com/s/m0popqb0mg8qbxo/Mastery23.mov?dl=0
Is a short video explaining a really easy vector :)
Here is other link about vectors :D:
Continue reading ]]>Creation and use of vectors in C++ #TC1017 #Mastery23
Vector is a template class that is a perfect replacement for the good old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help operating consistently on the contained objects.
The first step using vector is to include the appropriate header:
Here is my link
https:/
Is a short video explaining a really easy vector
Here is other link about vectors :D:
]]>
Reading and writing of files in C++
So far, we have been using the iostream standard library, which provides cin and coutmethods for reading from standard input and writing to standard output respectively.
This tutorial will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types:
Data Type |
Description |
ofstream |
This data type represents the output file stream and is used to create files and to write information to files. |
ifstream |
This data type represents the input file stream and is used to read information from files. |
fstream |
This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files. |
To perform file processing in C++, header files and must be included in your C++ source file.
Opening a File:
A file must be opened before you can read from it or write to it. Either the ofstream orfstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;">
void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Mode Flag |
Description |
ios::app |
Append mode. All output to that file to be appended to the end. |
ios::ate |
Open a file for output and move the read/write control to the end of the file. |
ios::in |
Open a file for reading. |
ios::out |
Open a file for writing. |
ios::trunc |
If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;">
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;">
fstream afile;
afile.open("file.dat", ios::out | ios::in );
Closing a File
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;">
void close();
Writing to a File:
While doing C++ programming, you write information to a file from your program using the stream insertion operator (
Reading from a File:
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Continue reading ]]>
Reading and writing of files in C++
So far, we have been using the iostream standard library, which provides cin and coutmethods for reading from standard input and writing to standard output respectively.
This tutorial will teach you how to read and write from a file. This requires another standard C++ library called fstream, which defines three new data types:
Data Type |
Description |
ofstream |
This data type represents the output file stream and is used to create files and to write information to files. |
ifstream |
This data type represents the input file stream and is used to read information from files. |
fstream |
This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files. |
To perform file processing in C++, header files
Opening a File:
A file must be opened before you can read from it or write to it. Either the ofstream orfstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;”>
void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Mode Flag |
Description |
ios::app |
Append mode. All output to that file to be appended to the end. |
ios::ate |
Open a file for output and move the read/write control to the end of the file. |
ios::in |
Open a file for reading. |
ios::out |
Open a file for writing. |
ios::trunc |
If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;”>
ofstream outfile;
outfile.open(“file.dat”, ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;”>
fstream afile;
afile.open(“file.dat”, ios::out | ios::in );
Closing a File
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;”>
void close();
Writing to a File:
While doing C++ programming, you write information to a file from your program using the stream insertion operator (
Reading from a File:
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
]]>
Creation and use of vectors in C++
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;">
type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;">
double balance[10];
Creation and use of vectors in C++
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Declaring Arrays:
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;”>
type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:
#D6D6D6 1.0pt; mso-border-alt: solid #D6D6D6 .75pt; padding: 4.0pt 4.0pt 4.0pt 4.0pt; background: #EEEEEE;”>
double balance[10];
Creation and use of vectors in C++
Vector is a template class that is a perfect replacement for the good old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help operating consistently on the contained objects.The first step using vector is to include the appropriate header:
Note that the header file name does not have any extension; this is true for all of the Standard Library header files. The second thing to know is that all of the Standard Library lives in the namespace std. This means that you have to resolve the names by prepending std:: to them:
For small projects, you can bring the entire namespace std into scope by inserting a using directive on top of your cpp file:
This is okay for small projects, as long as you write the using directive in your cpp file. Never write a using directive into a header file! This would bloat the entire namespace std into each and every cpp file that includes that header. For larger projects, it is better to explicitly qualify every name accordingly. I am not a fan of such shortcuts. In this article, I will qualify each name accordingly. I will introduce some typedefs in the examples where appropriate—for better readability.
Continue reading ]]>Creation and use of vectors in C++
Vector is a template class that is a perfect replacement for the good old C-style arrays. It allows the same natural syntax that is used with plain arrays but offers a series of services that free the C++ programmer from taking care of the allocated memory and help operating consistently on the contained objects.The first step using vector is to include the appropriate header:
Note that the header file name does not have any extension; this is true for all of the Standard Library header files. The second thing to know is that all of the Standard Library lives in the namespace std. This means that you have to resolve the names by prepending std:: to them:
For small projects, you can bring the entire namespace std into scope by inserting a using directive on top of your cpp file:
This is okay for small projects, as long as you write the using directive in your cpp file. Never write a using directive into a header file! This would bloat the entire namespace std into each and every cpp file that includes that header. For larger projects, it is better to explicitly qualify every name accordingly. I am not a fan of such shortcuts. In this article, I will qualify each name accordingly. I will introduce some typedefs in the examples where appropriate—for better readability.
]]>Reading and writing of files in C++
A file must be opened before you can read from it or write to it. Either the ofstream or fstreamobject may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Mode Flag | Description |
---|---|
ios::app | Append mode. All output to that file to be appended to the end. |
ios::ate | Open a file for output and move the read/write control to the end of the file. |
ios::in | Open a file for reading. |
ios::out | Open a file for writing. |
ios::trunc | If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:
#d6d6d6;">ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
#d6d6d6;">fstream afile; afile.open("file.dat", ios::out | ios::in );
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void close();
While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream or fstream object instead of the cout object.
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:
#d6d6d6;">#include #include using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout "Writing to the file" endl; cout "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile data endl; cout "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile data endl; // close the opened file. outfile.close(); // open a file in read mode. ifstream infile; infile.open("afile.dat"); cout "Reading from the file" endl; infile >> data; // write the data at the screen. cout data endl; // again read the data from the file and display it. infile >> data; cout data endl; // close the opened file. infile.close(); return 0; }
When the above code is compiled and executed, it produces the following sample input and output:
#d6d6d6;">$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9
Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.
Credits:
http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm
Continue reading ]]>Reading and writing of files in C++
A file must be opened before you can read from it or write to it. Either the ofstream or fstreamobject may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.
Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
Mode Flag | Description |
---|---|
ios::app | Append mode. All output to that file to be appended to the end. |
ios::ate | Open a file for output and move the read/write control to the end of the file. |
ios::in | Open a file for reading. |
ios::out | Open a file for writing. |
ios::trunc | If the file already exists, its contents will be truncated before opening the file. |
You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:
#d6d6d6;">ofstream outfile; outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
#d6d6d6;">fstream afile; afile.open("file.dat", ios::out | ios::in );
When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
#d6d6d6;">void close();
While doing C++ programming, you write information to a file from your program using the stream insertion operator (ofstream or fstream object instead of the cout object.
You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:
#d6d6d6;">#include#include using namespace std; int main () { char data[100]; // open a file in write mode. ofstream outfile; outfile.open("afile.dat"); cout "Writing to the file" endl; cout "Enter your name: "; cin.getline(data, 100); // write inputted data into the file. outfile data endl; cout "Enter your age: "; cin >> data; cin.ignore(); // again write inputted data into the file. outfile data endl; // close the opened file. outfile.close(); // open a file in read mode. ifstream infile; infile.open("afile.dat"); cout "Reading from the file" endl; infile >> data; // write the data at the screen. cout data endl; // again read the data from the file and display it. infile >> data; cout data endl; // close the opened file. infile.close(); return 0; }
When the above code is compiled and executed, it produces the following sample input and output:
#d6d6d6;">$./a.out Writing to the file Enter your name: Zara Enter your age: 9 Reading from the file Zara 9
Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.
Credits:
http:/
Creation and use of matrixes in C++ (multi - dimensional arrays)
The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y, you would write something as follows:
#d6d6d6;">type arrayName [ x ][ y ];
Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.
A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below:
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns.
#d6d6d6;">int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ };
The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example:
#d6d6d6;">int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:
#d6d6d6;">int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array. You can verify it in the above digram.
#d6d6d6;">#include using namespace std; int main () { // an array with 5 rows and 2 columns. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; // output each array element's value for ( int i = 0; i 5; i++ ) for ( int j = 0; j 2; j++ ) { cout "a[" i "][" j "]: "; cout a[i][j] endl; } return 0; }
When the above code is compiled and executed, it produces the following result:
#d6d6d6;">a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8
As explained above, you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions.
Credits:
http://www.tutorialspoint.com/cplusplus/cpp_multi_dimensional_arrays.htm
Continue reading ]]>Creation and use of matrixes in C++ (multi – dimensional arrays)
The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size x,y, you would write something as follows:
#d6d6d6;">type arrayName [ x ][ y ];
Where type can be any valid C++ data type and arrayName will be a valid C++ identifier.
A two-dimensional array can be think as a table, which will have x number of rows and y number of columns. A 2-dimensional array a, which contains three rows and four columns can be shown as below:
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that uniquely identify each element in a.
Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns.
#d6d6d6;">int a[3][4] = { {0, 1, 2, 3} , /* initializers for row indexed by 0 */ {4, 5, 6, 7} , /* initializers for row indexed by 1 */ {8, 9, 10, 11} /* initializers for row indexed by 2 */ };
The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to previous example:
#d6d6d6;">int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:
#d6d6d6;">int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array. You can verify it in the above digram.
#d6d6d6;">#includeusing namespace std; int main () { // an array with 5 rows and 2 columns. int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; // output each array element's value for ( int i = 0; i 5; i++ ) for ( int j = 0; j 2; j++ ) { cout "a[" i "][" j "]: "; cout a[i][j] endl; } return 0; }
When the above code is compiled and executed, it produces the following result:
#d6d6d6;">a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8
As explained above, you can have arrays with any number of dimensions, although it is likely that most of the arrays you create will be of one or two dimensions.
Credits:
http:/
Creation and use of strings in C++
C++ provides following two types of string representations:
The C-style character string.
The string class type introduced with Standard C++.
The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard Library but for now let us check following example:
At this point, you may not understand this example because so far we have not discussed Classes and Objects. So can have a look and proceed until you have understanding on Object Oriented Concepts.
#d6d6d6;">#include #include using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout "str3 : " str3 endl; // concatenates str1 and str2 str3 = str1 + str2; cout "str1 + str2 : " str3 endl; // total lenghth of str3 after concatenation len = str3.size(); cout "str3.size() : " len endl; return 0; }
When the above code is compiled and executed, it produces result something as follows:
#d6d6d6;">str3 : Hello str1 + str2 : HelloWorld str3.size() : 10
Credits:
Continue reading ]]>Creation and use of strings in C++
C++ provides following two types of string representations:
The C-style character string.
The string class type introduced with Standard C++.
The standard C++ library provides a string class type that supports all the operations mentioned above, additionally much more functionality. We will study this class in C++ Standard Library but for now let us check following example:
At this point, you may not understand this example because so far we have not discussed Classes and Objects. So can have a look and proceed until you have understanding on Object Oriented Concepts.
#d6d6d6;">#include#include using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; int len ; // copy str1 into str3 str3 = str1; cout "str3 : " str3 endl; // concatenates str1 and str2 str3 = str1 + str2; cout "str1 + str2 : " str3 endl; // total lenghth of str3 after concatenation len = str3.size(); cout "str3.size() : " len endl; return 0; }
When the above code is compiled and executed, it produces result something as follows:
#d6d6d6;">str3 : Hello str1 + str2 : HelloWorld str3.size() : 10
Credits:
]]>Creation and use of arrays in C++
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:
#d6d6d6;">type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:
#d6d6d6;">double balance[10];
You can initialize C++ array elements either one by one or using a single statement as follows:
#d6d6d6;">double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:
#d6d6d6;">double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous example.
#d6d6d6;">balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above:
Credits:
http://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
http://www.cplusplus.com/doc/tutorial/arrays/
Continue reading ]]>
Creation and use of arrays in C++
C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
To declare an array in C++, the programmer specifies the type of the elements and the number of elements required by an array as follows:
#d6d6d6;">type arrayName [ arraySize ];
This is called a single-dimension array. The arraySize must be an integer constant greater than zero and type can be any valid C++ data type. For example, to declare a 10-element array called balance of type double, use this statement:
#d6d6d6;">double balance[10];
You can initialize C++ array elements either one by one or using a single statement as follows:
#d6d6d6;">double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
The number of values between braces { } can not be larger than the number of elements that we declare for the array between square brackets [ ]. Following is an example to assign a single element of the array:
If you omit the size of the array, an array just big enough to hold the initialization is created. Therefore, if you write:
#d6d6d6;">double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
You will create exactly the same array as you did in the previous example.
#d6d6d6;">balance[4] = 50.0;
The above statement assigns element number 5th in the array a value of 50.0. Array with 4th index will be 5th, i.e., last element because all arrays have 0 as the index of their first element which is also called base index. Following is the pictorial representaion of the same array we discussed above:
Credits:
http:/
http:/
]]>
Use of loops with "for"
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.
The syntax of a for loop in C++ is:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">for ( init; condition; increment ) { statement(s); }
Here is the flow of control in a for loop:
The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to theincrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-image: url('http://www.tutorialspoint.com/cplusplus/images/try-it.jpg') !important; background-attachment: initial !important; background-color: #eeeeee !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: 100% 0%; background-repeat: no-repeat !important;">#include using namespace std; int main () { // for loop execution for( int a = 10; a 20; a = a + 1 ) { cout "value of a: " a endl; } return 0; }
When the above code is compiled and executed, it produces the following result:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Credits:
Continue reading ]]>Use of loops with “for”
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.
The syntax of a for loop in C++ is:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">for ( init; condition; increment ) { statement(s); }
Here is the flow of control in a for loop:
The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to theincrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-image: url('http://www.tutorialspoint.com/cplusplus/images/try-it.jpg') !important; background-attachment: initial !important; background-color: #eeeeee !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: 100% 0%; background-repeat: no-repeat !important;">#includeusing namespace std; int main () { // for loop execution for( int a = 10; a 20; a = a + 1 ) { cout "value of a: " a endl; } return 0; }
When the above code is compiled and executed, it produces the following result:
#d6d6d6; font-size: 12px; overflow: auto; color: #313131; background-color: #eeeeee;">value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19
Credits:
]]>