Asked By
ramiz
140 points
N/A
Posted on - 05/07/2011
How can we access the array elements? I am working on loops but there is a problem in the code, every time I have to initialize “I” to the next value when I want to store any value in the array. Is it right way to assign values to an array? Int marks[5]; Int i=1; Marks[i]=5; i=2; Marks[i]=4; If yes then tell me how can I access the values from the array when I want to display the value of specific index?
Answered By
JBond
0 points
N/A
#90702
How to access Array Elements when working with loops?
An Array is a group of consecutive memory locations with same name and type. Simple variable is a single memory location with a unique name and a type. But an array is a collection of different adjacent memory locations. All these memory locations have same name and data type. The memory locations in an array are known as elements of an array.
The total number of elements in an array is called “Length” of an array.
Each element in the array is accessed with reference to its position of location in the array. This position is called Index or Subscript.
Each individual element of an array is accessed by specifying the
1: Name of an array
2: Index of element
This is the complete information about the procedure of accessing array elements.
The code you have given is incorrect. Here you are assigning values to the elements of an array. So you don’t need to use “i” here.
The correct code will be like
Int marks[4];
Marks[1]=5;
Marks[2]=6;
Marks[3]=8;
Marks[4]=15;
Answered By
ramiz
140 points
N/A
#90703
How to access Array Elements when working with loops?
Thanks a lot dear. Now, the issue of accessing remains the same (not solved). Guide me how we can access the elements of an array? Is there any other way to access the array elements in a group instead of accessing them on by one individually? And what is the faster way of accessing the array elements.
Answered By
MRobert
0 points
N/A
#90704
How to access Array Elements when working with loops?
There are two ways of accessing array elements.
1: Without loops
2: With loops
In the first way we access the elements of an array with defining the index. For example: If we have an array like: Int arr [5]; We will access the array elements as: Cout<
Answered By
JBond
0 points
N/A
#90705
How to access Array Elements when working with loops?
Yea, using loops when you are working with arrays is faster and reliable. And loop are best for arrays.
I am sending you a code of a program where we are taking 5 integers from the user and saving in the array. Then we will display all the values of an array by using loops instead of individual index.
Program:
#include
#include
Void main()
{
Int arr[5];
For( int i=0; i<5; i++)
{
Cout<<”Enter an integer “;
Cin>>arr[i];
}
Cout<<”The values in array are: n”;
For( int i=0; i<5; i++)
Cout<
Answered By
ramiz
140 points
N/A
#90707
How to access Array Elements when working with loops?
Thanks a lot. You both have helped me to understand the concept of this problem.
Now I have understood while we are working with the group of elements “Array”, we must use a method to access the elements in group format. And loops are best to access array elements. Individual initialization and accessing is lengthy and slow with respect to using loops for both, initialization and accession.
Your expert advice helps me solve my problem!
Answered By
Raheel
410 points
N/A
#90708
How to access Array Elements when working with loops?
Awesome working, really helpful
Regards
Answered By
MRobert
0 points
N/A
#90709
How to access Array Elements when working with loops?
Yea you are right. JBond, you have explained very well. Superb
Regards