Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

C PROGRAMMING  HELP

I'm trying to get a portion of my script to work as a udf inside of a header file that already contains two other udfs that are working properly. 

Here is the script with // lines saying the start and end of the portion i want to be a udf in the header file that I will also paste below the script.

Script:

#include <stdio.h>
#include <math.h>
#include "PROJECT HEADER2.h"    

int main()
{
    // Open the input file
    FILE *input_file = fopen("Data.txt", "r");
    if (input_file == NULL) {
        printf("Error: could not open input file\n");
        return 1;
    }

    // Open the output file
    FILE *output_file = fopen("Result.txt", "w");
    if (output_file == NULL) {
        printf("Error: could not open output file\n");
        fclose(input_file);
        return 1;
    }
//EVERYTHING STARTING HERE
    // Read the input file and compute coefficients for each line
    int line_count = 0;
    double a1, a2, y0, y_prime0, lambda1, lambda2, C1, C2, C_alpha, theta;
    while (fscanf(input_file, "%lf %lf %lf %lf", &a1, &a2, &y0, &y_prime0) == 4) {
        // Compute the roots of the equation
        double discriminant = a2 * a2 - 4 * a1;
        // Complex roots
        if (discriminant < 0) {     
            lambda1 = -a2 / (2 * a1);
            lambda2 = sqrt(-discriminant) / (2 * a1);
        // Repeated real roots
        } else if (discriminant == 0) {     
            lambda1 = lambda2 = -a2 / (2 * a1);
        // Distinct real roots
        } else { 
            lambda1 = (-a2 + sqrt(discriminant)) / (2 * a1);
            lambda2 = (-a2 - sqrt(discriminant)) / (2 * a1);
        }
//AND ENDING HERE I WANT IN HEADER FILE
        // Compute coefficients and write output to file based on type of roots
        compute_coefficients(lambda1, lambda2, &C1, &C2, &C_alpha, &theta);
        fprintf(output_file, "%d %lf %lf\n", 
            (lambda1 == lambda2) ? 2 : ((fabs(lambda1 - lambda2) < 1e-10) ? 1 : 3),
            (lambda1 == lambda2) ? C1 : ((fabs(lambda1 - lambda2) < 1e-10) ? C1 : C_alpha),
            (lambda1 == lambda2) ? C2 : ((fabs(lambda1 - lambda2) < 1e-10) ? C2 : theta)
        );
        display_output(lambda1, lambda2, C1, C2, C_alpha, theta);
        line_count++;
    }

    // Close the input and output files
    fclose(input_file);
    fclose(output_file);

    return 0;
}

Header File:

#ifndef PROJECT_HEADER2_H

#define PROJECT_HEADER2_H

//Calculation function
void compute_coefficients(double lambda1, double lambda2, double *C1, double *C2, double *C_alpha, double *theta)
{
    if (lambda1 == lambda2) { 
    // Repeated real roots
        *C1 = 1;
        *C2 = 0;
        *C_alpha = lambda1;
        *theta = 0;
    } else if (fabs(lambda1 - lambda2) < 1e-10) { 
    // Distinct real roots (with rounding tolerance)
        *C1 = 1;
        *C2 = lambda1;
        *C_alpha = 0;
        *theta = 0;
    } else { 
    // Distinct real roots
        *C1 = (lambda2 - 1) / (lambda2 - lambda1);
        *C2 = (1 - lambda1) / (lambda2 - lambda1);
        *C_alpha = (lambda1 - lambda2) / (lambda2 - 1);
        *theta = atan2(1 - lambda1, lambda2 - 1);
    }
}
//Print function
void display_output(double lambda1, double lambda2, double C1, double C2, double C_alpha, double theta)
{
    printf("lambda1 = %lf\n", lambda1);
    printf("lambda2 = %lf\n", lambda2);
    printf("C1 = %lf\n", C1);
    printf("C2 = %lf\n", C2);
    printf("C_alpha = %lf\n", C_alpha);
    printf("theta = %lf\n", theta);
}
#endif

Thank you for your help!

Expert Solution
Check Mark
Still need help?
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

That didn't quite work for me, can you do a demonstration please?

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

That didn't quite work for me, can you do a demonstration please?

Solution
Bartleby Expert
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education