need
Print the cosine curve to the console.
Cosine curve, also called cosine wave (cosinwave), is a curve derived from the cosine ratio in mathematical trigonometric functions. Also representative of an analog signal, as opposed to a square wave which represents a digital signal.
The standard pure cosine function formula is:
example code
/**
* @author: 冲哥 @C语言中文社区
* @date: 2023/02/14
* @description:
*/
#include <stdio.h>
#include <math.h>
#define PI 3.141592653
#define BIG_SIZE 10 //图形放大的倍数
int main() {
int x;//x轴
double y;//y轴
int m;
for (y = 1; y >= -1; y -= 0.1) {//y的取值范围是-1~1,这里设置步长为0.1
m = acos(y) * BIG_SIZE;//计算y对应的横坐标的值
for (x = 1; x < m; x++) {
printf(" ");//打印*之前的空格
}
printf("*");//打印*
for (; x < 2 * PI * BIG_SIZE - m; x++) {//打印对称面的空格和*
printf(" ");
}
printf("*\n");
}
}
operation result
code analysis
The arccosine function acos() is used to draw the cosine curve. #include <math.h>
The header file needs to be introduced in the code. acos(y) calculates the value of the abscissa through the value of the ordinate, determines the value of the abscissa, and the abscissa value of its symmetrical position It can also be determined that the abscissa value obtained by 2 * PI
subtracting, in order to make the printed curve more intuitive, the value of the x-axis in the code is enlarged by 10 times.