Created
May 21, 2024 20:56
-
-
Save apzzd/6e49afb34bf1d69d0b7d170e45195d14 to your computer and use it in GitHub Desktop.
✨ c code for multiplying a vector by a 2x2 matrix ✨
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) { | |
// creating variables | |
float vector[4]; | |
float matrix[4]; | |
int i; | |
char* token; | |
char* rest; | |
// splitting first arg by ", " and assigning the values to the vector | |
rest = argv[1]; | |
i=0; | |
while ((token = strtok_r(rest, ", ", &rest))) { | |
vector[i] = atof(token); | |
i++; | |
} | |
// doing the same thing for the matrix | |
rest = argv[2]; | |
i=0; | |
while ((token = strtok_r(rest, ", ", &rest))) { | |
matrix[i] = atof(token); | |
i++; | |
} | |
// multiplying the matrix and vector | |
float product[] = { | |
(vector[0] * matrix[0]) + (vector[1] * matrix[2]), | |
(vector[0] * matrix[1]) + (vector[1] * matrix[3]) | |
}; | |
// printing it out | |
printf("(%g) (%g %g) = (%g)\n", vector[0], matrix[0], matrix[1], product[0]); | |
printf("(%g) (%g %g) = (%g)\n", vector[1], matrix[2], matrix[3], product[1]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment