array calling, what's wrong?

hi all

how are you?

can any one tell me what is wrong about the following code, and how to correct it?

thank you

#include<iostream>

#include<stdlib.h>

#define cromo_len 13

#define pop_sz 20

void main()

{

float pop_arr[pop_sz][cromo_len];

int i;

.

.

.

.

calc_fit(pop_arrIdea);

}

float calc_fit(float arr[])

{

.

.

.

.

return arr[cromo_len];

}

[1260 byte] By [m_amin] at [2008-1-10]
# 1

What's the code trying to do? Assuming that you want calc_fit to access the whole of the array pop_arr I would change would be the declaration of calc_fit to:

Code Snippet
float calc_fit(float (&arr)[pop_sz][cromo_len])
{
...
}

JonathanCaves-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...
# 2

i am taking 1 row only of the array, and trying to pass it to the function, calculate the sum of its bits in he last bit.

so i am not the whole array.

m_amin at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...
# 3

Then I would suggest something like the following:

Code Snippet

#define cromo_len 13
#define pop_sz 20

float calc_fit(float (&arr)[cromo_len])
{
// ...
}

int main()
{
float pop_arr[pop_sz][cromo_len];

// Initialize array

for (int i = 0; i < pop_sz; ++i)
{
<something> = calc_fit(pop_arr[i]);
}
}

JonathanCaves-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...
# 4
m_amin wrote:

float calc_fit(float arr[])

{

.

.

.

.

return arr[cromo_len];

}

return arr[cromo_len-1] is likely to work better.

Along with whatever else you are doing, recall that a float array1[len] has elements

array1[ 0 ] to array1[len-1]

- Dennis

orcmid at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...
# 5

thank you

but the answer was as simple as:

i should have written the function before the main, not vise versa. my function was working all the time!!!

m_amin at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C++ 2005 Express Edition...