range - based loop traversal in array

36 Views Asked by At

**The code I tried to implement is : **

#include<iostream>
using namespace std;

int main()
{
   int n, k;
   cout << "Enter the number of elements: \n";
   cin >> n;

   cout << "Enter the array: \n";
   int *arr = new int[n];
   for (int i = 0; i < n; i++)
        cin >> arr[i];

   cout << "Enter k: " << endl;
   cin >> k;

   cout << "Elements less than k: " << endl;
   for (int x : arr)
   {
       if (x < k)
            cout << x << endl;
   }

   
   delete[] arr;

   return 0;
}

**The error it's throwing is :** error: 'begin' was not declared in this scope for (int x : arr) ^

The array is dynamically allocated. Takes elements and var 'k' as input and ideally returns the elements which are less than k. first loop is traditional for loop second one is range based, and the problem is in that

I tried taking reference '&x' in there to but the problem persists

problem :

error: 'begin' was not declared in this scope
    for (int x : arr)
                 ^
0

There are 0 best solutions below