answersLogoWhite

0


Best Answer

We use loops with arrays because it's one the simplest methods of traversing an array. since each element in an array is the same size, every element can be access via an offset from the start of the array, using the array suffix operator []. Arrays employ zero-based offsets because the first element is always at the same address as the array itself. Thus the first element is at offset [0], while the third is at offset [2]. What this means is that if we multiply the size of an element by its offset index, we can determine the address of that element and therefore access that element's value. The subscript operator does this for us automatically, thus giving us constant-time random access to any element in the array. We can also use pointers to manually calculate the address of an element (which is what actually goes on Behind the Scenes). However, when we wish to traverse the array, one element at a time, a loop is the simplest method of doing so. The loop simply iterates through all the offset indices, beginning with offset 0, then 1, and 2, and so on. The final index is always 1 less than the number of elements in the array, because arrays are zero-based.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

For the same reason you use arrays in any other language -- to store one or more related data elements of the same type within contiguous memory, with constant time random access to any element in the array. The most common type of array is a string, which is a simply an array of type char (ANSI/ASCII and multi-byte ASCII characters) or wchar_t (Unicode characters) .

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you use loop in arrays in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Example program arrays in turbo c plus plus?

Yes, you can use for-loop in a C program compiled by Turbo C.


Array in c plus plus must be defined at compil time?

No. Arrays can be defined at runtime, just as they can in C. It's just that it's generally more convenient to use vectors instead of dynamic arrays at runtime, thus arrays are generally used statically, at compile time.


What is the Difference between arrays in c and c plus plus?

Nothing whatsoever. They are exactly the same.


How do you convert meters to centimeters in c plus plus using the for loop construct?

It is unnecessary to use a for loop to convert meters to centimeters. Just multiply by 0.01.


Does 'for loop' works in C plus plus?

In C++, a for loop is structured as follows: for( int index = 0; index < 10; ++i ) { //do something }


C program to print numbers 1 to n?

how do we use loops in c plus plus programing and what are basic differences between do,for and while loop


Is for loop is faster than while loop in turbo c plus plus compiler?

No, why did you think so?


What is an infinite loop in c plus plus?

An infinite loop is one sequence of commands that just repeats over and over again forever. When it comes to creating an infinite loop you can use the: for do while and do statements. using the keywords 'true'


Example of flowchart of while loop in c plus plus?

kk


What is CPU usage small programm c plus plus?

Example: int main (void) { LOOP: goto LOOP; }


How do you display stars in c plus plus using the if else structure and a for loop and you use void main?

It depends on what program you design really


Do you have pointer concept in c plus plus language?

Yes. All string variables are pointers as are other arrays.