Answers to Tan Haoqiang's C language design exercises (3)

Table of contents

Analyze the following program: 

To solve the equation 2*x^3-4*x^2+3*x-6=0 using the bisection method, you need to define a function that evaluates the equation and finds the roots within a given interval. Here is an example program in C that uses the bisection method to solve an equation:



Analyze the following program: 

#include<stdio.h>

int main(){

    char c1,c2;

    c1=97;

    c2=98;

    printf("c1=%c,c2=%c\n",c1,c2);

    printf("c1=%d,c2=%d\n",c1,c2);

    return 0;

}

(1) What information will be output when running? Why?

(2) If lines 4 and 5 of the program are changed to

c1=197;

c2=198;

What information will be output when running? Why?

(3) If line 3 of the program is changed to

int c1,c2;

What information will be output when running? Why?


This C program involves character variables and ASCII encoding. Let's analyze each situation:

**(1) Original program code:**
```c
#include<stdio.h>

int main(){

    char c1,c2;

    c1=97;

    c2=98;

    printf("c1=%c,c2=%c\n",c1,c2);

    printf("c1=%d,c2=%d\n",c1,c2);

    return 0;

}
```

This program first defines two variables of type char `c1` and `c2`, and then assigns them to 97 and 98 respectively. Next, it uses the `printf` function to output the character and integer values ​​of these two variables.

The output information will be as follows:

```
c1=a,c2=b
c1=97,c2=98

```

This is because the ASCII character corresponding to 97 is 'a', and the ASCII character corresponding to 98 is 'b'. Therefore, the character values ​​of `c1` and `c2` are 'a' and 'b', and the integer values ​​are 97 and 98.

**(2) Change lines 4 and 5 of the program to:**
```c
c1=197;
c2=198;
```

In this case, the program's output will change:

```
c1=Å,c2=Æ
c1=-59,c2=-58

```

This is because in ASCII encoding, the character corresponding to 197 is 'Å', and the character corresponding to 198 is 'Æ'. Character variables actually store ASCII code values ​​when stored. Therefore, the integer values ​​of `c1` and `c2` are -59 and -58 respectively.

**(3) Change line 3 of the program to:**
```c
int c1,c2;
```

If you change the data type of `c1` and `c2` to `int`, the program will no longer output as characters, but as integers. The output information will be as follows:

```
c1=97,c2=98
c1=97,c2=98

```

This is because now `c1` and `c2` are integer variables of type `int` and they will be output as integers and no longer interpreted as characters. Therefore, the values ​​of `c1` and `c2` are still 97 and 98, respectively, output as integers.

Use the bisection method to find the roots of the following equation between (-10, 10): 2*x^3-4*x^2+3*x-6=0


To solve the equation 2*x^3-4*x^2+3*x-6=0 using the bisection method, you need to define a function that evaluates the equation and finds the roots within a given interval. Here is an example program in C that uses the bisection method to solve an equation:

```c
#include <stdio.h>
#include <math.h>

// Function that defines the equation
double equation(double x) {     return 2 * pow(x, 3) - 4 * pow(x, 2) + 3 * x - 6; }

int main() {     double a = -10.0; // Left boundary     double b = 10.0; // Right boundary     double epsilon = 1e-6; // Error tolerance


    // Use bisection method to find roots
    while (fabs(b - a) > epsilon) {         double mid = (a + b) / 2;         double f_mid = equation(mid);

        if (f_mid == 0) {             printf("A root of the equation is: %.6lf\n", mid);             break;         } else if (f_mid * equation(a) < 0) {             b = mid;         } else {             a = mid;         }     }







    printf("An approximate root of the equation is: %.6lf\n", (a + b) / 2);

    return 0;
}

```

In this program, we first define the function of the equation 2*x^3-4*x^2+3*x-6, and specify the interval (-10, 10) and the tolerance error epsilon. We then use the bisection method to find the roots of the equation, iterating until the length of the interval is less than the tolerance error. Eventually, the program will output an approximate root.

Note that this method assumes that the equation has exactly one root in the given interval. If the equation has multiple roots, you may need to modify it depending on the situation.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132957194