Search code examples
cindices

How to get top indices of float array in C and pairing it with strings stored in an another array


I updated the code to find top 5 indices of a float array. Some how it is only updating top[0]th element for max indices. In the mentioned example below max indices is like below top[0] = 9, top[1] =7, top[2]=5 and so on. But it is updating top[0] only.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main() {
    double *arr =malloc(sizeof(double)*10);
    int N=10;
    int n =5;
    int *top =malloc(sizeof(int)*10);
    arr[0] = 0.123; 
    arr[1] = 0.345;
    arr[2] = 0.445;
    arr[3] = 0.545;
    arr[4] = 0.645;
    arr[5] = 0.745;
    arr[6] = 0.542;
    arr[7] = 0.945;
    arr[8] = 0.145;
    arr[9] = 0.995;
    int top_count = 0;
    int i;
    for (i=0;i<N;++i) {
        // invariant: arr[top[0]] >= arr[top[1]] >= .... >= arr[top[top_count-1]]
        // are the indices of the top_count larger values in arr[0],...,arr[i-1]
        // top_count = max(i,n);
        int k;
        for (k=top_count;k>0 && arr[i]>arr[top[k-1]];k--);
        // i should be inserted in position k
        if (k>=n) continue; // element arr[i] is not in the top n
        // shift elements from k to top_count
        printf("6:: Iam here\n");fflush(stdout);
        int j=top_count;
        if (j>n-1) { // top array is already full
                j=n-1;
        } else { // increase top array
            top_count++;
        }

        for (;j>k;j--) {
            top[j]=top[j-1];
        }

        // insert i
        top[k] = i;
        printf("top[%0d] = %0d\n",k,top[k]);

        printf("top_count=%0d\n",top_count);
    }
    return top_count;
}

Solution

  • int top_elems(double *arr, int N, int *top, int n);
    
    int top_count = top_elems(&output, 10, top,5);
    
    • output already decomposes to double * do you are passing the address_ of a pointer to double into a function that should take a pointer to double
    • top is not initialised before the function call so is undefined the first use of it dereferences this undefined pointer
    • ... yeah, I'm not going to go further. your code has some serious but simple to spot issues. If you had made a MCV example, you should have seen some of of them, instead of just dumping your code here.

    After code changes:

    I updated question to update the top five indices of float array into top[] array. but it is only updating top[0]th element. anything wrong with the code?

    Well, I updated the indentation of the code in the question to make it more readable.

    You will notice that there is no indentation after this for loop:

    for (k=top_count;k>0 && arr[i]>arr[top[k-1]];k--);
    

    This is because the loop body of this loop is:

    ;
    

    That's it. it is just an empty statement that does nothing. All the loop does is set variables, which are then modified in the rest of the code that follows, but as they are not within a loop, they are only executed the once.

    As the loop goes from top_count down to 0, it should be obvious why it is just the first index that is modified.