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;
}

Recommended Answers

All 2 Replies

>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++)

i see. that pretty much solved everything. thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.