0
How to work with arrays and how to enter data into them and retrieve them for use ???
how array helps and how to enter and get data from them !!!!!
2 Réponses
+ 1
declare the array
int myarray[]={10, 11, 12, 13};
myarray length is 4because it contains 4 elements that are indexed starting from 0
So the firts element of the array (10 in the example) is in the 0 position
cout<<myarray[0];
will output the value stored in the 0 position (first element)of myarray: 10.
cout<<myarray[1];
will output the value stored in the 1 position (second element) of myarray: 11.
0
1.you can enter data into them by using a for loop.
eg.
int numbers[5]; //array of 5 elements,index 0 to 4
for(int x=0;x<5;x++){
cin>>numbers[x];
}
2. can use them ,for example, to calculate the square of each element and display them.
int temp;
for(int i=0;i<5;i++);
temp = numbers[i]*numbers[i];
numbers[i]=temp;
cout<<numbers[i]<<endl;
}