Algorithms :: Sorting :: How To numbers 1 to 100 in increasing order? |
|||
| By: progGoon |
Date: 05/06/2003 00:00:00 |
Points: 125 | Status: Answered Quality : Excellent |
|
How can I create a C++ program taht initializes a four by five two dimensional array with the following values: 1-100 and stores them in a one dimensional array in increasing order and the program should display the contents of the sorted array. Buble sort or selected sort should not be used in this program. |
|||
| By: VGR | Date: 05/06/2003 10:06:00 | Type : Answer |
|
| well, algorithm is easy and doesn't require any "sort" algorithm, but it's of no use : a 4x5 matrix holds 20 values and you want to store values 1 to 100 no luck :D Unless you wrote about 20 random values between 1 and 100, oeuf corse :D then the algorithm is easy : // initialize for i from 1 to 100 occurences=0; // fill in for i in lines for j in columns occurences[data[i,j]]++; // display sorted array in increasing order for i from 1 to 100 for j from 1 to occurences display i //end |
|||
| By: ifsu | Date: 06/06/2003 18:57:00 | Type : Comment |
|
| 1 question Do those values between 1 and 100 repeat? . If they do, VGR's answer would be the way to go. If they don't, a faster alternative (simplification) would be trivial. |
|||
| By: VGR | Date: 06/06/2003 20:59:00 | Type : Comment |
|
| I would be glad to see your "simplification" :D :D :D I humbly think my solution is the fastest as it implies no sorting and only increments a number of occurences |
|||
| By: jwenting | Date: 06/06/2003 21:34:00 | Type : Comment |
|
| well, storing 1-100 in an array in increasing order: int arr[100]; for (int i==0;i<100;arr=++i); The 2 dimensional array is not needed if the numbers 1-100 need to be stored in it only to be printed in ascending order lateron from a 1 dimensional array. Even more trivial would be: for (int i==0;1<100;System.out.println(i)); |
|||
| By: jwenting | Date: 06/06/2003 21:35:00 | Type : Comment |
|
| that should of course be for (int i==0;1<100;System.out.println(++i)); |
|||
| By: ifsu | Date: 06/06/2003 23:08:00 | Type : Comment |
|
| > I would be glad to see your "simplification" :D :D :D Pretty simple. If the values don't repeat you don't have to count occurences. Your last 'for' wouldn't be needed ('if' or '(?:)' instead ). |
|||
| By: VGR | Date: 07/06/2003 00:05:00 | Type : Comment |
|
| and how do you expect 20 values from 1 to 100 not to repeat ?!?? :D :D :D :D :D |
|||
| By: VGR | Date: 07/06/2003 00:06:00 | Type : Comment |
|
| @jwenting : you suppress the prerequisites of the problem. Then of course the problem doesn't exist any more. Very clever resolution :D If you were one of my students, you would get 0/20 or E- |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








