new array and old array

i have problem with the howfaraway function i want newarray = old array -avg but it doesnt print and im not getting the error.

1
2
3
4
5
6
7
8
9
10
11
12
13


void howfaraway (double i[], int n)

{
    int newarray, oldarray, avg;
    for(i=0; i<n; i++){
    newarray[i]=oldarray[i]-avg;
}

return newarray;
}
Last edited on
There is a lot of wrong stuff with howfaraway function.
first, newarray,oldarray, and avg are integers variables. Not arrays, as you think when you did newarray[i] = oldarray[i].
Besides, i is an array not an index.
Also, avg is not initialized. Hence, you'll have junk in the result of the manipulation at line 55.
There is more to note:
1
2
3
4
5
6
void howfaraway()
{
// ... declare and set newarray

return newarray;
}

The function claims to return no value (void on line 1), yet the code attempts to return something.

If the newarray were an array, then it could not be returned; an array is not a valid return type.
(The std::array<double> is an object type though and thus a valid return type.)

However, an array parameter is effectively a by reference parameter.
This does change an array of the caller:
1
2
3
4
5
6
void howfaraway( double newarr[], const double oldarr[], size_t n ) {
  // newarr must be >= oldarr
  for ( size_t i=0; i < n; ++i ) {
    newarr[i] = oldarr[i];
  }
}
Topic archived. No new replies allowed.