Error : 'For statement require a suitable begin.' [duplicate] - c++

This question already has answers here:
Passing Arrays to Function in C++
(5 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 4 months ago.
This program is to find Frequency of different elements in array using unordered map. But I am unable to debug it, as this error is occuring for the first time in my code. Please help me debug this code.
#include<iostream>
#include<unordered_map>
using namespace std;
void frequency(int arr[] , int n)
{
unordered_map<int,int> m;
for(int x : arr) /* This line is showing error(for statement require a suitable begin function and none was found .)*/
m[x]++;
for( auto x : m)
cout<<x.first<<" : "<<x.second;
}
int main()
{
int n = 8;
int arr[n] = {10,12,10,15,10,20,12,12};
frequency(arr,n);
}

Related

what is for(auto& x : s) in C++ [duplicate]

This question already has answers here:
C++11 range based loop: How does it really work
(3 answers)
Range based for-loop with &
(3 answers)
How to correctly implement custom iterators and const_iterators?
(9 answers)
How to make my custom type to work with "range-based for loops"?
(10 answers)
Closed 21 days ago.
Since I am new to C++, I do not know what the expression means?
and how can i co-relate this expression with C language.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// string to be converted to lowercase
string s = "GEEKSFORGEEKS";
for (auto& x : s) {
x = tolower(x);
}
cout << s;
return 0;
}

C++ array length function give me a different result than main function [duplicate]

This question already has answers here:
size of array passed to C++ function? [duplicate]
(7 answers)
determine size of array if passed to function
(10 answers)
Sizeof in function [duplicate]
(1 answer)
Closed 6 months ago.
#include<iostream>
#include <string>
using namespace std;
void show_length(int array1[]){
int length=sizeof(array1)/sizeof(array1[0]);
cout<<"Length inside function = "<<length<<"\n";
}
int main(){
int array1[6]={3,6,4,2,1,4};
int length=sizeof(array1)/sizeof(array1[0]);
cout<<"Length inside main = "<<length<<"\n";
show_length(array1);
system("pause");
}
i recive 6 as an answer in the main (which is the right answer) but 2 in the separate function, why this happens?

Function not working as it's supposed to in C++ [duplicate]

This question already has answers here:
C sizeof a passed array [duplicate]
(7 answers)
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 8 months ago.
I am trying to make my own function that takes a double array and returns maximum index. But when I run the code to check the function with a random array below, it returns 0 when it's supposed to return 9. When I run the same exact code inside the function in main, it works. What's wrong with my code that is working in main() but not when used inside a function?
int findMaxIndex(double array[]) {
int maxIndex=0;
int arraySize = sizeof(array)/sizeof(array[0]);
for (int i=0;i<arraySize;i++) {
if (array[maxIndex]<array[i]) {
maxIndex=i;
}
}
return maxIndex;
}
int main() {
double array[10] =
{11.20,22.2,33.3,43.4,55.5,66.6,77.7,88.8,90.0,100};
cout <<findMaxIndex(array)<<endl;
return 0;
}

Function calling and return statement [duplicate]

This question already has answers here:
Omitting return statement in C++
(3 answers)
Why does this C++ snippet compile (non-void function does not return a value) [duplicate]
(7 answers)
Closed 2 years ago.
In the given C++ code when the statement n isn't present the function abc returns 10 .I.e def(10) acts as if it is a return statement but it isn't.
The output of the function is always 10 and doesn't lead to undefined behaviour when statement n as given in code isn't present in the code.
I am using sublime text 3.
So why it is behaving in such a manner or am I missing any concept
#include<bits/stdc++.h>
using namespace std;
int def(int x){
return x;
}
int abc(int x){
def(10);
return x;//statement: n
}
int main(){
cout<<abc(5);
}

Why is the size of array not same in both the cases? [duplicate]

This question already has answers here:
Why is sizeof(array) different in these two cases? [duplicate]
(2 answers)
Closed 7 years ago.
#include<iostream>
using namespace std;
void f(int arr[])
{
int a=sizeof(arr);
cout<<a;
}
int main()
{
int n;
cin>>n;
int arr[n];
int a=sizeof(arr);
cout<<a<<"\n";
f(arr);
}
Output:
24
8
Why is my output not same in both the cases even when i am printing the size of same array?
In the first call it shows the size of array.
But in the second case it shows the size of the pointer passed.
When arrays are passed through functions they decay into pointers. So after passing, the:
sizeof(array)
Shows the size of the pointer.