there is function :

int median(int a[], int b[])
{

}

how do i find the number of elements in a and b?
i am doing sizeof(a)/sizeof(int) but it does not give right results.
P.S. i need to submit the function only in online compiler so i dont know what is the length of array.

henrySh commented: you need a for loop to enter an read the members from the array +0

Recommended Answers

All 32 Replies

Pass the size in using two more parameters. There's no standard way to determine the size of an array when given nothing but a pointer to the first element.

Or use std::vector instead of array

commented: the array has a limited members.....while the vector has unlimited members,so we can add members as we need +0

@all
there is a question and i need to submit it online.

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

int median(int input1[],int input2[])
{
    //Write code here
}

here i cant use vector and cant pass the size from main function. i just need to complete the median function and submit it.

You have already been told there is no way to safely do this, you either pass the size of the arrays to the function or you don't do it at all unless they are of a known constant size.

If you cannot use more parameters then pass the size in the first element of each array.

i cant use vector and cant pass the size from main function. i just need to complete the median function and submit it

If you can't change that function then the task is not possible. You can't compute the median if you don't know the number of elements in the array. The only other possibility is to make the array size a global variable, but that is a horrible suggestion.

Maybe the question says something about the array ending with a specific number? Like the last element of the array might be 0 or -1? In that case you can find out the size of the array by going through the array until you encounter that number. Or possibly it says that the arrays will have a specific size. If the question doesn't mention anything like that, it's simply not solvable.

in input specification it is mentioned both of array size will be equal to 'N' and nothing else.
i have seen some score of people who submits it. i found CSHARP solution has got points .
is there any way in this language?

in input specification it is mentioned both of array size will be equal to 'N' and nothing else.

That's enough, isn't it? So, just use i < N as your loop condition.

@sepp2k i dont know what is 'N' so i cant use it in loop.

the admins of that site was not replying to my mails. i am assuming there is no way to find the length of arrays.

i dont know what is 'N' so i cant use it in loop.

What do you mean? Can't you just literally write i < N? Did you try it?

If the problem description says that the size is N, then I'm going to assume that they define a constant N that you can then use. Otherwise writing that would be nonsense.

@sepp2k i have already tried i<N but this is giving error as N is undefined.
by the way in CSHARP ,Length is giving the length of the array.

i have already tried i<N but this is giving error as N is undefined.

Locally or on the online compiler? Can you post the exact wording of the assingment? Or post a link to it if it's publically accessible?

by the way in CSHARP ,Length is giving the length of the array.

Yes, but C++ isn't C#. C++ arrays simply don't know their length. They're just a chunk of memory with no assoicated metadata.

@sepp2k this is the link

check if i am missing something?

@shanki: that link just takes us to the coding challenge -- we can't see the problem unless we begin the challenge. Since it wants to know everything about my FB I declined to take it.

Just took a look at the challenge. There's a lot more involved than just the median. you're combining and sorting 2 arrays first.

@tinstaafl i know what the question is . my main problem is how to get the length of the two arrays?

my main problem is how to get the length of the two arrays?

Our main problem is that we don't know the whole question.

i give a link but i dont know why you guys cant open it and read the question and answer my query?

I can't; the link seems to lead to some kind of Facebook login. I'm not joining facebook just to read a question and I expect many others will feel similarly.

I am a FB member but I refused to allow them full access to my account. That's just stupid.

i take the screenshots of the question and saved them in doc file.
this is the dropbox link to download the file.

after reading the question, please read my first post then you will better understand what problem i am facing.

There's nothing in the question to prohibit declaring SIZE as a global constant, and using that for the loop. Also the problem you're encountering doesn't seem to be in the size of the array, because sizeof(a)/sizeof(int) produces the right number, which in this case is 11.

because sizeof(a)/sizeof(int) produces the right number,

Not in those functions it doesn't. Arrays are always passed as pointers, so sizeof(any array) is the same as sizeof(any pointer) (at least in 32-bit operating systems such as MS-Windows, MS-DOS and *.nix.

i have seen people submitted in C and C++ too. dont know how?

after reading the question, please read my first post then you will better understand what problem i am facing

I suppose people will better understand it, at least better than yourself it seems.
'n' or 'N' is just a variable number uses to indicate the arrays can be of different sizes.
But guess what, you determine those sizes yourself before you even create them for the purpose of your demonstration, so you already know the sizes. And as mentioned above there is nothing prohibiting you from passing those sizes into a function.

No sane developer/person/company it going to have an array of employees and not know how many employees there are in it.

you determine those sizes yourself before you even create them

well if you have read the question(in the link), then you will know that these arrays are not 'created' by me, these are input to me. if you have participated in any Topcoder SRM then you will better 'understand' this.
i request you to please 'read' all the comments in this post.

i have seen people submitted in C and C++ too. dont know how?

Huh? That's confusing???

these are input to me

Input how? The problem description doesn't clarify that. Do you just write your function with a given signature and the online judge provides a main function that calls your function? Or do you read the input from stdin (which is what you'd do in most online judge systems that I'm familiar with)? In the latter case you can just keep track of how many items you've read from stdin.

if you have participated in any Topcoder SRM then you will better 'understand' this.

I haven't.

Edit: Oh, I hadn't seen the second page of your link. Looks like it's the former version (i.e. you write the function only and the main function is supplied). Then it's just not possible (unless a constant N were defined for you, but you already said that's not the case). No idea how others could submit a working solution for that problem.

well if you have read the question(in the link), then you will know that these arrays are not 'created' by me, these are input to me. if you have participated in any Topcoder SRM then you will better 'understand' this.

If somebody gives you an unchangeable interface where the size is not passed, assumed to be something, acquired from input, or requires a sentinel value, you're SOL. At that point all you have is a pointer, and there's no standard way to figure out the size of an array when given a pointer to the first element (as stated clearly in my first reply).

I've read the question in the link and nowhere does it specify how you're to determine the value of N, thus the problem definition itself is ambiguous and faulty. Contact the author of the problem and complain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.