An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array. For example:double salary = balance[9];The above statement will take 10th element from the array and assign the value to salary variable. Following is an example which will use all the above mentioned three concepts viz. declaration, assignment and accessing arrays:#include <iostream>using namespace std; #include <iomanip>using std::setw; int main (){ int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } cout << "Element" << setw( 13 ) << "Value" << endl; // output each array element's value for ( int j = 0; j < 10; j++ ) { cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl; } return 0;}This program makes use setw() function to format the output. When the above code is compiled and executed, it produces following result:Element Value 0 100 1 101 2 102 3 103 4 104 5 105 6 106 7 107 8 108 9 109
C++
Topic: Array
Explain about Accessing Array Elements ?
Browse random answers:
Explain about Declaring Arrays ?
Explain about Initializing Arrays?
Explain about Accessing Array Elements ?
What is the difference between an ARRAY and a LIST?
Define pointer and array. Explain the difference between them.
When should we use container classes instead of arrays?
What is Dynamic memory management for array?
What is the difference between class and objects?
What is static class member?
What are methods and fields?
What is an array?
What is a character array?
How do you decide which integer type to use?
Can you store multiple data types in System.Array?
Difference between "vector" and "array"?
How can double dimensional arrays be dynamically initialized in C++?
What is the difference between the System.Array.CopyTo() and System.Array.Clone()?
Why can't constant values be used to define an array's initial size?
What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
Delete duplicate elements from array
Is it valid to address one element beyond the end of an array?
Can the sizeof operator be used to tell the size of an array passed to a function?
What is meant by ?method-wars??
Write a program to delete an element from an array?