Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How do I filter an array in C?
Post
How do I filter an array in C?
No, I'm not trying to get the full program written completely in C by you guys. I only need some way to implement the functionalities of each function I found confusing.
In this challenge in Code Golf CD, I have to make a program that computes the determinant of a two-dimensional array. It also has a program written in JavaScript that solves the challenge which looks like this:
function laplaceDet(matrix) {
if (matrix.length === 1) return matrix[0][0];
let sum = 0;
for (let rowIndex = 0; rowIndex < matrix.length; ++rowIndex) {
let minorMatrix = matrix.filter((_, index) => index !== rowIndex)
.map(row => row.slice(1));
sum += ((-1) ** rowIndex) * matrix[rowIndex][0] * laplaceDet(minorMatrix);
}
return sum;
}
I want to challenge myself into using C for solving the challenge. First, I'll need to reimplement the program, whereas the golfing part will be dealt with later, although I've already been golfing it. Here's what I got so far.
Why'd I stop? There's a function named filter()
and I'm not so sure how to get it implemented, and since C has no maps (at least I think none exist in C), it's also difficult to complete the program without it.
So I'll have to improvise... How? How do I implement the filter()
method, maybe without using a function to save bytes, and what's a C equivalent to the line of code matrix.filter((_, index) => index !== rowIndex).map(row => row.slice(1));
?
1 comment thread