我提供的这段代码有什么问题?
What's wrong in this code i provided?
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int x[100];
float a,f,t;
cout<<" Enter the amplitude:";
cin>>a;
cout<<"Enter frequency:";
cin>>f;
f=1.0/f;
cout<<"Enter the time interval::";
cin>>t;
int i=0;
while(i<t)
{
x[i] = a * sin(2 * 3.14 * f * t);
cout<<x[i]<<" ";
}
return 0;
}
当我运行它时,我只是得到大量的输出值,都是一样的。例如,输入5
, 7
和9
会得到连续输出:
4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4
:
and so on...
您既没有增加i
也没有减少t
,这意味着您的循环很可能是无限的:
while(i<t)
{
x[i] = a * sin(2 * 3.14 * f * t);
cout<<x[i]<<" ";
}
根据你的需要,在循环体中应该有一个类似i++
的东西。
您可能还会发现每个数组元素都被设置为相同的值,因为您在计算中使用的是t
而不是i
的某个函数。
你需要包含两个标题
#include <iostream>
#include <cmath>
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 0-1背包代码中的错误.我的代码中有什么错误
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 这行代码在C++类中意味着什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 以下 C++ 代码用于 -> "#define idiv(a, b) (((a) + (b) / 2) / (b))" 是什么?
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 此代码中的操作流程是什么?C/C++.
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 此代码验证公式是什么意思?
- 需要以下代码的帮助,下面的代码有什么问题
- C++标准提案代码:什么是 N 和 P?
- C - 哈希算法代码什么都没有返回
- 将"12345678"转换为"1-234-5678",我的代码什么也没产生
- 在编译过程中,琐碎的(没有效果的)代码什么时候会被删除
- 有人能告诉我为什么这个代码什么都不打印吗