0% found this document useful (0 votes)
57 views

Arrays InCplusplus

Arrays in C++ can be one-dimensional or two-dimensional. One-dimensional arrays store elements in a single variable using subscripts to access individual elements. Two-dimensional arrays are similar to matrices with rows and columns accessed using row and column subscripts. For loops are commonly used to iterate through array elements to perform operations on each one.

Uploaded by

seshavps
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Arrays InCplusplus

Arrays in C++ can be one-dimensional or two-dimensional. One-dimensional arrays store elements in a single variable using subscripts to access individual elements. Two-dimensional arrays are similar to matrices with rows and columns accessed using row and column subscripts. For loops are commonly used to iterate through array elements to perform operations on each one.

Uploaded by

seshavps
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Arrays in C++

Array
Just as in Matlab, a collection of related data is called an array Arranged in rows and/or columns Stored in a single variable

One Dimensional Array

One variable that consists of n elements One-dimensional arrays in C++ are similar to the Matlab row array Note that element numbers start at 0

Array Declarations
Array variables must be declared in C++ Examples
int temps[4]; creates an array called temps that holds four integers float nums[20]; creates an array called nums that holds 20 floating point numbers

Array Initialization
Arrays can be initialized as they are declared
int temps[5] = {82, 78, 91, 85, 73};

Do not have to specify array size if initial values are used. Array automatically created large enough to hold initial values
int temps[ ] = {82, 78, 91, 85, 73};

Subscripts
Individual array elements accessed by using subscripts Subscript is the element number Subscript must be an integer within the range of the array In C++, subscripts always start at 0

Examples

x = temps [0]; y = temps [2]; temps[3] = 23;

// x is 82 // y is 91 //changes content of last // element from 87 to 23

Warning
C++ will not flag an error if the array subscript goes out of bounds Ex: int A[3]; // declare A with 3 elements A[5] = 2; // subscript out of range This is perfectly legal, but will lead to unpredictable behavior Beware!

Arrays and For Loops


C++ is a general purpose programming language that has not be optimized for mathematics as Matlab has Cannot do array manipulation as easily Almost always need to use a for loop to step through the array elements

Examples
//Print out the contents of array A, which has //5 elements for (int i = 0; i < 5; i++) { cout << A[i] << endl; }

Examples
//Set all 100 elements of array B to 0 for (int j = 0; j < 100; j++) { B[j] = 0; } //Copy array B to array C for (int k = 0; k < 100; k++) { C[k] = B[k]; }

Example
Generate (x, y) points for the equation y = mx + b x should vary between -10 and +10 m and b should be chosen by the user

Algorithm
Prompt for and read in m (the slope) and b (the offset) Create an array X containing values between -10 and +10 For each element in X, calculate the corresponding Y using the formula Y = mX + b

#include <iostream> using namespace std; int main() { int X[21], Y[21]; int m, b, x_val; // prompt for and read in slope and offset cout << "Please enter the slope: "; cin >> m; cout << "Please enter the offset: "; cin >> b;

//x values start at -10 and go to +10 x_val = -10; // Loop creates X[i] and Y[i] and prints out x,y pair for (int i = 0; i < 21; i++) { X[i] = x_val; x_val++; Y[i] = (X[i] * m) + b; cout << "(" << X[i] << ", " << Y[i] << ")" << endl; } return 0; }

Two-Dimensional Arrays
Just as in Matlab, C++ supports two dimensional arrays A two dimensional array has rows and columns, like a table Sometimes called a matrix

Two Dimensional Array Declarations


Similar to one-dimensional arrays Examples
int temps[4][4]; creates an array called temps with 4 rows and 4 columns (holds 16 integers) float nums[20][2]; creates an array called nums with 20 rows and 2 columns (holds 40 floating point numbers)

Two Dimensional Array Initialization


Arrays can be initialized as they are declared
int temps[2][2] = {{82, 78}, {91, 85}}; Values for each row held in { }

Subscripts
Individual array elements accessed by using subscripts Subscript is row number, column number Both parts of the subscript must be integers within the range of the array Subscripts start at 0

Examples
A is 1 1 1 222 333 m = A[0,0]; n = A[2, 1];

// m is 1 // n is 3

Examples, con't
A[1, 1] = 5; A is 1 1 1 252 333 A is 1 1 8 252 333

A[0, 2] = 8;

Nested For Loops


Use nested for loops to step through two dimensional arrays Usual format is:
for each row for each column do something to array[row][column]

Example
// Initialize array to all 1's int ex[3][2]; for (int row = 0; row < 3; row++){ for (int col = 0; col < 2; col++) { ex[row][col] = 1; } }

Example
// Display contents of an array int ex[3][2] = {{1,1}, {2,2}, {3,3}}; for ( int row = 0; row < 3; row++){ for ( int col = 0; col < 2; col++) { cout << ex[row][col] << " "; } cout << endl; // new line at end of each row }

You might also like