SINE implementation

Telechargé par Jaylow Boyd
PAPUA NEW GUINEA UNIVERSITY OF TECHNOLOGY
ELECTRICAL AND COMMUNICATIONS ENGINEERING
DEPARTMENT
YEAR: 2 SEMESTER: 1, 2025
INTRO. TO C PROGRAMMING LAB: 2
NAME: JORDAN KAMESO
ID#: 2330010
COURSE: BEEL/2
INSTRUCTOR: MR. C. PEK
LECTURER: DR. D. CHEN
Part 1: Implementing the Sine Function
You need to implement the sine function using the Taylor series:
Here’s the C code to approximate sin(x) using the first few terms:
#include <stdio.h>
// Function to calculate power
double power(double base, int exp) {
double result = 1.0;
for (int i = 0; i < exp; i++) {result *= base;}
return result;
}
// Function to calculate factorial
long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++)
{fact *= i;}
return fact;
}
// Function to approximate sine using Taylor series
double sin_approx(double x, int terms) {
double sum = 0.0;
for (int i = 0; i < terms; i++) {
int exponent = 2 * i + 1;
double term = power(x, exponent) / factorial(exponent);
if (i % 2 == 1) {term = -term;}
sum += term;
}
return sum;
}
// Main function for testing
int main() {
double x;
printf("Enter angle in radians: ");
scanf("%lf", &x);
int terms = 10; // Adjust the number of terms for precision
printf("Approximated sin(%.5f) = %.5f\n", x, sin_approx(x, terms));
return 0;
}
What This Code Does
1. power() computes xnx^n using a loop.
2. factorial() calculates the factorial of a number.
3. sin_approx() computes sin(x) using Taylor series with a given number of terms.
4. The main() function takes input from the user, calls sin_approx(), and prints the
result.
Part 2: Testing the Function
To test if sin_approx() works correctly, compare it with the math.h sin() function.
Modify your main() function:
#include <math.h> // Include math.h for the standard sin() function
int main() {
double x;
printf("Enter angle in radians: ");
scanf("%lf", &x);
int terms = 10;
double approx = sin_approx(x, terms);
double actual = sin(x); // Standard C sine function
printf("Approximated sin(%.5f) = %.5f\n", x, approx);
printf("Actual sin(%.5f) = %.5f\n", x, actual);
printf("Error = %.10f\n", fabs(approx - actual)); // Show error
return 0;
}
Test Cases
Try running the program with these values:
Small angles: 0, π/6 (0.5236), π/4 (0.7854), π/3 (1.0472), π/2 (1.5708)
Larger values: 3π/2,
Negative values: -π/3, -π/2
Part 3: Debugging Code
Your lab provides this incorrect code:
#include <stdio.h>
int main() {
int x = 5;
int y = 0;
printf("The value of x is: %d\n", x)
int z = x / y;
return 0;
}
Errors & Fixes
1. Missing semicolon in printf()
o Fix: printf("The value of x is: %d\n", x);
2. Division by zero (int z = x / y;)
o Fix: Check if y is zero before division.
Fixed Code:
#include <stdio.h>
int main() {
int x = 5;
int y = 0;
printf("The value of x is: %d\n", x);
if (y != 0) {
int z = x / y;
printf("Result of division: %d\n", z);
} else {
printf("Error: Division by zero is not allowed.\n");
}
return 0;
}
Answers to the Lab Questions
1. What are the three most important things to generate correct code with ChatGPT?
1. Use clear and specific prompts (e.g., "Implement sine function using Taylor series
in C").
2. Understand and verify the output (ChatGPT might generate incorrect code, so test
it).
3. Iterate and refine (Ask ChatGPT to improve precision, fix errors, or optimize
performance).
2. Why is it important to test the generated code?
Generated code might have logic errors or edge cases ChatGPT didn’t consider.
Testing ensures the implementation is accurate and meets the required precision.
Helps identify hidden bugs or inefficiencies.
3. Why is it still essential to learn to write code manually?
Debugging skills: Understanding code helps in fixing errors when AI generates
incorrect results.
Efficiency & Optimization: AI-generated code may not be optimized for speed or
memory.
Real-world application: Many programming tasks require creativity and problem-
solving, which AI alone cannot handle.
1 / 4 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!