Click here to Skip to main content
15,617,203 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need a program to return the sorted order indices from an array of numbers:

a[4] = {10,7,2,3}; // input
b[4] = {4,3,1,2}; // expected output

In a[4] lowest element is 2 (arrayindex = 2), output value of array index 2 should be append to 1, and 2nd lowest element is 3 (array index = 3), output value of array index 3 should be append to 2.

Can any one help with this program?

Updated 19-Jan-11 3:36am
Espen Harlinn 19-Jan-11 7:50am    
#realJSOP 19-Jan-11 7:53am    
Your examples don't make any sense at all, and your description doesn't help.
Kasson 19-Jan-11 8:10am    
You want your Homework to be done by Us?
Aescleal 19-Jan-11 10:31am    
Can anyone help with the specification?

Since your 'output array' looks containing just the order of the corrensponding item of the input array (when increasingly ordered), you may do:
int i, j;
for (i = 0; i < 4; i++)
  b[i] = 0;
  for (j = 0; j < 4; j++)
    if (a[i] >= a[j]) 

Share this answer
Andrew Brock 20-Jan-11 3:34am    
Sorry, forgot to vote your answer. 5
CPallini 20-Jan-11 3:36am    
Oh it doesn't matter (and you shouldn't rate it 5 if you think it is poorly explained), anyway, thank you :-).
After reading your question 5 times I think I understand what u want.

I was going to try and clarify what you are trying to do better, but it is a rather tricky thing to explain ;P

This code produces the same output as your variable b.
#include <stdio.h>
#include <limits.h>
int main() {
    int pGapArray[] = { 10, 7, 2, 3 }; //Your a[], the input array
    int pNoGapArray[sizeof(pGapArray) / sizeof(pGapArray[0])];
    int nLowestIdx = 0; //This is equal to the index of the lowest number yet to be processed
    int nLowestVal = INT_MAX; //This is equal lowest number yet to be processed
    int nMinLowest = -1; //This is equal to the highest number we have processed
    int nValue = 1; //This is equal to the value we are writing to pNoGapArray
    for (int nIndex = 0; nIndex < sizeof(pGapArray) / sizeof(pGapArray[0]); ++nIndex) {
        nLowestVal = INT_MAX; //Reset the lowest value found
        for (int nSearch = 0; nSearch < sizeof(pGapArray) / sizeof(pGapArray[0]); ++nSearch) {
            if (pGapArray[nSearch] < nLowestVal && pGapArray[nSearch] > nMinLowest) {
                nLowestIdx = nSearch;
                nLowestVal = pGapArray[nSearch];
        nMinLowest = pGapArray[nLowestIdx];
        pNoGapArray[nLowestIdx] = nValue++;
    printf("  pGapArray[] = { ");
    for (int nIndex = 0; nIndex < sizeof(pGapArray) / sizeof(pGapArray[0]); ++nIndex) {
        printf("%d, ", pGapArray[nIndex]);
    printf("pNoGapArray[] = { ");
    for (int nIndex = 0; nIndex < sizeof(pNoGapArray) / sizeof(pNoGapArray[0]); ++nIndex) {
        printf("%d, ", pNoGapArray[nIndex]);
    return 0;
Share this answer
Nish Nishant 19-Jan-11 9:36am    
Voted 5.

Ironic that it was marked as answer and voted a 1!
JF2015 19-Jan-11 9:40am    
Good answer.
Andrew Brock 19-Jan-11 9:43am    
Funny that I think CPallini's answer was better, although not as well explained
CPallini 20-Jan-11 3:33am    
@Andrew: :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900