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

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;
}

Related

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

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);
}

When passing an array as an argument, is the same memory space of the array used, or is a copy made [duplicate]

This question already has answers here:
Passing Arrays to Function in C++
(5 answers)
Why isn't the size of an array parameter the same as within main?
(13 answers)
What is array to pointer decay?
(11 answers)
Closed 2 years ago.
Consider the following program:
void doSomething(char array[5])
{
char b = 3;
array[0] = b;
return;
}
int main()
{
char array[5] = {0, 1, 2, 3, 4};
doSomething(array);
return 0;
}
My question is, when passing the array to the doSomething function, is a copy of the array made and takes up more memory?, or is the pointer of the array simply passed, and the very same array is being modified?

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.

Why does the sizeof operator return the wrong value when used on an array passed to a method? [duplicate]

This question already has answers here:
sizeof an array passed as function argument [duplicate]
(3 answers)
Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?
(10 answers)
Closed 8 years ago.
int size(int arr1[])
{
int size1=sizeof(arr1)/sizeof(int);
cout<<size1<<endl;
return size1;
}
void main()
{
int b[]={1,2,3,4,5};
int size2 = size(b);
cout<<size2<<endl;
for (int i=0;i<size2;i++)
{
cout<<b[i];
}
}
I have put the b[] function into size() and check the size then return value.
however, it just return 1 as the answer.
Can anyone please help me to solve this.
A beginner of C++
sizeof(arr1) in the function returns the size of a pointer, not of the whole array.
That´s just how the language is.
You´ve to determine the array size without sizeof:
Either pass a second parameter with the number, or fill the array in a way
you can find the end because a certain value is there (and nowhere else)

Why does the length function return 2? [duplicate]

This question already has answers here:
When a function has a specific-size array parameter, why is it replaced with a pointer?
(3 answers)
sizeof(array) / sizeof(int) [duplicate]
(2 answers)
Closed 9 years ago.
I have written a function size which takes integer array as an argument.
int length(int a[])
{
return sizeof(a)/sizeof(int);
}
int main()
{
int a[] = {1,3,5,6,9,4,2,1,0,0};
int len = sizeof(a)/sizeof(int);
cout << len; // This correctly prints 10 .
len = size(a);
cout << len; // But this print 2 .why ??
return 0;
}
Can someone explain me this behaviour?
Thanks.
The reason you get 2 is because sizeof(int *) is twice as large as sizeof(int), and arrays decay into pointers when passed into a function.
There are various ways to work around this. In C++, you could use a std::vector<int> a = { ... }; instead, which would solve the problem (by calling the a.size() to get the size, as sizeof(a) wouldn't work).
Because sizeof(a) is returning the size of the pointer which will be either 4 bytes or 8 bytes depending upon whether your app is 32-bit or 64-bit. So you would need to maintain the length of your array using a separate variable.
Or, better still, I would recommend using one of the standard collection classes, like std::vector for example since this will maintain the length automatically.