Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Arrays and multidimensional arrays
Description: It contains info on Arrays,multidimensional arrays etc in C++ programming.It can be studied by 1st year beginner or anyone with decent computer knowledge.
Description: It contains info on Arrays,multidimensional arrays etc in C++ programming.It can be studied by 1st year beginner or anyone with decent computer knowledge.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
The position number within square brackets is called a subscript
...
For example, if a = 5 and b = 6, then the statement
c[5]=5;
// assign value 5 to 6th element of array c
c[ a + b ] += 2; // adds 2 to array element c[11]
...
Defining Arrays
The following definition reserves 12 elements for integer array c, which has subscripts in the range 0–11
int a[ 12 ];
char b[ 12 ];
Like any other variables, uninitialized array elements contain garbage values
...
#include
This type is recommended for any
variable that represents an array’s size or an array’s subscripts
...
h>,
which is often included by other headers (such as
Initializing an Array in a Definition with an Initializer List
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
int n[ 10 ] = { 0 }; // initializes entire array to zeros
int n[ 10 ] = { }; // initializes entire array to zeros
int n[ 10 ] = { 32,27 }; // initializes two elements as 32 and 27, and rest of the array to zeros
int n[] = { 1, 2, 3, 4, 5 }; // would create a five-element array initialized with the indicated values
int n[ 5 ] = { 32, 27, 64, 18, 95, 14 }; //causes a syntax error because there are six initializers and
only five array elements
...
A
symbolic constant is an identifier that’s replaced with replacement text by the C preprocessor before
the program is compiled
...
e
...
The array
element array of the students’ responses
...
h>
#define RESPONSES_SIZE 40 // define array sizes
#define FREQUENCY_SIZE 11
int main( void )
{
size_t answer;
// counter to loop through 40 responses
size_t rating;
// counter to loop through frequencies 1-10
int frequency[ FREQUENCY_SIZE ] = { 0 };
// initialize frequency counters to 0
// place the survey responses in the responses array
int responses[ RESPONSES_SIZE ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6,
5, 7, 6, 8, 6, 7, 5, 6, 6,5, 6, 7, 5, 6, 4, 8, 6, 8, 10 };
for ( answer = 0; answer < RESPONSES_SIZE; ++answer ) {
++frequency[ responses [ answer ] ];
}
// display results
printf( "%s%17s\n", "Rating", "Frequency" ); // output the frequencies in a tabular format
for ( rating = 1; rating < FREQUENCY_SIZE; ++rating ) {
printf( "%6d%17d\n", rating, frequency[ rating ] );
}
}
C has no array bounds checking to prevent the program from referring to an element that does not
exist
...
h>
int main( void )
{
int n[ 10 ];
// n is an array of 10 integers
size_t i;
for ( i = 0; i < 10; ++i ) {
n[ i ] = 0;
}
printf( "%s%13s\n", "Element", "Value" );
for ( i = 0; i < 15; ++i ) {
printf( "%7u%13d\n", i, n[ i ] );
}
// accessing more elements than the array size
}
Static Local Arrays and Automatic Local Arrays
A static local variable exists for the duration of the program but is visible only in the function body
...
This reduces program
execution time, particularly for programs with frequently called functions that contain large arrays
...
If you do not explicitly initialize a static array, that array’s
elements are initialized to zero by default
...
h>
void staticArrayInit( void );
// function prototype
void automaticArrayInit( void );
// function prototype
int main( void )
{
printf( "First call to each function:" );
staticArrayInit();
automaticArrayInit();
printf( "\n\nSecond call to each function:" );
staticArrayInit();
automaticArrayInit();
} // end main
void staticArrayInit( void ) // function to demonstrate a static local array
{
static int array1[ 3 ];
size_t i; // counter
printf( "\nValues on entering staticArrayInit:" );
for ( i = 0; i <= 2; ++i ) {
// function to demonstrate an automatic local array
printf( "array1[ %u ] = %d ", i, array1[ i ] );
}
printf( "\nValues on exiting staticArrayInit:" );
for ( i = 0; i <= 2; ++i ) {
// modify and output contents of array1
printf( "array1[ %u ] = %d ", i, array1[ i ] += 5 );
} // end for
} // end function staticArrayInit
void automaticArrayInit( void ) // function to demonstrate an automatic local array
{
int array2[ 3 ] = { 1, 2, 3 };
// initializes elements each time function is called
size_t i;
printf( "\n\nValues on entering automaticArrayInit:" );
for ( i = 0; i <= 2; ++i ) {
// output contents of array2
printf("array2[ %u ] = %d ", i, array2[ i ] );
}
printf( "\nValues on exiting automaticArrayInit:" );
for ( i = 0; i <= 2; ++i ) {
// modify and output contents of array2
printf( "array2[ %u ] = %d ", i, array2[ i ] += 5 );
}
}
OUTPUT:
First call to each function:
Values on entering staticArrayInit:
array1[ 0 ] = 0 array1[ 1 ] = 0 array1[ 2 ] = 0
Values on exiting staticArrayInit:
array1[ 0 ] = 5 array1[ 1 ] = 5 array1[ 2 ] = 5
Values on entering automaticArrayInit:
array2[ 0 ] = 1 array2[ 1 ] = 2 array2[ 2 ] = 3
Values on exiting automaticArrayInit:
array2[ 0 ] = 6 array2[ 1 ] = 7 array2[ 2 ] = 8
Multidimensional Arrays
represent tables of values consisting of information arranged in rows and columns
...
Tables or arrays that require two subscripts to
identify a particular element are called double-subscripted arrays
...
A two -dimensional array is, essentially, an array of one-dimensional arrays
The array contains three rows and four columns, so it’s said to be a 3-by-4 array
...
#include
For
example, a double-subscripted array int b[2][2] could be defined and initialized with
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
The values in the first set of braces initialize row 0 and the values in the second set of braces initialize row 1
...
Thus,
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
would initialize b[0][0] to 1, b[0][1] to 0, b[1][0] to 3 and b[1][1] to 4
...
// Initializing multidimensional arrays
...
h>
void printArray( int a[][ 3 ] ); // function prototype
// function main begins program execution
int main( void )
{
// initialize array1, array2, array3
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6 } };
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5 };
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
printf( "Values in array1 by row are:" );
printArray( array1 );
printf ( "Values in array2 by row are:" );
printArray( array2 );
printf ( "Values in array3 by row are:" );
printArray( array3 );
} // end main
// function to output array with two rows and three columns
void printArray( int a[][ 3 ] )
{
size_t i; // row counter
size_t j; // column counter
// loop through rows
for ( i = 0; i <= 1; ++i ) {
// output column values
for ( j = 0; j <= 2; ++j ) {
printf( "%d ", a[ i ][ j ] );
} // end inner for
printf( "\n" ); // start new line of output
} // end outer for
} // end function printArray
Values in array1 by row are:
123
456
Values in array2 by row are:
123
450
Values in array3 by row are:
120
400
Multidimensional Arrays (more than 2)
C allows arrays of more than two dimensions
...
[SizeN];
int m[4][3][6][5];
Variable Length Arrays
As explained earlier, in C89 array dimensions must be declared using constant expressions
...
However, this is not the case for C99, which adds a
powerful new feature to arrays: variable length
...
This is
called a variable-length array
...
void fun(int n)
{
int arr[n];
//
Title: Arrays and multidimensional arrays
Description: It contains info on Arrays,multidimensional arrays etc in C++ programming.It can be studied by 1st year beginner or anyone with decent computer knowledge.
Description: It contains info on Arrays,multidimensional arrays etc in C++ programming.It can be studied by 1st year beginner or anyone with decent computer knowledge.