Click here to Skip to main content
15,887,288 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write function with prototype:
double mySin(double x, double epsi);
which calculates and return the value sin(x) approximately by the formula:
sin(x) = - + -... +(-1)n
here n is the first integer for which | |  epsi is satisfied.

What I have tried:

   double q3() {
       int x;
       int n = 1;
       double p = 0;
       int dau = -1;
       int tongquat2 = 1.0 / (2 * n + 1);
       while (tongquat2 >= 1e-10) {
           tongquat2 = 1.0 / (2 * n + 1);
           p += -1 * dau * pow(-1, n) * pow(x, 2 * n + 1) / (giaithua(2 * n + 1));
           dau *= -1;
           n++;
       }
       return p;
   }
int main(){
   int x;
   scanf("%d", &x);
   printf("sin(%d)= %.6lf", x, q3());
}

why code is not correct? help me the erors.
Posted
Updated 30-Jan-24 6:45am
v2
Comments
CPallini 30-Jan-24 5:39am    
The info you provided is not complete. The stop condition formula ('here n is the first integer...') is not visible. The mySyn function is not defined. You didn't show the implementation of giaithua and so on.

There are three types of errors that could be involved here, and we have no idea which it is:
1) Compilation / linking errors: you try to compile your app and get problems reported before it can run. If I try to compile your code as shown, I have to add these two lines:
C
#include <stdio.h>
#include <math.h>
and also add a "dummy definition" of the giaithua function to get a clean compilation of the code you show.
Until you have a clean compilation, you do not produce an EXE file, and your app cannot run.
2) Run time errors: these stop your app from continuing because something happened that the system can't cope with - a "divide by zero" error perhaps, or an attempt to read a file that doesn't exist for example. =The system displays an error message to you, and kills your app immediately.
We can't help you fix that without knowing what the message was, where it occurred, and what data you entered to get it.
3) Logic errors: these mean you get unexpected results but your app doesn't crash. FOr example, if you expect you app to give a result of 42 but it gives you 666 instead.
We can't help you fix that without knowing what the value was, what you expected to get, when you got it, and what data you entered to get it.

I'm going to assume its one of the last two, and the process for finding the problem needs your code running with your data - neither of which we have. Compiling does not mean your code is right! :laugh:

So it's going to be up to you, but you aren't on your own.
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Without having to take a closer look at the q3() function, it is already clear that it cannot calculate a sin() because no parameter is passed.
C
int main() {
    int x;
    scanf("%d", &x);
    printf("sin(%d)= %.6lf", x, q3());
}


//edit:
At first glance, the compiler warns you about this point:
C
int n = 1;
int tongquat2 = 1.0 / (2 * n + 1);
while (tongquat2 >= 1e-10) { ...

Conversion from "double" to "int", possible data loss!
The data types involved are unsuitable and an arithmetic overflow can also occur.

Even if giaithua() would return a meaningful value, it is noticeable that x is not initialized.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900