Reputation: 79
So I am supposed to take an array of integers, and a window size and "smooth the values of the array out. Meaning, it does this by taking the average of x values before and x values after a particular number and replacing that number with that average in a new array. Example, input array of {3,4,1,4,3,7,8} with a window size of 3. First and last values would be 0 as the window cannot extend before/after the array.
Here's my code
int *lowPassFilter(int arr[], int windowsize) {
// initialize new array to be returned
int *newarray[(sizeof(arr)/sizeof(arr[0]))];
for (int i = 0; i < (sizeof(arr)/sizeof(arr[0])); i++) {
if (arr[i] == arr[0]) {
newarray[0] == 0;
} else if (arr[i] == arr[sizeof(arr)/sizeof(arr[0])]) {
newarray[sizeof(arr)/sizeof(arr[0])] == 0;
} else if (((arr[i] - windowsize) < windowsize) || ((arr[i] + windowsize) - sizeof(arr)/sizeof(arr[0])) < windowsize) {
newarray[i] == 0;
} else {
newarray[i] == ((arr[i - ((windowsize - 1) / 2)] + (arr[i - ((windowsize - 1) / 2) + 1])
+ arr[i] + (arr[i + ((windowsize - 1) / 2)]) + (arr[i + ((windowsize - 1) / 2) + 1])) / 2);
}
}
return newarray;
}
int main() {
int g = rand() % ((7 - 3) + 1) + 3
int test[] = rand()
lowPassFilter(test, g);
}
The window size must be a value between 3 and 7, and the inputted array must be randomly generated.
I'm really stuck as how to fix this function. What I have does not compile. Any help would be appreciated.
Upvotes: 0
Views: 1549
Reputation: 17910
Your code suffers from multiple serious flaws. I think you're misunderstanding basic types like arrays etc. Just start with,
#include <cstdlib>
, causing the compiler not to find a definition for function rand()
. In C++ it might actually be good style to call it as std::rand()
instead.main
function are not followed by semicolons.sizeof(arr)
will return sizeof(int *)
instead of the size of the array passed, because a function parameter with the type int[]
basically decays to a pointer to int
. Hence, inside the function, the size of array arr
is not known, the function only has a pointer. I suggest the entire array be passed as an std::array
instead, or using a int *
to point to the data and a std::size_t
to hold the size.return newarray;
attempts to return a value of type int **
whereas the function type specifies the return type as int *
. Either way, you're returning a pointer to some local data, therefore ever accessing it after the function returns is undefined behavior. You specified the type of newarray
incorrectly as an array of pointers to int
whereas you probably wanted an array of int
instead.int test[] = rand();
attempts to initialize an int
array with an int
scalar.something == somethingElse;
have no effect, where you probably meant to use something = somethingElse;
.int i
of the for
-loop is signed variable, but you compare it with an unsigned (sizeof(arr)/sizeof(arr[0]))
. This might also lead to an undefined behavior (probably infinite loop) if i
overflows before ever reaching (sizeof(arr)/sizeof(arr[0]))
. There is also at least one other signed-vs-unsigned comparison in the code. I suggest that variables holding sizes of arrays and other objects be of the unsigned type std::size_t
.0
(and not from 1
). Hence newarray[sizeof(arr)/sizeof(arr[0])]
attempts to access an out-of-bounds array element, one past the end of the array.You're code would also be more readable if you formatted it a bit, and de-duplicated some code. For example you could write
newarray[i] == ((arr[i - ((windowsize - 1) / 2)] + (arr[i - ((windowsize - 1) / 2) + 1])
+ arr[i] + (arr[i + ((windowsize - 1) / 2)]) + (arr[i + ((windowsize - 1) / 2) + 1])) / 2);
more understandably as
auto const j = (windowsize - 1) / 2;
newarray[i] = (arr[i - j] + (arr[i - j + 1]) + arr[i]
+ (arr[i + j]) + (arr[i + j + 1])) / 2;
Next time, if something doesn't work and you want StackOverflow users to help you, please provide a Minimal, Complete, and Verifiable example.
Protip: Always compile your code with compiler warnings enabled, and read the warnings and suggestions. With GCC and Clang you should use the -Wall
and -Wextra
compiler flags.
Upvotes: 2