Thread: Arrays

  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Rep Power


    #include <iostream>
    using namespace std;
    void main();
    int max;
    	int a[max]; bool ok;
    	int n; float avg;
    	getdata (a[];n;
    	ok=linearsearch(a[]n, item);
    	if (ok) cout<<"yes":
    void reverse( int r[], int nn);
    	int i;
    	for (i=(n-1); i>=0;i--)
    		float average(in r[]; int n)
    			int i; int sum;//sum=0
    			for (i=0;sum=0; i<<nn; i++)
    			float temp sum;//floattemp= float (sum);
    			return (temp/nn);
    			sort (a[], n;
    			void sort (int r[] int nn)
    				int [i]
    					for (j=0; j<(nn-1); j++)
    						if (r[j+1]j<r[j]
    							int T=r[j];
    		}//for j
    	}//for i
    2 errors: Lines 12 and 31 both have the same error...
    error C2447: missing function header (old-style formal list?)

    *edit* added code tags
    Last edited by Onslaught; September 27th, 2003 at 02:31 PM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Rep Power
    You've got quite a few things screwed up so that, frankly, it's a mess. I assume that you are just starting to learn C++. Then to throw in a confusion factor, the compiler misinterpreted one of your mistakes.

    The main error which as indicated by the title is that you are passing the array to the function incorrectly. The declaration of the function reverse and sort contain the correct syntax: an array parameter is declared using square brackets; e.g. int r[] . But then when you call that function and pass the array, you only use the array's name; e.g. output(a,n);. Just use the array name to pass an array to a function.

    The rest is just a mess. You have left off closing parentheses from function calls. It appears that you have a new function, sort, being declared within another function, reverse. remember that parentheses always come in pairs and that the closing parenthesis must occur before the end of the statement.

    BTW, the semicolon marks the end of the statement. So you don't want something like this:
    getdata (a[];n;
    First, no closing parenthesis for the getdata function call. Second, the statement n; is meaningless.

    You are also putting a semicolon after the function header and before the body, which is incorrect. And that int max; between the main function header and the function body appears reminiscent of the old K&R syntax from the earliest days of C, in which the parameter list was placed between the function header and body. Don't use it. Here is how a function should be defined:
    void main(void)
        // place the body of the main function here
    void reverse( int r[], int nn)
        // place the body of the main function here
    Notice no semicolon after the function header. Also notice that nothing goes between the function header and the opening brace. And notice that for each and every open brace there is a corresponding close brace, such that the function body must end with a close brace that pairs up with the open brace that starts the function body and that this must be done before you can define another function.

    Also notice that I used code tags to retain the formatting of my code. Please do likewise when you post. It keeps your code readable and it keeps us from having to tell you to use them. Read the sticky note at the top of this forum. They recommend php tags, but I prefer code tags. Remember, if you make it unnecessarily difficult for us to help you, then we won't.

    Go through your code and clean it up. Make sure everything matches up; ie parentheses and braces and brackets. Make sure you put semicolons where you need them and don't where you don't.

    Oh, and also, that int max needs to be a const and it needs to have a value assigned to it, especially if you are going to use it to declare the size of an array; the compiler needs to know that value at compile time.
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Rep Power
    There is no way on earth that only generated two errors! If if did, throw away your compiler. MinGW/GCC 3.2 produced this:

    main.cpp:3: `main' must return `int'
    main.cpp:6: parse error before `{' token

    main.cpp:10: ISO C++ forbids declaration of `n' with no type
    main.cpp:10: redefinition of `int n'
    main.cpp:9: `int n' previously declared here
    main.cpp:11: `a' was not declared in this scope
    main.cpp:11: parse error before `]' token
    main.cpp:12: ISO C++ forbids declaration of `getdata' with no type
    main.cpp:13: `a' was not declared in this scope
    main.cpp:13: parse error before `]' token
    main.cpp:14: ISO C++ forbids declaration of `reverse' with no type
    main.cpp:15: syntax error before `<<' token
    main.cpp:16: ISO C++ forbids declaration of `item' with no type

    main.cpp:17: ISO C++ forbids declaration of `ok' with no type
    main.cpp:17: conflicting types for `int ok'
    main.cpp:8: previous declaration as `bool ok'
    main.cpp:17: `a' was not declared in this scope
    main.cpp:17: parse error before `]' token
    main.cpp:25: parse error before `{' token
    main.cpp:27: syntax error before `>=' token
    main.cpp:27: syntax error before `--' token
    main.cpp:30: `in' was not declared in this scope
    main.cpp:30: parse error before `[' token
    cc1plus.exe: warnings being treated as errors
    main.cpp: In function `float average(...)':
    main.cpp:33: warning: suggest parentheses around assignment used as truth value
    main.cpp:33: `nn' undeclared (first use this function)
    main.cpp:33: (Each undeclared identifier is reported only once for each function it appears in.)

    main.cpp:33: parse error before `;' token
    main.cpp:35: `r' undeclared (first use this function)
    main.cpp:36: parse error before `}' token
    main.cpp:37: parse error before `;' token
    main.cpp:40: `a' undeclared (first use this function)
    main.cpp:40: parse error before `]' token
    main.cpp:41: parse error before `int'
    main.cpp:44: `j' undeclared (first use this function)
    main.cpp:44: parse error before `)' token
    main.cpp:48: parse error before `=' token
    main.cpp: At global scope:
    main.cpp:49: ISO C++ forbids declaration of `r' with no type
    main.cpp:49: `t' was not declared in this scope
    main.cpp:49: assignment (not initialization) in declaration
    main.cpp:50: parse error before `}' token

    You seem to be posting at every little problem you come across today, and are even repeating the same errors in different posts! Fix some of the stupid errors and typos first before posting. Difficult problems are one thing, but posting code that isn't even close to valid C/C++ is not helping your cause.


IMN logo majestic logo threadwatch logo seochat tools logo