Thread: Basic question: some unknown error

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    103

    Basic question: some unknown error

    Code:
    #include <stdio.h>
    /*  C program to print the first n Fibonacci Numbers. */
    void main(void){
      int k,n;
     
      printf("How many Fibonacci number do you want listed? ");
      scanf("%i",&n);
      int  fib[n] = {1,1};
      for (k = 2; k < n; k++) /* the ++ is the increment operator */
    	fib[k] = fib[k-1] + fib[k-2];
      /* print the results, four per line */
      for (k = 0; k < n;++k)
      {
      if (k % 4 == 0)
    	printf("\n");
      printf("%8i", fib[k]); /* that’s 8 ints */
      } /* end of for loop */
      printf("\n");
    } /* end of main */
    That's just a basic program that was made to print Fibonacci numbers. I modified it a bit. When I compile it, I get the error "error: variable-sized object may not be intialized" what does this mean?
    Last edited by Poincare; 01-21-2009 at 08:01 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey here's the other way:
    Code:
    #include <stdio.h>
    
    int fib (int num) {
    	int x=-1, y=-1;
    	if (num <= 2) {
    		return 1;
    	}
    	x=fib(num-1); y=fib(num-2);
    	return x+y;
    }
    
    int main (int argc, char *argv[]) {
    	printf("answer: %d\n", fib(atoi(argv[1])));
    	return 0;
    }
    It doesn't print the sequence but you could use it that way, eg.
    [root~/C/models] ./a.out 20
    answer: 6765
    Last edited by MK27; 01-21-2009 at 08:20 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Poincare View Post
    When I compile it, I get the error "error: variable-sized object may not be intialized" what does this mean?
    I imagine it involves this:
    Code:
    printf("How many Fibonacci number do you want listed? ");
      scanf("%i",&n);
      int  fib[n] = {1,1};
    Here's another one that does the sequence:
    Code:
    #include <stdio.h>
    
    int main () {
    	int a=0, b=1, n=a+b,i,ii;
    	puts("Number of iterations");
    	scanf("%d",&ii);
    	printf("%d %d ",a,b);
    	for (i=0;i<ii;i++) {
    		n=a+b;
    		printf("%d ",n);
    		a=b;b=n;
    	}
    }
    [root~/C/models] ./a.out
    Number of iterations
    12
    0 1 1 2 3 5 8 13 21 34 55 89 144 233
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Poincare View Post
    Code:
    #include <stdio.h>
    /*  C program to print the first n Fibonacci Numbers. */
    void main(void){
      int k,n;
     
      printf("How many Fibonacci number do you want listed? ");
      scanf("%i",&n);
      int  fib[n] = {1,1};
      for (k = 2; k < n; k++) /* the ++ is the increment operator */
    	fib[k] = fib[k-1] + fib[k-2];
      /* print the results, four per line */
      for (k = 0; k < n;++k)
      {
      if (k % 4 == 0)
    	printf("\n");
      printf("%8i", fib[k]); /* that’s 8 ints */
      } /* end of for loop */
      printf("\n");
    } /* end of main */
    That's just a basic program that was made to print Fibonacci numbers. I modified it a bit. When I compile it, I get the error "error: variable-sized object may not be intialized" what does this mean?
    It means what it says -- the variable sized object fib[n] cannot be initialized. Just do fib[0] = fib[1] = 1 after it, making sure of course that some idiot user didn't type -147 for n first.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    [root~/C/models] ./a.out
    Number of iterations
    12
    0 1 1 2 3 5 8 13 21 34 55 89 144 233
    I sure hope you aren't actually root...

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    I sure hope you aren't actually root...
    Better me than some dumb ass.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    You're 11 years old, and you're playing with Fibonacci numbers?

    That's impressive.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    int main() not void main().

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Poincare View Post
    That's just a basic program that was made to print Fibonacci numbers. I modified it a bit. When I compile it, I get the error "error: variable-sized object may not be intialized" what does this mean?
    Code:
      int  fib[n] = {1,1};
    It is this line - since fib[n] is defined by the user-entered size (which, by the way, could be less than 2), the compiler can't produce the code to fill fib[].
    You could do
    Code:
    int fib[n];
    fib[0] = 1;
    fib[1] = 1;
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM