using bubble sort to sort a matrix by sum..

This is a discussion on using bubble sort to sort a matrix by sum.. within the C Programming forums, part of the General Programming Boards category; Yup, didn't listen to a thing I said! You're making changes that don't actually get you any closer to your ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Yup, didn't listen to a thing I said!

    You're making changes that don't actually get you any closer to your goal, if anything they've taken you backwards. Now the sort isn't working for a totally different reason. You've got the for loops wrong. Here is an example of what should work:
    Code:
        for (x = 0; x < rows-1; x++)
            for (y = x; y < rows-1; y++)
    Now I know you've gotten into the habbit of just commenting out stuff that you aren't using temporarily, and never deleting anything because you're never sure of yourself. However, you've got so much commented out junk that you're just making it harder for yourself. Start fresh by making a backup copy and then clearing out all the crap you don't need, and turn your warning level up to the max and delete every variable that is not used. You don't need holder2 or temp3 either, if you'd pay attention to what I told you earlier.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your "sorting the matrix question" thread, we fixed your program - the code is still there.
    It worked, with the last version even working on non-square matrices. You said it worked, also.

    So *why* are you posting this same program again?

    Why not use the code I fixed for you?

    If my code is too *beautiful* for you - wear sunglasses, dude!

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by iMalc View Post
    Yup, didn't listen to a thing I said!

    You're making changes that don't actually get you any closer to your goal, if anything they've taken you backwards. Now the sort isn't working for a totally different reason. You've got the for loops wrong. Here is an example of what should work:
    Code:
        for (x = 0; x < rows-1; x++)
            for (y = x; y < rows-1; y++)
    Now I know you've gotten into the habbit of just commenting out stuff that you aren't using temporarily, and never deleting anything because you're never sure of yourself. However, you've got so much commented out junk that you're just making it harder for yourself. Start fresh by making a backup copy and then clearing out all the crap you don't need, and turn your warning level up to the max and delete every variable that is not used. You don't need holder2 or temp3 either, if you'd pay attention to what I told you earlier.
    why you are saying that this code is not working??

    can you gives me one example for which the code is not working?

    Code:
    #include <stdio.h>
    
    int main()   { //star
    	int i,j,k,temp;
    	int rows = 50;
       int cols = 50;
       int jndex,input;
    //	int sum2=0;  sum2 is never used
    
       /* You can't make an array without telling it's size. rows and cols
       have not been assigned a value, yet.
       */
       int matrix[50][50];
    //	int matrix[rows][cols];
    //	int sum[rows][cols];
       int sum[50][50];
    //	int temp1[rows][cols];
       int temp1[50][50];
    //	int transpose [cols][rows];
       int transpose [50][50];
    	int index,kndex,tndex,gndex,lndex;
       int rows_sum[50];
       //int rows_sum[rows]; //rows could be ANY value, still.
    
    /* your program isn't designed for this
       printf("enter rows and cols [1..50] ==> ");
    	scanf("%d %d",&rows,&cols);
    */
    
    
    /* You can save yourself this code, just by making the array initialize to
    zero's, when you declare it:
    
    row_sum[4] = { 0 };
    
    This only works when you first declare them, not at any later time
    */
     printf("enter the size of a  matrix:");
       scanf("%d %d",&rows,&cols);
    	for (index = 0; index < rows; index++)
    	{
    	    rows_sum[index]=0;
    		for (kndex = 0; kndex < cols; kndex++)
    		{
    			matrix[index][kndex] = 0;
    			sum[index][kndex] = 0;
    			temp1[index][kndex] = 0;
    			transpose[index][kndex] = 0;
    		}//end inner for
    	}//end outer for
    	printf("enter numbers in a row for a matrix:"); //stat input
    	for (index =0;index<rows; index++)
    	{
    		for (kndex =0; kndex<cols; kndex++)
    		{
    			scanf("%d", &matrix[index][kndex]);
    			transpose[kndex][index] = matrix[index][kndex];
    		}
    	}
    	i = getchar();  //needed because of scanf()
    	//end input
    	//start power operation
    	//  rows sum
    	for (index = 0; index < rows; index++)
    	{
    		for (kndex = 0; kndex < cols; kndex++){
    			rows_sum[index]=rows_sum[index]+matrix[index][kndex];
    		printf("%d ",matrix[index][kndex]);
    		}
    		printf("\n");
    	}
          //  end rows sum
    
       for(i = 0; i <rows - 1; i++)  {
          for(j = i + 1; j <rows; j++)  {
             if(rows_sum[i] >rows_sum[j])   {
                temp = rows_sum[i];
                rows_sum[i] = rows_sum[j];
                rows_sum[j] = temp;
                //change this line remove the brace
                //now swap the rows elements, from row j to row i
                for(k = 0; k <cols; k++)   {
                   temp = matrix[i][k];                                      //here is line 62
                   matrix[i][k] = matrix[j][k];
                   matrix[j][k] = temp;
                }
             }  //add this brace
          }
       }
    
       //print the swapped matrix
       for(i = 0; i < rows; i++)  {
          putchar('\n');
          for(j = 0; j < cols; j++)
             printf(" %d", matrix[i][j]);
       }
    
       i = getchar();
       return 0;
    
    }//end main
    Last edited by transgalactic2; 12-22-2008 at 08:43 PM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    ??I have no idea what you mean??

    Are you saying that the program from your other matrix thread, that I fixed for you, is now *NOT* working, on some input?

  5. #20
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i think its working
    i tried it on many inputs

    iMalc says its not working.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by transgalactic2 View Post
    i think its working
    i tried it on many inputs

    iMalc says its not working.
    IMalc says it's not working, because you posted up in another thread "it's not working".

    It is working, and you can safely ignore iMalc *this one time* on the "working" part of the program. He just believed you when you said "it's not working" in the other thread, so it's got him confused. And so does the program, since you've kept in all the old obsolete notes, and still haven't learned to indent properly.

    Look at the code I wrote when I fixed your program - isn't that nice? Isn't that a lot more readable? You will be able to find and correct a lot more bugs in your program, much faster, if you will use a good style of indentation in your coding.

    This program uses a sort that I've never seen before. Standard for this situation would be a sort through the use of an index array. That seemed too advanced for you right now, so I thought of doing it this way - and it works fine. It is not the most efficient way to do this kind of sort, however.

    With small to medium sized integer arrays, this sort is very fast. Only with large arrays of integers, or medium arrays of strings, does it begin to take too long, where "too long" is defined as the time I need to locate, and take a sip from, my coffee cup.

    Please don't keep this thread going - the code problem has been fixed. Please don't start two or more threads on the same topic. It just confuses people. And again, I'll mention please clean up your program code.

    If you ignore these basic standards on the forum, you'll get less and less good help.

  7. #22
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Right before my post:
    i add the row swap par and it doesnt work
    I took that to mean that you made some change, it wasn't working, so you posted the code again.
    Then I mistook your problem for another one.
    Yes your loops as you had them will work, but they could be made more optimal, using the loops I posted, assuming I got them right myself.

    Try not to use two threads about the same piece of code next time, even if they appear to be seperate problems.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. testing a bubble sort algorithm
    By rushhour in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2009, 12:00 AM
  3. Bubble Sort Query
    By coolboarderguy in forum C Programming
    Replies: 2
    Last Post: 04-15-2008, 12:50 AM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM
  5. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

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