Home > Backend Development > C++ > C++ program to iterate over array

C++ program to iterate over array

WBOY
Release: 2023-09-01 17:09:17
forward
650 people have browsed it

C++ program to iterate over array

Array is a continuous storage of the same type of data in memory. To access or address an array, we use the starting address of the array. Arrays have indexing, using which When addressing an array, we use the starting address of the array. Arrays have indexes and can be accessed through indexes We can access the elements of the array. In this article, we will introduce ways to iterate over arrays Operate on an array. This means accessing the elements present in the array.

Use for loop

The most common way to iterate over an array is to use a for loop. We use a for loop to In the next example, iterate over an array. One thing to note is that we need the size of the array array in this.

grammar

for ( init; condition; increment ) {
   statement(s);
}
Copy after login

algorithm

  • Enter data in the array arr of size n.
  • For i := 0 to i := n, execute:
    • Print(arr[i])
The Chinese translation of

Example

is:

Example

#include <iostream>
#include <set>
using namespace std;

// displays elements of an array using for loop
void solve(int arr[], int n){
   for(int i = 0; i < n; i++) {
      cout << arr[i] << ' ';
   }
   cout << endl;
}
int main(){
   int arr[] = {10, 5, 11, 13, 14, 2, 7, 65, 98, 23, 45, 32, 40, 88, 32};
   int n = 15;
   cout << "Values in the array are: ";
   solve(arr, n);
   return 0;
}
Copy after login

Output

Values in the array are: 10 5 11 13 14 2 7 65 98 23 45 32 40 88 32
Copy after login
Copy after login

Use while loop

Similar to the for loop, we can use the while loop to iterate the array. In this case, it's also like this

The size of the array must be known or determined.

grammar

while(condition) {
   statement(s);
}
Copy after login

algorithm

  • Enter data in the array arr of size n.
  • i := 0
  • while i < n, do:
    • Print(arr[i])
    • i := i 1
The Chinese translation of

Example

is:

Example

#include <iostream>
#include <set>
using namespace std;

// displays elements of an array using for loop
void solve(int arr[], int n){
   int i = 0;
   while (i < n) {
      cout << arr[i] << ' ';
      i++;
   }
   cout << endl;
}
int main(){
   int arr[] = {10, 5, 11, 13, 14, 2, 7, 65, 98, 23, 45, 32, 40, 88, 32};
   int n = 15;
   cout << "Values in the array are: ";
   solve(arr, n);
   return 0;
}
Copy after login

Output

Values in the array are: 10 5 11 13 14 2 7 65 98 23 45 32 40 88 32
Copy after login
Copy after login

Use forEach loop

We can also use a modern for-each loop to iterate over the elements in an array The main advantage is that we don't need to know the size of the array.

grammar

for (datatype val : array_name) {
   statements
}
Copy after login

algorithm

  • Enter data in the array arr of size n.
  • For each element val in the array arr, perform the following operations:
    • print(val)
The Chinese translation of

Example

is:

Example

#include <iostream>
#include <set>
using namespace std;
int main(){
   int arr[] = {10, 5, 11, 13, 14, 2, 7, 65, 98, 23, 45, 32, 40, 88, 32};
   
   //using for each loop
   cout << "Values in the array are: ";
   for(int val : arr) {
      cout << val << ' ';
   }
   cout << endl;
   return 0;
}
Copy after login

Output

Values in the array are: 10 5 11 13 14 2 7 65 98 23 45 32 40 88 32 
Copy after login

in conclusion

This article describes various methods of traversing arrays in C. The main methods include:

drawback of the first two methods is that the size of the array has to be known beforehand, But if we use for-each loop, this problem can be alleviated. for-each loop supports all STL container and easier to use.

The above is the detailed content of C++ program to iterate over array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template