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

Pages: 12
Apr 20, 2013 at 4:50am
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 Apr 20, 2013 at 5:17am
Apr 20, 2013 at 4:56am
I don't see any reason why you wouldn't know the size, And don't know of a way to do that.
Apr 20, 2013 at 5:06am
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 Apr 20, 2013 at 5:07am
Apr 20, 2013 at 5:18am
@anmol this doesn't work if array are passed as arguments
Apr 20, 2013 at 3:43pm
Yes it does. When you call it do that, not inside of the function itself.
Apr 20, 2013 at 4:49pm
i knw it does outside function but here all i have to write is function
Apr 20, 2013 at 5:30pm
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
Apr 20, 2013 at 7:14pm
i cant change the argument list,it is already define
Apr 20, 2013 at 7:39pm
Try passing the array as a reference and using that reference in the size of operator like I showed you.
Apr 20, 2013 at 7:45pm
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.
Apr 20, 2013 at 7:45pm
give this a read. google is a wonderfull thing
http://www.cplusplus.com/faq/sequences/arrays/sizeof-array/
Last edited on Apr 20, 2013 at 7:58pm
Apr 20, 2013 at 9:03pm
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
Apr 20, 2013 at 9:08pm
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 Apr 20, 2013 at 9:10pm
Apr 20, 2013 at 10:23pm
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.
Apr 21, 2013 at 1:58am
That makes me sign up for a big old thing.

Can't you just copy/paste the requirements here?
Apr 21, 2013 at 4:30pm
it doesn't allow you to copy data and if you have fb account you can easily access it,pls check it if possible.
Apr 21, 2013 at 5:23pm
Type it out for us.
Apr 21, 2013 at 6:43pm
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
}
Apr 21, 2013 at 6:58pm
stdio.h? is this used in c++?

Aceix.
Apr 21, 2013 at 7:18pm
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