How to get an array to sort

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bryon
    New Member
    • Apr 2010
    • 12

    How to get an array to sort

    I have been trying to get an array to sort out 4 numbers that someone enters in. I have been reading on how to do this but nothing will work...

    This program is supposed to sort the numbers in ascending and descending order.

    This is what I have so far. I'm trying to get them to ascend right now but I'm failing.

    Code:
    #include "stdafx.h"
    
    #define ARY_SIZE 4
    
    int main(void)
    {
    	printf ("Welcome please enjoy this program.\n\n");
    
    	int num [ARY_SIZE];
    	int i;
    	int j;
    	int x;
    
    	printf ("Enter 4 numbers please.\n");
    	for (int i = 0; i < ARY_SIZE; i++)
    		scanf("%d", &num [i]);
    	{
            for (int j = i+1; j < ARY_SIZE; j++)
    		{
                    if (num[i] > num[j])
    				{
                            x = num[i];
                            num[i] = num[j];
                            num[j] = x;
                    }
            }
    	}
    	
    	
    
    
    
    	return 0;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Your brace at line 17 is mis-place so that by the time the loop on line 18 starts the loop on line 15 has finished. That means that at line 18 i has the value 4, j is initialised to 5 and the loop at line 18 is never entered.

    However even if you swap lines 16 and 17 which is what I think you intended this will not work because in that case the loop at line 18 tries to sort the data in the second half to the array but the data for that part of the array has not been entered yet.

    You should not be trying to sort in place in the same loop that is actually acquiring the data. Acquire all the data then sort all the data, 2 steps.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Can you use the Standard Library function qsort or are you required to implement the sort function yourself?

      If you have to write your own sort function then you should familiarize yourself with the more common sort algorithms. Has your instructor presented any sort algorithms to the class yet?

      Comment

      • bryon
        New Member
        • Apr 2010
        • 12

        #4
        I have never heard of qsort before so I don't know if we can use that or not. He doesn't really care as long as we get it to do what the problem asked for. As for the algorithms. I don't see anything on algorithms in the chapter we are on. Right now we are learning about pointer applications and all it talks about is dynamic/static arrays and pointer arithmetic. This is an online class so we pretty much have to learn from the book which doesn't help at all.....

        Here is the problem i have to do if it helps any:

        Write a program that reads integers from the keyboard and places them in an array. The program then will sort the array into ascending and descending order and print the sorted lists. The program must not change the original array or create any other integer arrays.

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          Don't know if you're allowed to create another integer array only to store the contents of the results. I would seriously check with your professor if you are allowed to create result integer arrays.

          If not, try looking for the smallest integer. Then after you've gone through once, look for the next one, etc..


          Aside, I don't understand why these beginning programming assignments have such retarded / unclear restraints. The professors who created these assignments seriously need to learn English if they are going to write the questions in English.
          Does that mean you can create say a float or double array? *wink

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            I agree with jkmyoung -- you need clarification from the instructor. On the face of it these instructions are contradictory: you can't sort the input array without either reordering it or writing the sorted numbers into an output array, both of which are prohibited.

            I suppose a legalistic pedant might sort the input array into a linked list or binary tree ... those aren't integer arrays. However, your instructor may not be amused.

            The only solution I can come up with that meets the requirements is to pseudo-sort the input array into the print queue using an approach vaguely analogous to straight-insertion. This is just another way of stating jkmyoung's approach using more jargon.

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              If the integers entered from the keyboard were assigned to a string s then another duplicate string say s1 could be originated with s1=s.s could be loaded into an array and left untouched. s1 could be sorted and printed in ascending order and then sorted and printed in descending order. If this actually worked would it be classified as unacceptable obfuscation ?

              Comment

              • bryon
                New Member
                • Apr 2010
                • 12

                #8
                Originally posted by whodgson
                If the integers entered from the keyboard were assigned to a string s then another duplicate string say s1 could be originated with s1=s.s could be loaded into an array and left untouched. s1 could be sorted and printed in ascending order and then sorted and printed in descending order. If this actually worked would it be classified as unacceptable obfuscation ?
                We are mainly working with arrays right now so I would not be allowed to use a strings. (not that I even know what a string is.....)

                Anyways, I haven't talked to him yet but as long as the program runs and does what its supposed to I doubt he will care what I do as long as I am working with arrays. I have done things that we haven't learned before and he didn't seem to mind cause he gave me a 100 and told me what a good job I did (which is his generic comment he gives everyone if the program runs). So if the last part of the question makes this almost impossiable to do then I think he won't mind. I honestly don't think he looks at the codes we send in unless they don't compile.

                P.S. This program is due on Friday so I'm kind of starting to freak out.....

                Comment

                • bryon
                  New Member
                  • Apr 2010
                  • 12

                  #9
                  Ok this is what I have gotten to work so far (I did the first code completely wrong)... i want the results to look like this though.

                  Original: (original order they entered the numbers in)
                  Ascending: (from smallest to largest)
                  Descending: (from largest to smallest)

                  I got the Ascending part but don't know how to get the other 2.

                  Code:
                  #include "stdafx.h"
                  
                  #define SIZE 4
                  
                  
                  int* getData (int* pAry, int arySize);
                  void selectSort (int* pAry, int* pLast);
                  void printData (int* pAry, int* pLast);
                  int* smallest (int* pAry, int* pLast);
                  void exchange (int* current, int* smallest);
                   
                  int main (void)
                  {
                  	int ary[SIZE];
                  	int* pLast;
                  
                  	pLast = getData (ary, SIZE);
                  	selectSort (ary, pLast);
                  	printData (ary, pLast);
                  
                  	return 0;
                  }
                  
                  
                  int* getData (int* pAry, int arySize)
                  {
                  	int ioResult;
                  	int readCnt = 0;
                  	int* pFill = pAry;
                  
                  	do
                  	{
                  		printf("Enter Number Please: ");
                  		ioResult = scanf("%d", pFill);
                  		if (ioResult == 1)
                  		{
                  			pFill++;
                  			readCnt++;
                  		}
                  	} while (ioResult == 1 && readCnt < arySize);
                  
                  	
                  
                  
                  	
                  	return (--pFill);
                  }
                  
                  void selectSort (int* pAry, int* pLast)
                  {
                  	int* pWalker;
                  	int* pSmallest;
                  
                  	for (pWalker = pAry; pWalker < pLast; pWalker++)
                  	{
                  		pSmallest = smallest (pWalker, pLast);
                  		exchange (pWalker, pSmallest);
                  	}
                  
                  	return;
                  }
                  
                  
                  int* smallest (int* pAry, int* pLast)
                  {
                  	int* pLooker;
                  	int* pSmallest;
                  
                  	for (pSmallest = pAry, pLooker = pAry + 1;
                  		pLooker <= pLast;
                  		pLooker++)
                  		if (*pLooker < *pSmallest)
                  			pSmallest = pLooker;
                  
                  	return pSmallest;
                  }
                  
                  
                  void exchange (int* current, int* smallest)
                  {
                  	int temp;
                  
                  	//printf("This is the current value %d and this is the smallest value %d", *current, *smallest);
                  
                  	temp = *smallest;
                  	*smallest = *current;
                  	*current = temp;
                  
                  	return;
                  }
                  
                  
                  void printData (int* pAry, int* pLast)
                  {
                  	int nmbrPrt;
                  	int* pPrint;
                  
                  	
                  	
                  	printf("\n\nAscending: ");
                  	for (pPrint = pAry, nmbrPrt = 0;
                  		pPrint <= pLast;
                  		nmbrPrt++, pPrint++)
                  		printf("%4d", *pPrint);
                  	printf("\n\nDone\n\n ");
                  
                  	return;
                  }

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    To print reverse order, you can either resort, or print from the end first and use -- instead of ++. eg set pPrint = pLast, and then check that pPrint >= pAry.

                    Comment

                    • bryon
                      New Member
                      • Apr 2010
                      • 12

                      #11
                      THANK YOU SOOOO MUCH!!! now I just need it to print the numbers they originally put in...

                      here is an example of what I get now that I can get id to print ascending and descending:

                      lets say the numbers I put in are in this order: 4 7 12 1

                      This is what i get

                      Original: 4 4 4 4
                      Ascending: 1 4 7 12
                      Descending: 12 7 4 1

                      See what I mean? I'm stuck on the original part now...

                      (the code is still the same as the last post I made only difference is I put in an extra printf statement at the very end and on line 43-46 i have this
                      Code:
                      printf("\n\nOriginal: ");
                      	for(int i = 0; i < SIZE; i++)
                      		printf("%4d ", *pAry);

                      Comment

                      • jkmyoung
                        Recognized Expert Top Contributor
                        • Mar 2006
                        • 2057

                        #12
                        ..You really can't print the original anymore because you've changed it.
                        Print the original before you change everything.

                        Eg make some function printOriginal, and put the first for loop from printData in it.
                        Then in your main function you have:
                        Code:
                            pLast = getData (ary, SIZE); 
                            printOriginal(ary, pLast);
                            selectSort (ary, pLast); 
                            printData (ary, pLast);

                        Comment

                        • bryon
                          New Member
                          • Apr 2010
                          • 12

                          #13
                          So even if you put the printf state up further in your code before it all changes (like where I put it) it still wont orint the original?

                          Look at lines 43-45


                          Code:
                          #include "stdafx.h"
                          
                          #define SIZE 4
                          
                          
                          int* getData (int* pAry, int arySize);
                          void selectSort (int* pAry, int* pLast);
                          void printData (int* pAry, int* pLast);
                          int* smallest (int* pAry, int* pLast);
                          void exchange (int* current, int* smallest);
                           
                          int main (void)
                          {
                          	int ary[SIZE];
                          	int* pLast;
                          
                          	pLast = getData (ary, SIZE);
                          	selectSort (ary, pLast);
                          	printData (ary, pLast);
                          
                          	return 0;
                          }
                          
                          
                          int* getData (int* pAry, int arySize)
                          {
                          	int ioResult;
                          	int readCnt = 0;
                          	int* pFill = pAry;
                          
                          	do
                          	{
                          		printf("Enter Number Please: ");
                          		ioResult = scanf("%d", pFill);
                          		if (ioResult == 1)
                          		{
                          			pFill++;
                          			readCnt++;
                          		}
                          	} while (ioResult == 1 && readCnt < arySize);
                          
                          	printf("\n\nOriginal: ");
                          	for(int i = 0; i < SIZE; i++)
                          		printf("%4d ", *pAry);
                          
                          
                          	
                          	return (--pFill);
                          }
                          
                          void selectSort (int* pAry, int* pLast)
                          {
                          	int* pWalker;
                          	int* pSmallest;
                          
                          	for (pWalker = pAry; pWalker < pLast; pWalker++)
                          	{
                          		pSmallest = smallest (pWalker, pLast);
                          		exchange (pWalker, pSmallest);
                          	}
                          
                          	return;
                          }
                          
                          
                          int* smallest (int* pAry, int* pLast)
                          {
                          	int* pLooker;
                          	int* pSmallest;
                          
                          	for (pSmallest = pAry, pLooker = pAry + 1;
                          		pLooker <= pLast;
                          		pLooker++)
                          		if (*pLooker < *pSmallest)
                          			pSmallest = pLooker;
                          
                          	return pSmallest;
                          }
                          
                          
                          void exchange (int* current, int* smallest)
                          {
                          	int temp;
                          
                          	//printf("This is the current value %d and this is the smallest value %d", *current, *smallest);
                          
                          	temp = *smallest;
                          	*smallest = *current;
                          	*current = temp;
                          
                          	return;
                          }
                          
                          
                          void printData (int* pAry, int* pLast)
                          {
                          	int nmbrPrt;
                          	int* pPrint;
                          
                          	
                          	
                          	printf("\n\nAscending: ");
                          	for (pPrint = pAry, nmbrPrt = 0;
                          		pPrint <= pLast;
                          		nmbrPrt++, pPrint++)
                          		printf("%4d", *pPrint);
                          
                          	printf("\n\nDescending: ");
                          	for (pPrint = pLast, nmbrPrt = 0;
                          		pPrint >= pAry;
                          		nmbrPrt--, pPrint--)
                          		printf("%4d", *pPrint);
                          
                          	printf("\n\nDone\n\n ");
                          
                          	return;
                          }

                          Comment

                          • bryon
                            New Member
                            • Apr 2010
                            • 12

                            #14
                            ....... I am so freaking proud of myself right now!!!

                            I got it to print the original and all I did was change line 45 from:
                            Code:
                            printf("%4d ", *pAry);
                            To:
                            Code:
                            printf("%4d ", *pAry++);

                            Comment

                            Working...