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

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Help with multi-diemensional array


    Hello,

    I have a problem with the following code

    int main()
    {
    int i,j,max;
    int a[3][3]={{85,99,77},
    {80,73,74},
    {89,33,88}};

    for (i=0;i<3;i++){
    max=0;
    printf (" \n");
    for (j=0;j<3;j++)
    if (max<a[i][j])
    max=a[i][j];
    printf ("%d the maximum is %i ",a[i][j],max);
    }
    }

    I want the output to be the ALL numbers in arrays and the maximum number of each row I tried and tried but I couldn't know where is the problem,and here is the output
    80 the maximum is 99
    89 the maximum is 80
    0 the maximum is 99

    any help pls
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    You could try posting nicely formated code like below...

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int i, j, max;
    	
    	int a[3][3]={{85,99,77}, {80,73,74}, {89,33,88}};
    
    	for (i= 0; i < 3; i++)
    	{
    		max=0;
    		printf (" \n");
    		
    		for (j=0;j<3;j++)
    		{
    			if (max < a[i][j])
    				max = a[i][j];
    			
    		printf ("%d the maximum is %i\n",a[i][j],max);
    		}
    		fputs("\n", stdout);
    	}
    	
    	return 0;
    }
    The output of the above code is...

    Code:
    85 the maximum is 85
    99 the maximum is 99
    77 the maximum is 99
    
    
    80 the maximum is 80
    73 the maximum is 80
    74 the maximum is 80
    
    
    89 the maximum is 89
    33 the maximum is 89
    88 the maximum is 89
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    printf ("%d the maximum is %i ",a[i][j],max);
    Print i and j too :)
    Code:
    printf ("%d the maximum is %i (i is %d; j is %d)\n", a[i][j], max, i, j);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    thank you very much

IMN logo majestic logo threadwatch logo seochat tools logo