-1

I want to rank an array such as the following:

mat[0] = 300  
mat[1] = 200  
mat[2] = 360  
mat[3] = 230  
mat[4] = 290  

The rank depends on the array above (max number gets rank 1 etc.):

rank[0] = 2  
rank[1] = 5  
rank[2] = 1  
rank[3] = 4  
rank[4] = 3  

How do I rank without moving the array in any kind of sorting algorithm or by having different input:

mat[0] = 400 
mat[1] = 100  
mat[2] = 220  
mat[3] = 230  
mat[4] = 50  

the rank would be:

rank[0] = 1  
rank[1] = 4  
rank[2] = 3  
rank[3] = 2  
rank[4] = 5

How would I do this in C?

Muschel
  • 340
  • 5
  • 11
learningbyexample
  • 1,527
  • 2
  • 21
  • 42
  • 2
    Have you tried something? Can you show us your code? – Marievi Mar 29 '16 at 05:34
  • related: http://stackoverflow.com/questions/13473/how-does-one-rank-an-array-sort-by-value-with-a-twist?rq=1 – jeremy Mar 29 '16 at 05:35
  • I don't want to use built in functions from C – learningbyexample Mar 29 '16 at 05:38
  • @Marievi I don't have any code because I only know sorting arrays to move the values, not leave them in the same place while assigning a new rank to them. – learningbyexample Mar 29 '16 at 05:40
  • Think about constructing a tree from your array, one element at a time. Each node of the tree is its value. Its breadth and depth determines its rank. You could use a treesort (https://en.wikipedia.org/wiki/Tree_sort). As you read out the nodes of a tree, you get back the elements in order of rank (http://www.martinbroadhurst.com/cpp-sorting.html#tree-sort). – Alex Reynolds Mar 29 '16 at 06:08
  • This isn't a code writing service. Think or research an algorithm, code it in C, and if you run into problems you can ask clear, specific questions about it here. – juanchopanza Mar 29 '16 at 07:07

3 Answers3

1

One way is to create an array or list of structs, with each struct containing 2 variables:

  • The value in your array (the sort key) e.g 300, 200, 360 etc
  • The index of the element in your array e.g. 0, 1, 2 etc

Sort the array/list with the efficient sorting algorithm of your choice.

Then, iterate over the array/list in sorted order, keeping a count which you increment by one each time, and for each struct, read out the index of the element in the original array and set that element to the value of the count.

samgak
  • 23,944
  • 4
  • 60
  • 82
0

What you can do is iterate through the array and rank the numbers into your second array. Try this piece of code, assuming that length is the length of mat and rank (as I can see, it is 5 in your case) :

int i, j, max = -1;

//First initialize rank array to zeros
for (i = 0; i < length; i++)
   rank[i] = 0;

for (i = 0; i < length; i++)
{
    for (j = 0; j < length; j++)
    {
        if ( (mat[j] > max) && (rank[j] == 0) )
        {
            max = mat[j];
            rank[j] = j+1;
        }
    }
    max = -1;
}
Marievi
  • 4,951
  • 1
  • 16
  • 33
-2

you must improve the program. try this:

#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

void greater(int[]);
int pos;
int cont = 0;

int main()
{
    int mat[SIZE] = {};
    int rank[SIZE] = {};

    for(int c = 0 ; c < SIZE; c++)
    {
        printf("numero %d:", c + 1);
        scanf("%d", &mat[c]);
    }

    printf("\n");

    for(int c = 0 ; c < SIZE; c++)
        printf("%d\t", mat[c]);


    int matCopy[SIZE];

    for (int i = 0; i < SIZE; i++)
        matCopy[i] = mat[i];



    for(int c = 0 ; c < SIZE; c++)
    {
        greater(matCopy);
        rank[pos] = c + 1 ;
    }

    printf("\n");
    for(int c = 0 ; c < SIZE; c++)
        printf("%d\t", rank[c]);

    return 0;
}


void greater(int v[])
{
    int greater = -9999;

    for(int c = 0 ; c < SIZE; c++)
    {
        if(v[c] > greater)
        {
            greater = v[c];
            pos = c;
        }

    }
    v[pos] = 0;
}
sonOFAthena
  • 108
  • 1
  • 6