'How to sort a 2d array based on these conditions ? (The conditions are given within the code)

/*
 The program must accept N integers as the input.  Each integer is
 given a weight.  The program must sort the integers in ascending
 order based on their weight and print the integers along with their
 weights as the output as given in the Example Input/Output
 sections.  The weight of each integer is calculated based on the
 conditions given below.


Conditions:
Weight = 5 if it is a perfect cube.
Weight = 4 if it is a multiple of 4 and divisible by 6.
Weight = 3 if it is a prime number.

Hint: Use stable sort (insertion sort, bubble sort or merge sort).

Boundary Conditions:
1 <= N <= 1000

Input Format:
The first line contains N.
The second line contains N integers separated by a space.

Output Format:
The first line contains integers with their weight as given in the Example Input/Output sections.

Example Input/Output 1:
Input:
7
10 36 54 89 12 216 27

Output:
<10,0>,<54,0>,<89,3>,<36,4>,<12,4>,<27,5>,<216,9>

Example Input/Output 2:
Input:
10
12 18 16 64 14 30 37 27 343 216

Output:
<18,0>,<16,0>,<14,0>,<30,0>,<37,3>,<12,4>,<64,5>,<27,5>,<343,5>,<216,9>
*/

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

int perfcube(int n)
{
    int cubert = cbrt(n);
    if (cubert * cubert * cubert == n)
    {
        return 1;
    }
    else
        return 0;
}

int divis(int n)
{
    if (n % 4 == 0 && n % 6 == 0)
    {
        return 1;
    }
    return 0;
}

int prime(int n)
{
    int count = 0;
    for (int i = 1; i <= n; i++)
    {
        if (n % i == 0)
        {
            count++;
        }
    }

    if (count == 2)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{

    int n;
    scanf("%d", &n);

    int a[n];
    int b[n][2];

    // scanning n variables into array a
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }

    // copying rows of a(1d array) to b(2d array)
    int l = 0; // variable to traverse 1d array without its own loop
    // traverse 2d array
    for (int j = 0; j < n; j++)
    {
        for (int k = 0; k < 2; k++)
        {
            if (k == 0)
            {
                // if k = 0 that is first col then store 1st col value of 1d array to 2d array
                b[j][k] = a[l++];
            }
            else
            {
                // if other cols come then skip it
                continue;
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            if (j == 0)
            {
                if (perfcube(b[i][j]))
                {
                    b[i][j + 1] += 5;
                }
                if (divis(b[i][j]))
                {
                    b[i][j + 1] += 4;
                }
                if (prime(b[i][j]))
                {
                    b[i][j + 1] += 3;
                }
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            printf("<%d,>", b[i][j]);
        }
        printf("\n");
    }

    return (0);
}

I tried approaching the problem like this and ended up with an output like this. Please help me proceed from here.

    Output
    <10,><0,>
    <36,><4,>
    <54,><0,>
    <89,><3,>
    <12,><4,>
    <216,><9,>
    <27,><5,>

I am new to programming

I have tried approaching the problem like this and ended up with an output like that.

Please help me proceed from here.

I am not allowed to use pointers or functions like qsort

How do I sort these in that format and print it Output of the program that I ended up with.

The output should match the question.



Solution 1:[1]

Sorting, at its core, centers around the comparison of two items. For example, if A < B, then A should come before B.

For example, we can reorder

3 5 2 1 4

to

1 2 3 4 5

We can see it is correct because each adjacent pair maintains a ? relationship.

This relationship is a comparison function. The one used for your standard sorting looks something like this:

bool compare( int a, int b )
{
  return a <= b;
}

 
What your homework is asking you to do is change the comparison function to not compare the values directly, but compare the results of a function applied to them:

bool compare( int a, int b )
{
  return weight_function( a ) <= weight_function( b );
}

You must write and implement the weight function, then use it in your sorting algorithm.

(Your algorithm probably has an if (a <= b) in it somewhere, which you could rewrite as if (compare( a, b ))... but you don’t need to do that, just use the weight functions properly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dúthomhas