Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // Structs definition
- typedef struct Point {
- float x;
- float y;
- } point;
- typedef struct Line {
- float a;
- float c;
- float from;
- float to;
- } line; //f(x) = ax + c in [from, to]
- // Global variables definition
- line parts[3];
- // Functions defintion
- line solver(point, point);
- float solution(float);
- int main() {
- point Data[4] = {{0, 0}, {15, 20}, {27, 55}, {35, 100}};
- for (int i=0; i<3; i++) {
- parts[i] = solver(Data[i], Data[i+1]);
- }
- float x;
- scanf("%f", &x);
- printf("%f\n", solution(x));
- return 0;
- }
- // Creates each part of the function
- line solver(point p, point q) {
- float a = (q.y - p.y)/(q.x - p.x);
- float c = p.y - a*p.x;
- line f = {
- .a = a,
- .c = c,
- .from = p.x,
- .to = p.y,
- };
- return f;
- }
- // This is The Function
- float solution(float x) {
- for (int i=0; i<3; i++) {
- line p = parts[i];
- if (x>=p.from && x<=p.to) {
- return (p.a*x + p.c);
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement