0

me and my amateur code again :$ This time if works somewhat fine, but gives a error table. any ideas mates?

#include <iostream>
using namespace std;

long fib_sk(int);
int fib_mas(int);
//===========================
int main()
{
	int iSk;
	cout<< "element count- "; cin>> iSk;
	if (iSk<0) { cout<< "need positive value "; return 0; }
	cout<< "fibonacci number array till " << iSk << "th element:" <<endl;
	cout<< fib_mas(iSk) <<endl;

	return 0;
}
//===========================
long fib_sk(int n)
{
	if (n<2) { return n; }
	else { return fib_sk(n-1) + fib_sk(n-2); }
}
//============================
int fib_mas(int m)
{
	int *iMas;
	iMas=new int(m);

	for (int i=1; i<=m; i++)
	{
		iMas[i]=fib_sk(i);
		cout<< iMas[i] << " ";
	}
	return 0;
}
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Lensva
0

>iMas=new int(m);
You need to use square brackets instead of parens:

iMas=new int[m];

>for (int i=1; i<=m; i++)
Arrays are zero-based in C++. That means you start at 0 and stop at n - 1 or you'll be accessing memory that you're not allowed to access:

for (int i=0; i<m; i++)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.