473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to get an array to sort

12
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.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #define ARY_SIZE 4
  4.  
  5. int main(void)
  6. {
  7.     printf ("Welcome please enjoy this program.\n\n");
  8.  
  9.     int num [ARY_SIZE];
  10.     int i;
  11.     int j;
  12.     int x;
  13.  
  14.     printf ("Enter 4 numbers please.\n");
  15.     for (int i = 0; i < ARY_SIZE; i++)
  16.         scanf("%d", &num [i]);
  17.     {
  18.         for (int j = i+1; j < ARY_SIZE; j++)
  19.         {
  20.                 if (num[i] > num[j])
  21.                 {
  22.                         x = num[i];
  23.                         num[i] = num[j];
  24.                         num[j] = x;
  25.                 }
  26.         }
  27.     }
  28.  
  29.  
  30.  
  31.  
  32.  
  33.     return 0;
  34. }
  35.  
Apr 21 '10 #1

✓ answered by jkmyoung

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.

13 5221
Banfa
9,065 Expert Mod 8TB
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.
Apr 21 '10 #2
donbock
2,426 Expert 2GB
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?
Apr 21 '10 #3
bryon
12
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.
Apr 21 '10 #4
jkmyoung
2,057 Expert 2GB
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
Apr 21 '10 #5
donbock
2,426 Expert 2GB
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.
Apr 21 '10 #6
whodgson
542 512MB
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 ?
Apr 22 '10 #7
bryon
12
@whodgson
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.....
Apr 22 '10 #8
bryon
12
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.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2.  
  3. #define SIZE 4
  4.  
  5.  
  6. int* getData (int* pAry, int arySize);
  7. void selectSort (int* pAry, int* pLast);
  8. void printData (int* pAry, int* pLast);
  9. int* smallest (int* pAry, int* pLast);
  10. void exchange (int* current, int* smallest);
  11.  
  12. int main (void)
  13. {
  14.     int ary[SIZE];
  15.     int* pLast;
  16.  
  17.     pLast = getData (ary, SIZE);
  18.     selectSort (ary, pLast);
  19.     printData (ary, pLast);
  20.  
  21.     return 0;
  22. }
  23.  
  24.  
  25. int* getData (int* pAry, int arySize)
  26. {
  27.     int ioResult;
  28.     int readCnt = 0;
  29.     int* pFill = pAry;
  30.  
  31.     do
  32.     {
  33.         printf("Enter Number Please: ");
  34.         ioResult = scanf("%d", pFill);
  35.         if (ioResult == 1)
  36.         {
  37.             pFill++;
  38.             readCnt++;
  39.         }
  40.     } while (ioResult == 1 && readCnt < arySize);
  41.  
  42.  
  43.  
  44.  
  45.  
  46.     return (--pFill);
  47. }
  48.  
  49. void selectSort (int* pAry, int* pLast)
  50. {
  51.     int* pWalker;
  52.     int* pSmallest;
  53.  
  54.     for (pWalker = pAry; pWalker < pLast; pWalker++)
  55.     {
  56.         pSmallest = smallest (pWalker, pLast);
  57.         exchange (pWalker, pSmallest);
  58.     }
  59.  
  60.     return;
  61. }
  62.  
  63.  
  64. int* smallest (int* pAry, int* pLast)
  65. {
  66.     int* pLooker;
  67.     int* pSmallest;
  68.  
  69.     for (pSmallest = pAry, pLooker = pAry + 1;
  70.         pLooker <= pLast;
  71.         pLooker++)
  72.         if (*pLooker < *pSmallest)
  73.             pSmallest = pLooker;
  74.  
  75.     return pSmallest;
  76. }
  77.  
  78.  
  79. void exchange (int* current, int* smallest)
  80. {
  81.     int temp;
  82.  
  83.     //printf("This is the current value %d and this is the smallest value %d", *current, *smallest);
  84.  
  85.     temp = *smallest;
  86.     *smallest = *current;
  87.     *current = temp;
  88.  
  89.     return;
  90. }
  91.  
  92.  
  93. void printData (int* pAry, int* pLast)
  94. {
  95.     int nmbrPrt;
  96.     int* pPrint;
  97.  
  98.  
  99.  
  100.     printf("\n\nAscending: ");
  101.     for (pPrint = pAry, nmbrPrt = 0;
  102.         pPrint <= pLast;
  103.         nmbrPrt++, pPrint++)
  104.         printf("%4d", *pPrint);
  105.     printf("\n\nDone\n\n ");
  106.  
  107.     return;
  108. }
  109.  
