dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: sorting elements of matrix

  1. #1
    Join Date
    Nov 2013
    Posts
    11

    sorting elements of matrix

    hi.i wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
    here's the code
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <conio.h>
    #include <string.h>
    using namespace std;
    int main()
    {
        int i,j,p,q,c1,c2,r1,r2,k,temp=1;
        char ans1,ans2,answer;
        int matrix1[100][100];
        int matrix2[100][100];
        int sum[100][100];
        int multi[100][100];
        int min[100][100];
        char func[80];
     for(i=c1,p=0;i>0,p<r1;i--,p++)
               {
                  for(j=0,q=0;j<c1,q<r1;j++,q++)
                  if(matrix1[i][j]>matrix1[i][j+1])
                  {
                     k=matrix1[i][j];
                     matrix1[i][j]=matrix1[i][j+1];
                     matrix1[i][j+1]=k;
                                }
                                }
                   cout<<"\nsorted matrix1 = ";             
                 for(i=0;i<r1;i++)
                 for(j=0;j<c1;j++)
                                {
                                cout<<matrix1[i][j]<<"   ";
                                if(j==r1-1)
                                cout<<"\n\n";
                  for(i=c2,p=0;i>0,p<r2;i--,p++)
               {
                  for(j=0,q=0;j<c2,q<r2;j++,q++)
                  if(matrix2[i][j]>matrix2[i][j+1])
                  {
                     k=matrix2[i][j];
                     matrix2[i][j]=matrix2[i][j+1];
                     matrix2[i][j+1]=k;
                                                     }
                                }
                     cout<<"\nsorted matrix2 = ";           
                 for(i=0;i<r2;i++)
                  for(j=0;j<c2;j++)
                       {           
                  cout<<matrix2[i][j]<<"    ";
                  if(j==r2-1)
                  cout<<"\n\n";
                                }  }
        cout<<"\n";
        getch ();
        return 0;
    }
    Last edited by Faraz95; January 11th, 2014 at 10:45 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,512

    Re: sorting elements of matrix

    Quote Originally Posted by Faraz95 View Post
    hi.i wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
    Just find the bugs and fix them!
    Did you try to debug your code?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,868

    Re: sorting elements of matrix

    but it doesn't run well
    Define "doesn't run well".
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.3.4)

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: sorting elements of matrix

    Quote Originally Posted by Faraz95 View Post
    hi.i wrote a code to sort elements of a matrix which are in one row,but it doesn't run well .what should i do??
    First, your two-dimensional arrays are uninitialized. They contain garbage data, so how are you going to sort garbage data that can change on each run of the program?


    Second, assuming that those matrices contain valid data in all the cells, usage of std::sort simplifies this problem:
    Code:
    #include <algorithm>
    //...
    for(size_t i = 0; i < 100; ++i)
        std::sort(&matrix1[i][0], &matrix1[i][0] + 100);
    That sorts all of the rows of matrix1 in ascending order.

    If this is a homework problem, and you're supposed to write the sort code, then instead of creating large matrices with 100 elements, start out with one with 3,4, or 5 elements. Then write very simple code that attempts to sort the matrix. That is much easier than trying to figure out where a 100 x 100 element matrix fails to sort.

    Also, you should be using the debugger to step through the code to see where the sort breaks down if it doesn't work. You should be doing this instead of just writing code, running it, and asking us to debug the code, all without any indication that you've attempted to debug the code yourself (if you did debug the code, where does the sort go wrong and why?).

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)