2D array sorting from min. to max.

This is a discussion on 2D array sorting from min. to max. within the C Programming forums, part of the General Programming Boards category; guyz i'm stucked outta here in a tiny problem but really it turned out to be a big one for ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    2

    2D array sorting from min. to max.

    guyz i'm stucked outta here in a tiny problem but really it turned out to be a big one for me. its all about sorting a 2D array for example [3][3] from min. to max. with integars.
    i've been only 2 monthes in C programmin so i think its for me may be its a hard one for now.
    this is the code that i've done, but i dunno why doesnt it work, and if there any logical errors i hope to see the solutions for it
    Code:

    #include <stdio.h>
    void main()
    {
    int x[3][3],i,j,m,k,tmp;
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    scanf("%d",&x[i][j]);
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    {
    for(m=i;m<3;m++)
    for(k=j+1;k<3;k++)
    if(x[i][j]>x[m][k])
    {
    tmp=x[i][j];
    x[i][j]=x[m][k]; x[d][f]=tmp;
    }
    }
    for(i=0;i<3;i++)
    for(j=0;j<3;j++)
    printf("%d ",x[i][j]);

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    You should use code tags around your er...code. Also explain what you expect it to do and what it's doing now that you weren't expecting it to do. There's more than one way to "sort a 2D array" so we can't quite be sure what you're looking for. Maybe give some sample input and output.

    However, I think you have more than logic errors going on here:
    Code:
    if(x[i][j]>x[m][k])
    {
    tmp=x[i][j];
    x[i][j]=x[m][k]; x[d][f]=tmp;
    }
    d and f aren't declared anywhere so your program shouldn't even compile.
    Last edited by itsme86; 10-14-2004 at 03:21 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  3. How To Declare and Dynamically Size a Global 2D Array?
    By groberts1980 in forum C Programming
    Replies: 26
    Last Post: 11-15-2006, 09:07 AM
  4. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 08:53 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21