Add problem 10 solution
This commit is contained in:
parent
3610120c57
commit
55d6a8c5a8
@ -12,3 +12,5 @@ set(proj_include "include")
|
|||||||
target_sources(${PROJECT_NAME} PRIVATE "${proj_sources}")
|
target_sources(${PROJECT_NAME} PRIVATE "${proj_sources}")
|
||||||
target_include_directories(${PROJECT_NAME} PRIVATE "${proj_include}")
|
target_include_directories(${PROJECT_NAME} PRIVATE "${proj_include}")
|
||||||
|
|
||||||
|
target_link_libraries(${PROJECT_NAME} m)
|
||||||
|
|
||||||
|
|||||||
7
include/p10.h
Normal file
7
include/p10.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef P10_H
|
||||||
|
#define P10_H
|
||||||
|
|
||||||
|
void calculate_magnitudes(double (*vectors)[8], int num_vectors, double *out_magnitudes);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
18
src/main.c
18
src/main.c
@ -9,6 +9,7 @@
|
|||||||
#include "p7.h"
|
#include "p7.h"
|
||||||
#include "p8.h"
|
#include "p8.h"
|
||||||
#include "p9.h"
|
#include "p9.h"
|
||||||
|
#include "p10.h"
|
||||||
|
|
||||||
void print_int_arr(int* arr, int size) {
|
void print_int_arr(int* arr, int size) {
|
||||||
for (int i = 0; i < size; ++i) {
|
for (int i = 0; i < size; ++i) {
|
||||||
@ -114,6 +115,23 @@ int main(void) {
|
|||||||
printf("Did not find any matching products!\n");
|
printf("Did not find any matching products!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Problem 10
|
||||||
|
printf("Problem 10:\n");
|
||||||
|
|
||||||
|
double vectors[3][8] = {
|
||||||
|
{ 1, 2, 3, 4, 5, 6, 7, 8 },
|
||||||
|
{ 0, 0, 0, 0, 0, 5, 0, 0 },
|
||||||
|
{ 1, 1, 1, 1, 1, 1, 1, 1 },
|
||||||
|
};
|
||||||
|
|
||||||
|
double magnitudes[3] = { 0 };
|
||||||
|
|
||||||
|
calculate_magnitudes(vectors, 3, magnitudes);
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
printf("magnitude %d: %.2lf\n", i, magnitudes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
src/p10.c
Normal file
16
src/p10.c
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include "p10.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
void calculate_magnitudes(double (*vectors)[8], int num_vectors, double* out_magnitudes) {
|
||||||
|
for (int i = 0; i < num_vectors; ++i) {
|
||||||
|
double sum_squares = 0.0;
|
||||||
|
|
||||||
|
for (int n = 0; n < 8; ++n) {
|
||||||
|
sum_squares += pow(vectors[i][n], 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
out_magnitudes[i] = sqrt(sum_squares);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user