Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Single-Dimensional Arrays (C# Programming Guide)
In this articleYou create a single-dimensional array using the new operator specifying the array element type and the number of elements. The following example declares an array of five integers:
This array contains the elements from Arrays can store any element type you specify, such as the following example that declares an array of strings:
Array InitializationYou can initialize the elements of an array when you declare the array. The length specifier isn't needed because it's inferred by the number of elements in the initialization list. For example:
The following code shows a declaration of a string array where each array element is initialized by a name of a day:
You can avoid the
You can declare an array variable without creating it, but you must use the
Value Type and Reference Type ArraysConsider the following array declaration:
The result of this statement depends on whether Retrieving data from ArrayYou can retrieve the data of an array by using an index. For example:
See also
FeedbackSubmit and view feedback for Introduction to C Programming ArraysOverview
Declaring Arrays
int i, j, intArray[ 10 ], number; float floatArray[ 1000 ]; int tableArray[ 3 ][ 5 ]; /* 3 rows by 5 columns */ const int NROWS = 100; // ( Old code would use #define NROWS 100 ) const int NCOLS = 200; // ( Old code would use #define NCOLS 200 ) float matrix[ NROWS ][ NCOLS ]; int numElements; printf( "How big an array do you want? " ); scanf( "%d", &numElements; ) if( numElements <= 0 ) { printf( "Error - Quitting\n" ); exit( 0 ); } double data[ numElements ]; // This only works in C99, not in plain C Initializing Arrays
int i = 5, intArray[ 6 ] = { 1, 2, 3, 4, 5, 6 }, k; float sum = 0.0f, floatArray[ 100 ] = { 1.0f, 5.0f, 20.0f }; double piFractions[ ] = { 3.141592654, 1.570796327, 0.785398163 };
Using Arrays
Sample Programs Using 1-D Arrays
/* Program to calculate the first 20 Fibonacci numbers. */ #include <stdlib.h> #include <stdio.h> int main( void ) { int i, fibonacci[ 20 ]; fibonacci[ 0 ] = 0; fibonacci[ 1 ] = 1; for( i = 2; i < 20; i++ ) fibonacci[ i ] = fibonacci[ i - 2 ] + fibonacci[ i - 1 ]; for( i = 0; i < 20; i++ ) printf( "Fibonacci[ %d ] = %f\n", i, fibonacci[ i ] ); } /* End of sample program to calculate Fibonacci numbers */ /* Sample Program Using Arrays */ #include <stdlib.h> #include <stdio.h> int main( void ) { int numbers[ 10 ]; int i, index = 2; for( i = 0; i < 10; i++ ) numbers[ i ] = i * 10; numbers[ 8 ] = 25; numbers[ 5 ] = numbers[ 9 ] / 3; numbers[ 4 ] += numbers[ 2 ] / numbers[ 1 ]; numbers[ index ] = 5; ++numbers[ index ]; numbers[ numbers[ index++ ] ] = 100; numbers[ index ] = numbers[ numbers[ index + 1 ] / 7 ]--; for( index = 0; index < 10; index++ ) printf( "numbers[ %d ] = %d\n" index, numbers[ index ] ); } /* End of second sample program */ Multidimensional Arrays
Sample Program Using 2-D Arrays/* Sample program Using 2-D Arrays */ #include <stdlib.h> #include <stdio.h> int main( void ) { /* Program to add two multidimensional arrays */ /* Written May 1995 by George P. Burdell */ int a[ 2 ][ 3 ] = { { 5, 6, 7 }, { 10, 20, 30 } }; int b[ 2 ][ 3 ] = { { 1, 2, 3 }, { 3, 2, 1 } }; int sum[ 2 ][ 3 ], row, column; /* First the addition */ for( row = 0; row < 2; row++ ) for( column = 0; column < 3; column++ ) sum[ row ][ column ] = a[ row ][ column ] + b[ row ][ column ]; /* Then print the results */ printf( "The sum is: \n\n" ); for( row = 0; row < 2; row++ ) { for( column = 0; column < 3; column++ ) printf( "\t%d", sum[ row ][ column ] ); printf( '\n' ); /* at end of each row */ } return 0; } Exercises
Material Below This Line Not Covered in CS 107 Until LaterPassing Arrays to Functions ( in C++ )
/* Program Illustrating the use of Arrays and Functions */ #include <iostream> using namespace std; // Finds max in the array float maxArray( const float numbers[ ], int arraySize, int & errorCode ); int main( void ) { float array1[ ] = { 10.0, 20.0, 100.0, 0.001 }; float array2[ 2 ][ 3 ] = { { 5.0, 10.0, 20.0 }, { 8.0, 15.0, 42.0 } }; int sizes[ 2 ] = { 4, 3 }, err1, err2, err3, err4, err5; float max1, max2, max3, max4, max5; max1 = maxArray( array1, 4, err1 ); max2 = maxArray( array1, sizes[ 0 ], err2 ); max3 = maxArray( array2[ 1 ], 3, err3 ); max4 = maxArray( array2[ 0 ], 6, err4 ); max5 = maxArray( array1, -4, err5 ); // Generates an error cout << "Maximums are " << max1 << ", " << max2 << ", " << max3 << ", " << max4 << ", " << max5 << endl; cout << "( Error codes are " << err1 << ", " << err2 << ", " << err3 << ", " << err4 << ", " << err5 << " )\n" ; return 0; } float maxArray( const float numbers[ ], int arraySize, int & errorCode ) { /* Function to find the maximum in an array of floats Note the use of the keyword "const" to prevent changing array data */ int i; float max; if( arraySize <= 0 ) { errorCode = -1; return 0.0; } errorCode = 0; max = numbers[ 0 ]; for( i = 1; i < arraySize; i++ ) max = ( numbers[ i ] > max ) ? numbers[ i ] : max; return max; } ExercisesWrite the following functions:
Character Strings as Arrays of Characters
What is the initialization of array?The initializer for an array is a comma-separated list of constant expressions enclosed in braces ( { } ). The initializer is preceded by an equal sign ( = ). You do not need to initialize all elements in an array.
What is correct syntax of array declaration by initializing elements?A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int, float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the size of the array.
How do you initializes an array in C?Method 1: Initialize an array using an Initializer List. int arr[5] = {1, 2, 3, 4, 5};. int arr[5] = {1, 2, 3};. // Valid. ... . #include <stdio.h> int main() { // You must mention the size of the array, if you want more than one // element initialized to 0 // Here, all 5 elements are set to 0! ... . 0 0 0 0 0.. Which of the following statements are true about arrays in Java?Arrays in java are essentially objects. These contains elements of similar data type and the elements in an array are stored in contiguous memory location. Hence, option (1) is the correct statement.
|