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!
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