1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

array code has syntax errors, what are they?

Discussion in 'C' started by GirlyGeek, Oct 31, 2012.

  1. GirlyGeek

    GirlyGeek New Member

    Joined:
    Oct 31, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I'm new to programming and am taking a C programming which is teaching me a lot on my own, but is still difficult. I do a lot of troubleshooting to figure out what the issues are but whenever it's syntax errors, I'm often at a loss because I can't detect what they are. Here's my code as of now:
    Code:
    int array[5];
    int maxEl,minEl,i;
    
    for
    {
       (int a=1; a< 5; a++)
       scanf( "%d", &array[a] )
    }
    
    int main()
    {
        
    printf( "Please input 5 numbers: " );
    
    }
    
    for(i = 1;i < 5;i++)
    {
    if(array[i] > maxEl)
    maxEl = array[i];
    if(array[i] < minEl)
    minEl = array[i];
    }
    
    printf("Diff: %d\n",diff( maxE1, minE1));
    
    
    int diff(int maxEI, int minEI)
    {
    return maxEI - minEI;
    }
    {
    system("pause");
    
    }
    
    
    I want to be able to find the difference between the smallest and largest element of an array of 5. What should I start doing differently? Thank you!
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    if you're compiling as C code, you need to declare any functions before they're called. You can this by using a prototype or put the function itself before main.

    Code:
    #include <stdio.h>
    
    int foo(void); // function prototype
    int bar(int a) { // function
       return a + a;
    }
    
    int main() {
       printf("foo = %d\nbar = %d",
                foo(), bar(5));
    }
    
    int foo(void) { // function definition
       return 10;
    }
    the for loop at the start of your program is an error. put it inside main or some other function.

    in C, arrays are indexed starting at position 0 through size - 1.

    Code:
    #include <stdio.h>
    
    // declare functions or function prototypes
    
    int main() {
    
       // declare variables
    
       // ask for user input
    
       // loop through array to find min and max
       // find and/or print the diff
    }
    
    // define any function prototypes here
    
    hope that helps
     
  3. GirlyGeek

    GirlyGeek New Member

    Joined:
    Oct 31, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Ah, I see my error. I forgot to add the ";" to declare it as a function prototype.
     

Share This Page