arrays - simple problem

This is a discussion on arrays - simple problem within the C Programming forums, part of the General Programming Boards category; Hello, I wrote this simple piece of code. Code: int main(void) int score [100]; score [0] = 5; score [1] ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27

    Lightbulb arrays - simple problem

    Hello,

    I wrote this simple piece of code.
    Code:
    int
    main(void)
    
    int score [100];
    
            score [0] = 5;
            score [1] = 3;
    
    for (i=2; 1<=100; i=i+1)
    score(i) = score[i-1] + score[i-2];
    
    return(0);
    When trying to compile it, i get this error:

    Code:
    flash:~>gcc formula.c -o form
    formula.c: In function `main':
    formula.c:8: error: syntax error before "score"
    flash:~>pico +8 formula.c
    I don't really understand what's wrong with line 8. To me, everything looks fine.

    Any ideas. Thanks

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    change this

    score(i) = score[i-1] + score[i-2];

    to this

    score[i] = score[i-1] + score[i-2];

    and it should compile. its gonna spit out some nasty values
    though because your loop runs through the entire array but only
    two elements are initiallised

    EDIT:

    for (i=2; 1<=100; i=i+1)

    should probably be changed to

    for (i=2; i<=100; i=i+1)
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Don't forget to box in your main.
    Code:
    int
    main(void)
    {
            int score [100];
    
            score [0] = 5;
            score [1] = 3;
    
            for (i=2; 1<=100; i=i+1)
                    score(i) = score[i-1] + score[i-2];
    
            return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Overlapping arrays problem
    By earth_angel in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2005, 10:19 AM
  3. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 08:59 AM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM

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