Apr 23 '10 #9
jkmyoung
2,057 Expert 2GB
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.
Apr 23 '10 #10
bryon
12
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
Expand|Select|Wrap|Line Numbers
  1. printf("\n\nOriginal: ");
  2.     for(int i = 0; i < SIZE; i++)
  3.         printf("%4d ", *pAry);
Apr 23 '10 #11
jkmyoung
2,057 Expert 2GB
..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:
Expand|Select|Wrap|Line Numbers
  1.     pLast = getData (ary, SIZE); 
  2.     printOriginal(ary, pLast);
  3.     selectSort (ary, pLast); 
  4.     printData (ary, pLast); 
Apr 23 '10 #12
bryon
12
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


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "stdafx.h"
  3.  
  4. #define SIZE 4
  5.  
  6.  
  7. int* getData (int* pAry, int arySize);
  8. void selectSort (int* pAry, int* pLast);
  9. void printData (int* pAry, int* pLast);
  10. int* smallest (int* pAry, int* pLast);
  11. void exchange (int* current, int* smallest);
  12.  
  13. int main (void)
  14. {
  15.     int ary[SIZE];
  16.     int* pLast;
  17.  
  18.     pLast = getData (ary, SIZE);
  19.     selectSort (ary, pLast);
  20.     printData (ary, pLast);
  21.  
  22.     return 0;
  23. }
  24.  
  25.  
  26. int* getData (int* pAry, int arySize)
  27. {
  28.     int ioResult;
  29.     int readCnt = 0;
  30.     int* pFill = pAry;
  31.  
  32.     do
  33.     {
  34.         printf("Enter Number Please: ");
  35.         ioResult = scanf("%d", pFill);
  36.         if (ioResult == 1)
  37.         {
  38.             pFill++;
  39.             readCnt++;
  40.         }
  41.     } while (ioResult == 1 && readCnt < arySize);
  42.  
  43.     printf("\n\nOriginal: ");
  44.     for(int i = 0; i < SIZE; i++)
  45.         printf("%4d ", *pAry);
  46.  
  47.  
  48.  
  49.     return (--pFill);
  50. }
  51.  
  52. void selectSort (int* pAry, int* pLast)
  53. {
  54.     int* pWalker;
  55.     int* pSmallest;
  56.  
  57.     for (pWalker = pAry; pWalker < pLast; pWalker++)
  58.     {
  59.         pSmallest = smallest (pWalker, pLast);
  60.         exchange (pWalker, pSmallest);
  61.     }
  62.  
  63.     return;
  64. }
  65.  
  66.  
  67. int* smallest (int* pAry, int* pLast)
  68. {
  69.     int* pLooker;
  70.     int* pSmallest;
  71.  
  72.     for (pSmallest = pAry, pLooker = pAry + 1;
  73.         pLooker <= pLast;
  74.         pLooker++)
  75.         if (*pLooker < *pSmallest)
  76.             pSmallest = pLooker;
  77.  
  78.     return pSmallest;
  79. }
  80.  
  81.  
  82. void exchange (int* current, int* smallest)
  83. {
  84.     int temp;
  85.  
  86.     //printf("This is the current value %d and this is the smallest value %d", *current, *smallest);
  87.  
  88.     temp = *smallest;
  89.     *smallest = *current;
  90.     *current = temp;
  91.  
  92.     return;
  93. }
  94.  
  95.  
  96. void printData (int* pAry, int* pLast)
  97. {
  98.     int nmbrPrt;
  99.     int* pPrint;
  100.  
  101.  
  102.  
  103.     printf("\n\nAscending: ");
  104.     for (pPrint = pAry, nmbrPrt = 0;
  105.         pPrint <= pLast;
  106.         nmbrPrt++, pPrint++)
  107.         printf("%4d", *pPrint);
  108.  
  109.     printf("\n\nDescending: ");
  110.     for (pPrint = pLast, nmbrPrt = 0;
  111.         pPrint >= pAry;
  112.         nmbrPrt--, pPrint--)
  113.         printf("%4d", *pPrint);
  114.  
  115.     printf("\n\nDone\n\n ");
  116.  
  117.     return;
  118. }
  119.  
Apr 23 '10 #13
bryon
12
....... 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:
Expand|Select|Wrap|Line Numbers
  1. printf("%4d ", *pAry);
To:
Expand|Select|Wrap|Line Numbers
  1. printf("%4d ", *pAry++);
Apr 23 '10 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: lawrence | last post by:
Is there an easy way to sort a 2 dimensional array alphabetically by the second field in each row? Also, when I use sort() on a two dimensional array, it seems to work a lot like...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
7
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
6
by: CodeRazor | last post by:
My array is int list = new int{20,99,6}; Using a for-loop, how can I order the output of this array. I don't want to use a sorted list. I know this is straightforward but can't figure it...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.