Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

66%
+2 −0
Challenges Compute the determinant

C, 147 bytes float d(float**m,int r){if(r<2)return**m;int i=r,j;float s=0,*n[--r];for(;i--;s+=(i%2?-1:1)**m[i]*d(n,r))for(j=r;j--;)n[j]=m[j+(j>=i)]+1;return s;} Try it online! Basically ...

posted 2y ago by Moshi‭

Answer
#1: Initial revision by user avatar Moshi‭ · 2021-08-28T00:54:46Z (over 2 years ago)
# C, 147 bytes

```c
float d(float**m,int r){if(r<2)return**m;int i=r,j;float s=0,*n[--r];for(;i--;s+=(i%2?-1:1)**m[i]*d(n,r))for(j=r;j--;)n[j]=m[j+(j>=i)]+1;return s;}
```

[Try it online!][TIO-ksv2632l]

Basically a C version of my example code. Takes an array of pointers and the size.

Prettified and commented version:

```c
float d(float **m, int r) {
	if(r < 2) return **m;  // Base case: 1x1 matrix

	int i = r, j;          // Variable initialization, i and j are counters
	float s = 0, *n[--r];  // s is the sum, n is the minor matrix

	for(; i--; s += (i % 2 ? -1 : 1) * *m[i] * d(n, r)) // Outer loop: loop over the rows of M.
	                                                    // After each iteration, add the term to the sum
		for(j = r; j--; )                           // Inner loop to fill in the minor matrix
			n[j] = m[j + (j >= i)] + 1;         // (j >= i) term skips over the ith row of M.
			                                    // Addition of 1 effectively removes the first element
	return s;
}
```

[Try it online!][TIO-ksv2hv64]

[C (gcc)]: https://gcc.gnu.org/
[TIO-ksv2hv64]: https://tio.run/##nVRNb5tAED3DrxjFsgQOOAHsfoQkVSv10EPVQ6P2QKxoC4u9LizWsk7dRv7rdWf4iB0HuW45jHeZeW/ePNYbu9M43mzSrGAaEqv@HQxyB4TUoGx4MA2RWgouwbdBcb1UkvIhwNkZvGMlhxjDBXgrD3KmlViZiECsgCtQDsyxsn0Q8YUpwb5lHOmFFiwTv5gWhcR2wGQCc2AKGYul1FyVplHrKZHq3IGBjFxXTerWJYgS9IxDuUSxst3lQhZqKyQtlBWCcN0QAadXYAnogw9vwPUANdswgEEeiQn@JhaqULZN5J@W2B6yolhcVBGKe9wTvyp@lFCk8HFoGvAfD5K/TYmcs3gGAlfN/CxJqgb4JgddtKOZRjXDnMwMYU6D2IfpP0jZaCeaVGQZev3cG8MwZDSfIG8ezeEUsMX1FQh7gmsv3CVsM7W08rtYlFs/hJ6RJ60lhnGsCUkiaHACesDTlMda3PPsJ56xHNnrr5kKVWrgGc@51KbRHL8yNNebnpBxtkw4XJY6EcVwdm2avYSnQnK4ef/55s63Vg7knkfBx@DTysdDfNseq1Wvd4fa77yIbHh4LIa1Q0W11rbIb4saHliHW6YBrJr0I6ezRTalC4V/i9Q6SbiGKOpPHehPJw60K8L3p7fypEu1g8cTx/FtO9wbM9gb0wt2UblP24C2AW2D4Ij5K5ajTKgadFYGbeVu73/wbLsMDtj31MKOXbepf/OocTuo3DbpMsuZkFZ1GTZnS/NSI8Ib4r10jmH/NqB3lLXNJxCfXo7xQ3ZA3IAwo@F4D4NyqszLDkzV3x3t9AkqzGhH2kF9B7LPRqipxw2ORqiE7YNHlBhTeNGRpSngFYXXRL02N7/jNGPTcuMueMKkFvHG/cqy7A8 "C (gcc) – Try It Online"

[TIO-ksv2632l]: https://tio.run/##lVJdb5swFH0uv@KqUSRM7CjYSbvNpXvaL1ilPVAURXzNCJzKUC1SlL8@5kugSTOUd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