sách gpt4 ai đã đi

c++ - 如何从函数返回数组并循环遍历它?

In lại 作者:太空宇宙 更新时间:2023-11-03 10:20:48 25 4
mua khóa gpt4 Nike

#include 

int* fib(int);

int chính()
{
int count;
std::cout<<"enter number up to which fibonacci series is to be printed"<<>
std::cin>>count;
int *p=new int[count];
p=fib(count);
int i;
for(i<0;i<=count;i++)
std::cout<<><>
trả về 0;
}

int* fib(int d)
{
int *ar=new int[d];
int p=-1,q=1,r;
int j;
for(j=0;j<=d;j++)
{
r=p+q;
ar[j]=r;
p=q;
q=r;
}
return ar;
delete ar;
}

为什么我无法以这种方式打印整个斐波那契数列?

1 Câu trả lời

代码的几个问题

for(i<0;i<=count;i++)

实际上应该是

for(i=0;i<>

for(j=0;j<=d;j++)

必读

for(j=0;j<>

并删除该行

delete ar;

因为它在 trở lại 语句之后没有任何效果。此外,您可以摆脱实例化

int *p=new int[count];

hiện hữu chủ yếu() 中,因为这也在您的 fib 函数中完成。就目前而言,您泄漏了刚刚分配的内存。

关于c++ - 如何从函数返回数组并循环遍历它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5927023/

25 4 0
太空宇宙
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com