1

I am trying to count the number of elements greater than the element on the right side of the array. Here my function goes.

int* SurpassersKing(int input1_size, int* input1,int* output_size)
    {
        int i,k;
        int count[input1_size];
        for  (i = 0; i < input1_size; i++)
            count[i] = 0;


        for ( i = 0; i < input1_size; i++) 
        {
            for ( k = i + 1; k <input1_size; k++) 
                {
                        if (input1[i] < input1[k]) {
                                 count[i]++;
                        }
                }
        } 

        return count;
    }

This is my function where I am counting greater elements in an array.

So in this following code snippet i have wriiten the main function , declaring all the veriable like output_size,counting array ,i ,k as an index for the arrays and printing the stuff , and calling counting function .

int main() {
    int output_size;
    int* output;

    int ip1_size = 0;
    int ip1_i;
    scanf("%d\n", &ip1_size);
    int ip1[ip1_size];
    for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
        int ip1_item;
        scanf("%d", &ip1_item);

        ip1[ip1_i] = ip1_item;
    }
    output = SurpassersKing(ip1_size,ip1,&output_size);
    int output_i;
    for(output_i=0; output_i < output_size; output_i++) {

        printf("%d\n", output[output_i]);

    }
    return 0;
}

but i am not getting the output required so what can i do to improve this.

Mickey Jack
  • 115
  • 1
  • 6

2 Answers2

1

Your logic to calculate the count of the numbers to the right side is correct. Only problem is you can't return arrays like that from a function. Try following:

Replace

int count[input1_size];

with

int * count = malloc(input1_size*sizeof(int));

And then in the main function

Add

free(output);

just before the return statement.

VHS
  • 9,534
  • 3
  • 19
  • 43
1

Oso your code has a few errors i found when i tried to compile.

  1. This is a c code so use #include

  2. Inside the function SurpassersKing you are trying to return array count which is not allowed. Never return local variables unless it is dynamically created.

  3. The output_size never gets initilized.

This is the final code:

#include <stdio.h>
#include<stdlib.h>

int* SurpassersKing(int input1_size, int* input1)
{
    int i,k;
    int * count = (int*)malloc(input1_size*sizeof(int));
    for  (i = 0; i < input1_size; i++)
        *(count + i) = 0;


    for ( i = 0; i < input1_size; i++) 
    {
        for ( k = i + 1; k <input1_size; k++) 
            {
                    if (input1[i] < input1[k]) {
                             count[i]++;
                    }
            }
    } 

    return count;
}

int main() {
// your code goes here
int output_size;
int* output;

int ip1_size = 0;
int ip1_i;
int output_i;

printf("Enter the size:\n");
scanf("%d",&ip1_size);
int ip1[ip1_size];
for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
    scanf("%d",%ip1[ip1_i]);
}
output = SurpassersKing(ip1_size,ip1);
output_size = ip1_size;

for(output_i=0; output_i < output_size; output_i++) {

    printf("%d\n",output[output_i]");

}
return 0;
}
Arvindsinc2
  • 716
  • 7
  • 18
  • If the question is c you cant use iostream, namespace and also inbetween declarations – Arvindsinc2 Mar 31 '17 at 03:47
  • I am not expert in C but can you do `int ip1[ip1_size];` if `ip1_size` is not constant? – Logman Mar 31 '17 at 04:01
  • @DavidBowling...ok – Arvindsinc2 Mar 31 '17 at 04:03
  • @Logman-- this is a variable length array, which was added to the Standard in C99. It was made optional in C11, but seems to be well-supported by implementations of C11. – ad absurdum Mar 31 '17 at 04:03
  • @Arvindsinc2 dynamic table size could be difficult to allocate on function stack. Normally you allocate dynamic memory on heap. – Logman Mar 31 '17 at 04:12
  • I have initialised the output_size in the main() by making output_size = ip1_size...you had declared output_size but never initialised it with a value. – Arvindsinc2 Mar 31 '17 at 04:41