Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Theoretische Informatik (http://www.informatikerboard.de/board/board.php?boardid=5)
--- Fibonacci-Zahlen Programm (http://www.informatikerboard.de/board/thread.php?threadid=2667)


Geschrieben von Lars am 05.12.2015 um 23:34:

  Fibonacci-Zahlen Programm

Meine Frage:
Hi, bin gerade mehr oder weniger am verzweifeln, weil ich nicht nachvollziehen kann, wo das Problem liegt.

Meine Ideen:
Ich häng bei dem ersten Teil der Aufgabe... Mein Programm:
#include <stdio.h>
int main()
{
int amax;
int a,b,c;
printf("Welche Zahl? ");
scanf("%u", &amax);
a=1;
b=1;
do
{
c=a+b;
a=b;
b=c;
}
while(a<amax);
return c;
}
Ich versteh nicht, was genau falsch ist. Kann mir jemand weiter helfen? Hab ich die falsche Schleife gewählt?



Geschrieben von Lars am 05.12.2015 um 23:38:

 

#include <stdio.h>
int main()
{
int amax;
int a,b,c;
printf("Welche Zahl? ");
scanf("%u", &amax);
a=1;
b=1;
do
{
printf("%i,",a);
c=a+b;
a=b;
b=c;
}
while(a<amax);
}
Sorry, das war das falsche Programm... Ich meine das hier.



Geschrieben von eulerscheZahl am 06.12.2015 um 07:46:

 

Was passt denn nicht?
Sieht auf den ersten Blick doch ok aus.
code:
1:
2:
Welche Zahl? 14
1,1,2,3,5,8,13,



Geschrieben von Lars am 06.12.2015 um 10:56:

 

Ich bin dämlich, stimmt passt. Ich weiß nicht, was ich gestern falsch gemacht habe... Danke dir, war wohl schon etwas spät smile .



Geschrieben von Lars am 06.12.2015 um 11:07:

 

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
#include <stdio.h>
int main()
{
	int amax;
	int a,b,c;
	printf("Welche Länge? ");
	scanf("%u", &amax);
	a=1;
	b=1;
	do
		{ 
		printf("%i, \n",a);
		c=a+b;
		a=b;
		b=c;
		amax--;
		}
	while(amax>0);
}

So hab ichs nun erweitert für beliebiger Länge, gibt's noch ne elegantere Lösung?



Geschrieben von eulerscheZahl am 06.12.2015 um 15:23:

 

Ich hätte es so gemacht:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
#include <stdio.h>
int main()
{
	int a,b,c, i, n;
	printf("Welche Länge? ");
	scanf("%u", &n);
	a=1;
	b=1;
	for (i = 0; i < n; i++)
	{ 
		printf("%i\n", a);
		c=a+b;
		a=b;
		b=c;
	}
}


Forensoftware: Burning Board, entwickelt von WoltLab GmbH