a Problem about two dimension arrays and Fibonacci

This is a discussion on a Problem about two dimension arrays and Fibonacci within the C Programming forums, part of the General Programming Boards category; Hi There, I Would like to ask for your help about two diferent programs that donīrun properly, one is using ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Unhappy a Problem about two dimension arrays and Fibonacci

    Hi There, I Would like to ask for your help about two diferent programs that donīrun properly, one is using a recursive function, I must print the numbers of the Fibonacci Serie, and in the other I mustcreate a two dimensional array which prints under the main diagonal only 0,

    here is the code for the first one;
    Code:
    int fibo (int);
    void main(){
    	int a, d;
    	printf("How many Numbers shall it print? "); 
    	scanf("%d", &a);
    
    	d=fibo (a);
    	printf("%d", d);
    	}
    
    /*int fibo (int uno int f){
    	int b, c=0, d=1;
    	printf("1");
    	for(b=1; b<=f; b++){
    
    	 c=c+d;
    	 d=d+c;
    
    	 printf(", %d, %d", c, d);
    	 }*/
    
    int fibo (int a){
    
    	int static uno=1;
    
    	 if (a>=1)
         uno=fibo(a);
    	  printf("\n%d", uno);
    
    	 if (a==0){
    	  return 1;
    
    	  }
    
    	 else{
    	 printf("\n%d ",uno);
    	 return (uno+ (fibo(a-1)));
    }
    	 }
    If Anyone Can help me at least giving me an Idea on how to make it work i would be very thankful, the main problem is that chen I call the fuction again, it should add a value for num equal to a number of the serie, and i thought y had it done when i placed, but it does nothing, actualyy it says it causes a failure in the internal battery or something like that, so any help will be well recieved.



    THe other code is correct in logic, what happens is that every time I run it, it completely closes c++, and i have no Idea why, if anyone can copy it and run it to see if it is general or itīs only my computer, thanks, however, even a comment on why is it doing this could be very helpful.
    ok, hereīs the code:

    Code:
    #include <stdio.h>
    
    void main(){
    
    int a, b, ren, col, mat[100][100];
    
    printf("line ");
    scanf("%d", &a);
    
    printf("column ");
    scanf("d", &b);
    
    
    for( ren=0;ren<b;ren){
     for (col=0; col<a;col++){
      scanf("%d", &mat[ren][col]);
    	 if (ren>col)
    	 mat[ren][col]=0;
    	 }
      }
    
    
      for( ren=0;ren<b;ren){
    	for (col=0; col<a;col++){
    	 printf("%d", &mat[ren][col]);
    	 }
    	 printf("\n");
    	 }
      }



    THANKS FOR TAKING THE TIME!!!!!!!

  2. #2
    Registered User
    Join Date
    Nov 2003
    Posts
    46

    Fibonacci

    I did a google search for "fibonacci recursive" and found a good pdf.

    http://www.comp.leeds.ac.uk/cs12/Han...-Fibonacci.pdf

    Then I wrote the following code that seems to work just fine.

    Code:
    #include <stdio.h>
    
    int fibo (int a){
    	if (a <= 1){
    		return a;
    	} else {
    		return fibo(a-1)+fibo(a-2);
    	}
    } /* fibo */
    
    int main(void){
    	int a;
    	
    	printf("How many Numbers shall it print? "); 
    	scanf("%d", &a);
    	
    	for(;a>0;a--){
    		printf("%d\n", fibo(a));
    	}
    
    	return 1;
    } /* main */

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    I don't really understand what you are supposed to be doing with the 2D array but

    for( ren=0;ren<b;ren)

    should probably be

    for( ren=0;ren<b;ren++)

    and this happens in two places.

    Peter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Ansi C Fibonacci code stinks! obvious bug?
    By Hansie in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 01:29 AM
  2. I hate Arrays with a passion....
    By cmangel518 in forum C++ Programming
    Replies: 0
    Last Post: 05-01-2002, 03:49 PM
  3. fibonacci numbers?????
    By help!! in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2001, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21