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

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Red face Finding the max and minimum value in an array


    Hello guys im using the Quincy C compiler and i have not been able to get this code to work. I am supposed to calculate the max and minimum values in an array.Here is my code:
    i always get the error: expected expression before '{' token. on the second last curly bracket.

    #include<stdio.h>
    main()
    {
    int i, j;
    float A[5][4] = {1.2, 0.0, -2.0, 0.35, 5.0, 3.5, 0.75, -1.5, 0, -1.0, 1.0, 2.3, 4.5, 4.25, -3.0, 3.6, 4.0, -0.5, 1.3, 1.68};
    float B[5][2], maxVal, minVal;
    j = 0;
    for (i = 0; i < 5; i++)
    {
    if (A[i][j] > maxVal)
    maxVal = A[i][j];
    if (A[i][j] < minVal)
    minVal = A[i][j];
    for (j = 1;j < 4;j++)
    }
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,182
    Rep Power
    2222
    Please use code tags to preserve your code's formatting. Not doing so makes your code an unreadable mess.

    Here's what your code looks like with code tags:
    Code:
    #include<stdio.h>
    main()
    {
    	int i, j;
    	float A[5][4] = {1.2, 0.0, -2.0, 0.35, 5.0, 3.5, 0.75, -1.5, 0, -1.0, 1.0, 2.3, 4.5, 4.25, -3.0, 3.6, 4.0, -0.5, 1.3, 1.68};
    	float B[5][2], maxVal, minVal;
    	j = 0;
    	for (i = 0; i < 5; i++)
    	{
    		if (A[i][j] > maxVal)
    			maxVal = A[i][j];
    		if (A[i][j] < minVal)
    			minVal = A[i][j];
    		for (j = 1;j < 4;j++)
    		}
    }
    What is that j for-loop for? It has no body. The compiler expected that for-statement to be followed by an open brace or a statement (eg, assignment, function call, control statement). Instead, it was followed by a close brace, which is incorrect syntax. That is what that error is telling you: it expected an open brace, but all you gave it was a lousy close brace.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Please use code tags when posting code.


    I suggest you start using braces {} for all of your control statements.

    Also the initialization of your array is incorrect. Why are you trying to create a two dimensional array and initializing it like a 1 dimensional array?


    Jim
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    4
    Rep Power
    0
    you have a for loop that doesn't do anything

    for (j = 1;j < 4;j++)
    and why did you use a two dimensional array
    one dimension array can do the trick

    this is an example
    Code:
    #include<stdio.h>
    
    int main()
    {
        float A[6] = {0.1, 5.2, 3.7, 9.7, -7.5, -10.15}, MaxVal = 0, MinVal = 0;
        int i;
        for( i = 0; i < 6; i++ )
        {
            if(A[i] > MaxVal)
                MaxVal = A[i];
            if(A[i] < MinVal)
                MinVal = A[i];
        }
        printf("MinVal = %f and MaxVal = %f", MinVal, MaxVal);
        return 0;
    }
    may be wrong to initiate MinVal and MaxVal with 0 but it just for example purpose only
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    Thank you very much


    This is my first time learning C i don't understand my teacher because he has a heavy accent. Thank you all for the help
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    15
    Rep Power
    0
    Hope you are looking to be as such,

    1. initialize with some value for maxVal and minVal, random garbage values are prone to errors.
    2. change the loops as such..
    3. initialize the loop with j=0.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main(){
    	
    	int i, j;
    	float A[5][4] = {1.2, 0.0, -2.0, 0.35, 5.0, 3.5, 0.75, -1.5, 0, -1.0, 1.0, 2.3, 4.5, 4.25, -3.0, 3.6, 4.0, -0.5, 1.3, 1.68};
    	float B[5][2], maxVal=0, minVal=0;
    
    	for (i = 0; i < 5; i++){
    		for (j = 0;j < 4;j++){
    			if (A[i][j] > maxVal)
    				maxVal = A[i][j];
    			if (A[i][j] < minVal)
    				minVal = A[i][j];
    		}
    	}
    		printf("%f %f ",minVal,maxVal);
    		getch();
    }

IMN logo majestic logo threadwatch logo seochat tools logo