Thread: Little Problem

  1. #1
    Unregistered
    Guest

    Question Little Problem

    This little bit of code is meant to scan each row of "array" noting the position of the element with the maximum value in each row and writing that position in to "maxArray" but it doesnt appear to be working. Any suggestions as to why this isnt working? Thanks

    Code:
      for(i=0; i<4; i++)
         {
            for(j=0; j<4; j++)
             {
              if(array[j][i] > XMax)
              XMax = j;
             }
             maxArray[i] = XMax;
         }

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Code:
      for(i=0; i<4; i++)
         {
            for(j=0; j<4; j++)
             {
              if(array[j][i] > XMax)
              XMax = j;
             }
             maxArray[i] = XMax;
         }
    Your indexes are around the wrong way.. Should be:

    array[i][j] - I think this will fix the problem.

    Code:
    for(i=0; i<4; i++)
    {
       for(j=0; j<4; j++)
      {
         if(array[i][j] > MAXVal)
         {
            MAXVal = array[i][j];
            maxArray[i] = j;
         }
      }
      MAXVal = 0;
    }
    Damn.. SavesTheDay, You got to it before I finished editing my code! But the above works.. Just tested it.

    Yes - you should be testing for the maximum value stored in the array not the highest index value..
    Last edited by foniks munkee; 02-18-2002 at 06:34 AM.

  3. #3
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    That was one problem...the other problem was this:

    XMax = j;
    This was setting the MAX to what J was....you are using j for the loop counter...you want the MAX to be the VALUE of j, then store the position of j...but the j and i were backwards too...this code is correct and working to fix both problems:

    Code:
    for(i=0; i<4; i++)
         {
            for(j=0; j<4; j++)
             {
    			if(array[i][j]>XMax)
    			{
    				XMax=array[i][j];
    				maxArray[i]=j;
    			}
             }
    	}
    enjoy.

  4. #4
    Unregistered
    Guest
    Thanks for that, it all seems fine now. The indexes being the wrong way round was just me being sloppy when I was typing this out. Deliberate mistake and all that.............

    Thanks a lot!

  5. #5
    Unregistered
    Guest
    Back again! This is a little expansion of the problem above so why waste space with a new topic. The code was sorted and working well but when I expanded it to scan a 64x64 array it gets the position of the first 4 or 5 maximum elements then just repeats the next one for the next 59 or so elements of maxArray. Seems like its bailing out or something. Any suggestions?

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Could you post your code?
    Callou collei we'll code the way
    Of prime numbers and pings!

  7. #7
    Unregistered
    Guest
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
    int i=0, j=0;
    int array[64][64], maxArray[64];
    int XMax=0;
    char filename[20];
    FILE *fHandle;
    
    printf("Enter Filename: ");
    scanf("%s", filename);
    
    if((fHandle = fopen(filename, "r")) == NULL)
    {
    		printf("Unable to open file");
      return 0;
    
    }
    else
    {
    
    for(i=0; i<64; i++)
    		{
    			for(j=0; j<64; j++)
    			{
    				fscanf(fHandle, "%d", &array[i][j]);
    			}
    		}
    
    }
    
    for(i=0; i<64; i++)
         {
            for(j=0; j<64; j++)
             {
    			if(array[i][j]>XMax)
    			{
    				XMax=array[i][j];
    				maxArray[i]=j;
    			}
             }
         }
    }
    As I said before it works for a 4x4 array but doesnt work for a 64x64 array. Confused!

  8. #8
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    weird...i'm in a hurry now so I don't really have time to mess with it...but off the top of my head I would *guess* that when the characters are being read in, something funny is going on.....since the code for the 4x4 worked, it's not going to fail on the 64x64...and I couldn't see any prominent typos...So I would toy around with the way the characters are being read in.....I couldn't think of anything else at the time..........don't forget to close your file, too......

  9. #9
    Unregistered
    Guest
    Ive messed about a bit but still no luck. The first 5 elements of maxArray are always correct but then the 6th one is 0 then it gets the 7th, 8th, 9th and 10th correct but then returns 0 up to the 16th element which is correct. Its pretty eratic like this all the way up to the 64th element with a lot of 0's which I know shouldnt be there!

    The file does get closed aswell now by the way!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int i=0, j=0;
    > int array[64][64], maxArray[64];
    > int XMax=0;
    > char filename[20];
    > FILE *fHandle;
    Depending on your compiler, this is over 8K (or 16K) of local variables.

    Some old (ie DOS) compilers only allocate a few K of stack.

    Try moving the arrays off the stack like so
    static int array[64][64], maxArray[64];

  11. #11
    Unregistered
    Guest
    that didnt work. Its a windows based compiler (Dev C++ 4). Pretty up to date. Ive noticed aswell now that it doesnt always work for the 4 by 4 array. It sommetimes messes up the 4th element. First 3 are always correct though. sheer baffledness has come over me.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In that case, check your input

    Code:
    if ( fscanf(fHandle, "%d", &array[i][j]) != 1 ) {
        printf( "Error reading file at i=%d, j=%d\n", i, j );
    }

  13. #13
    Unregistered
    Guest

    Unhappy

    Theres no errors in the input. Ive tried a few different txt files and a couple of dat files and the patern in the output is the same for each one (with the first 6 elements fine and eratic 0's.....).

  14. #14
    Registered User
    Join Date
    Feb 2002
    Posts
    9
    The source code and input should be attached below! The input txt file has been set up so that the output array should read 0 to 63. Its decided to change its mind today and only get the first 3 elements and the 19th correct?? Im hoping ive just been a complete idiot here and missed something simple.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You orginally asked
    > This little bit of code is meant to scan each row of "array" noting the position of the element with the maximum value in each row and writing that position in to "maxArray"

    If you're expecting this from your example data
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    etc


    Then you need to change your code as follows
    Code:
        for(row=0; row<64; row++)
        {
            XMax = 0;               // new row, new max
            for(col=0; col<64; col++)
            {
                if(array[row][col]>XMax)
                {
                    XMax=array[row][col];
                    maxArray[row]=col;
                }
            }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM