Thread: Help with a simple problem that eludes me

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    Help with a simple problem that eludes me

    i have a table
    int tab1[25]
    i need to organize the values in the table from smallest to largest(there are no numbers repeated) , BUT if the value is -1, then these have to go to the end of all other numbers
    and example of the table before the organization would be
    {4,7,2,9,5,10,-1,-1,-1,-1,-1,-1,-1,-1.....-1}
    after the organization
    {2,4,5,7,9,10,-1,-1,-1......-1}

    Code:
    void organizer(int tab1[3][25])
    { 
    	int a,b,c;
    	int tabhelper[25];
    	for(a=0;a<3;a++)
    	{	
    		for(b=0;b<25;b++)
    		{
    			tab1[a][b]=tabhelper[b];
    			
    			
    		}
    		for(b=0;b<25;b++)
    		{
    			int d;
    			if(tabhelper[b]>tabhelper[b+1])
    			{
    				d =tabhelper[b];
    				tabhelper[b]=tabhelper[b+1]
    				tabhelper[b+1]=d;
    			}
    		}
    		
    		
    		
    	}
    	
    }
    ive been trying to do this for about 1 hour, but i always have some mistake or it just doesnt work, and i dont have much time to lose cause this is a reallly really small part of a project i have to do.
    thank you in advanced

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    First of all why you are having a 2D array if you want to just sorting

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Put it another way: sort the list of numbers in ascending order, with the proviso that -1 is treated as greater than any other number.

    So, you can write out your sort normally, then test. You will end up with the -1s in front, but after that you just need to change the comparison to make it such that -1 is treated as greater than any other number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You haven't initialized tabhelper array, so "tab1[a][b]=tabhelper[b];" will be put garbage values in the tab1 array.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    woops sorry
    i realize i did a big mistake in the first lines of code
    it should be tabhelper[b]=tab1[a][b]; not tab1[a][b]=tabhelper[b];

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    i wrote the first part of the code wrong, its supposed to be tabhelper[b]=tab1[a][b]; not tab1[a][b]=tabhelper[b];
    and i need to organize the 3 "sub" arrays of the double array table

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    you have to use any sorting algorithm. And take a counter which will take care of -1 and at the end concatenate all the -1 in to the array at end of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM