How to know how many elements are in array which is passed as an argument to function ?

Pages: 12
like
int add(int a[],int b[])

how i can use loop to traverse array if i dnt knw the size?

I was participating in a programming contest in which i only have to write function and function declaration was given as int med(int intput1[],int intput2[])

how i can perform operations on them?
Last edited on
I don't see any reason why you wouldn't know the size, And don't know of a way to do that.
use this:

int add(int a[], int b[], size_t sizea);

To calculate size:

cout<<add(a,b,sizeof(a)/sizeof(a[0])
Last edited on
@anmol this doesn't work if array are passed as arguments
Yes it does. When you call it do that, not inside of the function itself.
i knw it does outside function but here all i have to write is function
well, you can add another argument for the size of the array, like this:
1
2
int size_of_array = sizeof (your_variable);
some_func (your_variable, size_of_array);


CMIIW
i cant change the argument list,it is already define
Try passing the array as a reference and using that reference in the size of operator like I showed you.
Or declare a global variable called size and set it to the size of the array and use that inside of the function so it does not modify the argument list.
give this a read. google is a wonderfull thing
http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/
Last edited on
i don't know that if array passed is declared as global or in void main as i can't see the void main() and ican't take chance as only 1 attempt is left
like
int add(int a[],int b[])

how i can use loop to traverse array if i dnt knw the size?

...

i cant change the argument list,it is already define



You can't. It's 100% impossible (at least not without doing extremely nasty, compiler-dependent tricks).

Unless the size is stored globally somewhere there is no way to know the array size from just those parameters.


EDIT: I find it hard to believe that the contest is giving out impossible problems. Maybe you are misinterpretting the problem? Can you give a link to the contest?
Last edited on
https://apps.facebook.com/techchallenge/?fb_source=search&ref=ts&fref=ts

it link to the coding challenge,there you can choose coding challenge and after that c++ as language.
That makes me sign up for a big old thing.

Can't you just copy/paste the requirements here?
it doesn't allow you to copy data and if you have fb account you can easily access it,pls check it if possible.
Type it out for us.
in that you have to first merge two arrays in sorted order and then find their median
and code box is given as below

#include<stdio.h>
#include<string.>

int median(int input1[],int input2[])
{
//type your code here
}
stdio.h? is this used in c++?

Aceix.
When I see stuff like:

Tech Challenge would like to access your public profile, friend list, email address, work history, education history, hometown, current city and your friends' work histories, education histories, hometowns and current cities.


I always hit cancel.

Anyway, it's a coding challenge. If you can't even repeat the instructions intelligibly, maybe the challenge isn't for you.
Pages: 12