Thread: finding smallest of 2 dimensional array

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    12

    finding smallest of 2 dimensional array

    I need help with my code. I need to find the smallest and largest of each row of a two dimensional array, coming from a structure....
    the structure has 3 different skaters and each one has a two dimensional array as follows
    (example is just of one skater)

    here is the array
    0 2 1 1 1 0 1 0 1 1 1 1
    1 2 2 2 2 1 2 1 2 2 2 2
    2 1 1 1 1 0 0 2 1 2 1 2
    2 1 1 1 1 1 2 1 1 1 1 1
    2 2 2 2 1 2 3 3 2 3 2 1
    1 1 1 2 2 0 0 0 1 2 1 1
    1 0 2 2 1 1 1 2 2 2 2 1
    1 1 2 2 1 1 1 1 2 2 1 1

    the structure is skater[x].scores[][];
    Code:
    {
    	
    int i = 0;
    int smallest[7];
    int f;
    int j;
    int a;
    
    	while ( i < count)
    	{
    		for ( j = 0; j < 8; j++)
    		{
    			for ( a = 0; a<12; a++)
    			{
    				smallest[j] = skater[i].scores[j][a];
    				if( smallest[j]< skater[i].scores[j][a+1] );
    				smallest[j] = skater[i].scores[j][a+1];
    			}
    			printf ( "%d\n", smallest[j]);
    		}
    		
    		printf( "\n\n");
    		i++;
    	}
    Last edited by vlad_i; 03-18-2011 at 05:40 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use code tags.

    [code] ...your code goes in here... [/code]

    Here's the logic:
    Code:
    for each row
        for each column
            if here[ row ][ column ] < smallest
                set smallest to this value
    You can do the same thing for biggest. The only change you want is to make an array with as many elements as you have rows, and use it to store the lowest or highest for that row.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    12
    So i tried switching it around but its still not working really. I need the smallest of each row stored into a 1 dimensional array smallest[j]. any other suggestions?


    Code:
    while ( i < count)
    	{
    		for ( j = 0; j < 8; j++)
    		{
    			for ( a = 0; a<12; a++)
    			{
    				if( smallest[j]> skater[i].scores[j][a] );
    				smallest[j] = skater[i].scores[j][a];
    			}
    			printf ( "%d\n", smallest[j]);
    		}

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vlad_i View Post
    So i tried switching it around but its still not working really.
    What does "not really working" mean?
    Code:
    f( smallest[j] > skater[i].scores[j][a] );
    				smallest[j] = skater[i].scores[j][a];
    There are two problems.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    if (smallest[j] > skater[i].scores[j][a])
        smallest[j] = skater[i].scores[j][a];
    if (largest[j] < skater[i].scores[j][a])
        largest[j] = skater[i].scores[j][a];
    And you need to initialize smallest and largest arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM