0

Suppose I have 3 arrays.

arr1 = {96, 100, 104, 108}

arr2 = {8, 4, 4, 16}

Now I create a third array in which I will store elements = arr1[i]/arr2[i]

Therefore arr3 = {12, 25, 26, 9}

Now I want to sort arr3. But also sort arr1 and arr2 in the same order.

Like now arr3 = {9, 12, 25, 26} I also want to sort arr1 and arr2 by the same order meaning

arr1 = {108, 96, 100, 104} arr2 = {16, 8, 4, 4}

So for arr3[2] = 26 I can print/use arr1[2] = 96 && arr2[2] = 8.

I want to be able to access the element of arr1 and arr2 by arr3.

mkrieger1
  • 10,793
  • 4
  • 39
  • 47
  • Create a structure which will have three elements (arr1[i],arr2[i],arr3[i]). make an array of structures and sort them based on third element. Refer this link https://www.geeksforgeeks.org/structure-sorting-in-c/ for more details. – anupam691997 Oct 09 '18 at 11:51

4 Answers4

0

Most (if not all?) sorting will use swapping of elements. When you sort arr3 and swap elements of it, just swap the corresponding elements of the other arrays as well.

So when you swap e.g. arr3[i] with arr3[j], then also swap arr1[i] with arr1[j] and arr2[i] with arr2[j].

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
0

First you define a data structure to hold the values:

typedef struct {
    int dividend, divisor, result;
} intTuple;

Then you define a sorting function for C's qsort

int compare(const intTuple *e1, const intTuple *e2){
    return (e1->result > e2->result)? 1 : 0;
}

Then you initialize the data, perform the division and call the qsort:

int main() {
    intTuple tuples[4] = { {96, 8}, {100, 4}, {104, 8}, {108, 16} };
    for (int i = 0; i < 4; i++) {
        tuples[i].result = tuples[i].dividend / tuples[i].divisor;
    }
    qsort(tuples, 4, sizeof(intTuple), compare);
    for (int i = 0; i < 4; i++) {
        printf("%d ", tuples[i].dividend);
    }
}

Test it here.

Blaze
  • 16,456
  • 1
  • 21
  • 43
  • 1
    How to see compilation warnings and errors on onlinegdb? You should get used to making your compiler emit warnings as in [ideone](https://ideone.com/9ti6Hr). – pmg Oct 09 '18 at 12:53
  • Good question! I don't know. I use Visual studio most of the time, but `qsort` didn't work in it, hence why I resorted to this online tool. There was a "share" option, so I thought I might as well use that. – Blaze Oct 09 '18 at 12:59
0

Don't sort the array, sort the indexes

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

int arr1[] = {96, 100, 104, 108};
int arr2[] = {8, 4, 4, 16};
int arr3[] = {12, 25, 26, 9};

int delta(const void *a, const void *b) {
    const int *aa = a;
    const int *bb = b;
    return arr3[*aa] - arr3[*bb];
}

int main(void) {
    int indexes[] = {0, 1, 2, 3};
    qsort(indexes, 4, sizeof (int), delta);

    printf("arr1:");
    for (int k = 0; k < 4; k++) {
        printf(" %d", arr1[indexes[k]]);
    }
    printf("\narr2:");
    for (int k = 0; k < 4; k++) {
        printf(" %d", arr2[indexes[k]]);
    }
    printf("\n");
}

Sorry for the globals.

pmg
  • 98,431
  • 10
  • 118
  • 189
-1
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

// created a structure containing three elements
struct node
{
    int a;
    int b;
    int c;
} arr[3];

// comparator function which is used to sort structure based on its one property 'c' value
bool compareTwoStudents(node aa, node bb) 
{ 
    return (aa.c < bb.c); 
} 

int main() {
    
    arr[0].a=96;
    arr[1].a=100;
    arr[2].a=108;
    
    arr[0].b=8;
    arr[1].b=4;
    arr[2].b=16;
    
    arr[0].c=12;
    arr[1].c=25;
    arr[2].c=9;
    
    // sort function of c++, takes third argument as the custom comparator function
    sort(arr, arr+3, compareTwoStudents); 
    
    for(int i=0;i<3;i++)
    {
        cout<<arr[i].a<<" "<<arr[i].b<<" "<<arr[i].c<<"\n";
    }
    
    
    return 0;
}
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
anupam691997
  • 310
  • 2
  • 8