c++-5
c++-5
Out put
rollno 1: 101
rollno 2: 102
rollno 3: 103
rollno 4: 104
rollno 5: 105
2-D ARRAYS
As we have seen the example of one- dimensional array, we also have seen
the how to initialized the data item / elements to one – dimensional array.
The array that's contain numbers of rows and columns like a the matrix i.e.
the array which contain the two square brackets and mentioned the size of an
arrays is called as a 2-D arrays or 2 dimensional arrays .
Declaration of 2-D arrays:
Datatypes variablename[dimension/size][dimension/size]
o int anArray[3][5]; // a 3-element array of 5-element arrays or three rows
and 5 columns In this case, since we have 2 subscripts or index , this is
called as a two dimensional array.
o In a two-dimensional array:-
the first subscript or index consider as an row, and
the 2nd subscript or index considers as an column.
o the above two-dimensional array is laid out as follows:
#include <iostream>
int main() {
const int numRows = 3;
const int numCols = 4;
// Initialize a 2D array with 3 rows and 4 columns
int matrix[numRows][numCols] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Access and print elements of the 2D array
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) { out put
std::cout << matrix[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
2-D ARRAYS con’t
Explanation:
Array Declaration - In this example, `matrix` is a two-dimensional array with 3
rows and 4 columns.
Initialization:
- The array is initialized using an initializer list with values for each row and column.
- In the example, the matrix is initialized with values `{1, 2, 3, 4}`, `{5, 6, 7, 8}`, and
`{9, 10, 11, 12}`.
Accessing Elements:
- Elements in a 2D array are accessed using two indices: one for the row and one for
the column.
- To access an element at row `i` and column `j`, you use `matrix[i][j]`.
Printing the Array - The nested `for` loops iterate over each element of the 2D
array and print them row by row.
- The outer loop iterates over rows (`i`), and the inner loop iterates over columns
(`j`).
Output - The program will output the elements of the 2D array in a tabular
format, with each row printed on a new line.
Two-dimensional arrays are useful for representing matrices, tables, grids, and other
structured data where elements are logically organized in rows and columns. They
provide a way to store and manipulate data in a structured manner, making it easier to
2-D ARRAYS con’t example
#include<iostream>
#define nNumRows 5
#define nNumCols 5
int main()
{
int nProduct[nNumRows ][nNumCols ] = { 0};
// Calculate a multiplication table
for (int nRow = 0; nRow < nNumRows; nRow++)
for (int nCol = 0; nCol < nNumCols; nCol++)
nProduct[nRow][nCol] = nRow * nCol;
#include<iostream>
#define nNumRows 5
#define nNumCols 5
int main()
{
int nProduct[nNumRows ][nNumCols ] = { 0};
// Calculate a multiplication table
for (int nRow = 0; nRow < nNumRows; nRow++)
for (int nCol = 0; nCol < nNumCols; nCol++)
nProduct[nRow][nCol] = nRow * nCol;
ptr = #
pointer = #
ptrToPtr = &pointer;
std::cout << "The value of num is: " << num << "\n";
std::cout << "The value of pointer variable is: " << *ptr << "\n";
std::cout << "The value of pointer to pointer is: " << **ptrToPtr << "\n";
return 0;
}
Out put
String
Introduction to Strings:
A string is a sequence of characters.
Earlier versions of C++ lacked built-in types for strings, making string
manipulation a tedious task involving character arrays.
Introduction of the String Class:**
- The ANSI standard introduced a new class called `string` in C++.
- To use the `string` class, the program must include the `<string>`
header file.
- The `string` class in C++ is comprehensive, offering a rich set of
constructors, member functions, and operators.
Commonly Used Constructors:
- Three commonly used constructors of the `string` class:
- string(); :- Creates an empty string.
- string(const char *str); :-Creates a string from a null-terminated
character array.
- string(const string &str); :- Creates a string from another string
object.
Accessing Strings:
- Initialization of strings using user input via `cin` command has
limitations.
- The string length should not exceed the array size as C++ compiler
does not check bounds on character arrays.
- `cin` cannot handle multi-word strings due to space being considered a
String
return 0;
} out put
String