Given 3 arrays,one accepting Strings the rest integers,lets say:A1[ ],A2[ ],A3[ ].
We get the difference of each element of A2[ ] and A3[ ] to a previously define values.
take for example this code:

//preference.c
#include<stdio.h>
#include<string.h>
typedef char * string; //for STRINGS
char name;
int test; // number of test cases
int HEIGHT,WEIGHT; //for solenn
int suitors; //number of suitors      
string suitor_names[0];
int suitor_h[0];
int suitor_w[0];


void sort(){
}

int main(){

//scanf("%d",&test);{} //for test cases later
scanf("%d%d",&HEIGHT,&WEIGHT); //get solenn 
scanf("%d",&suitors);       //get number of suitors
string suitor_names[suitors]; // Make #ofsuitors strings
suitor_h[suitors];
suitor_w[suitors];
int i,w,h;
        for (i = 0; i < suitors; i++)
        {
              scanf("%s",&name);
        suitor_names[i] = name; 
        scanf("%d%d",&h,&w);        
        suitor_h[i] = h ;
        suitor_w[i] = w ;

    }
    int j,k,l;
    for(j=0;j<suitors-1;j++){
            diff_h[j] = HEIGHT - suitor_h[j];
            diff_h[j] = WEIGHT - suitor_h[j];

    }

    //SORT THE DIFFERENCE FROM LEAST TO HIGHEST 
    // THEN PRINT THE NAMES FROM THE PERSON WITH the least difference to the one w/ highest  



 }

Example Input:

170 60
8
Allen       168     58
Wendell     166     60
Marlon      165     60
Brian       165     60
Christopher 172     57
Rene        175     65
Mark        169     75
Kelvin      170     58

Output:

 Kelvin Mark Allen Christopher Wendell Brian Marlon Rene

Is there any algorithm, even a brute force one to get the answer?

Recommended Answers

All 5 Replies

I don't quite understand how the data is to be sorted. Is it according to column 2 minus the value of column 3?, for example for Kelvin it would be 170-58 = 112

Put the data into a structure then sort the structures

struct data
{
   char name[20];
   int a1;
   int a2;
   int diff;
 };

 struct data array[255];

Read the data into the above array then sort the array on member variable diff.

No. its supposed to be 170 - 170 , then 60 - 58
the first 170 and the 60 came from kelvin. the other 170 and the 58 came from the first input, lets say a person is supposed to hold those values to, let say her name is Solenn.
So Solenn and Kelvin has 0 difference in height and 2 difference in weight, making Kelvin the one with the least difference among the others, so kelvin was first in the list. I hope u get it.:)

Line 38 you probably want diff_w and suitor_w

Sort on the value you want (diff_w or diff_h). Make sure you swap all the arrays. Then output your data for that sort.

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include <math.h>
typedef char * string;
int test; // number of test cases
int HEIGHT,WEIGHT; //for solenn
int suitors; //number of suitors      
// suitor_names[suitors];

void swap(int *a, int *b){

  int tmp;

  tmp = *a;
  *a = *b;
  *b = tmp;
}

void Sort(int a[],int size){
int i, j;

  for (i=0; i<size; i++){
   // for (j=size-1; j>i; j--)
      if(a[i] > a[i+1])
        swap(&a[i], &a[i+1]);
    }
}

int main(){
//scanf("%d",&test);{} //for test cases later
scanf("%d %d",&HEIGHT,&WEIGHT); //get solenn 
printf("%d %d\n",HEIGHT,WEIGHT);
scanf("%d",&suitors);       //get number of suitors
suitors = suitors;
char S1[1000]; // Make 5/#ofsuitors strings
string name[suitors];
int suitor_h[suitors];
int suitor_w[suitors];
int diff_h[suitors];
int diff_w[suitors];

int i,w,h;
char a;
            for (i = 0; i < suitors; ++i)
            {
                //scanf("%s",&name);
            //suitor_names[i] = name; 

            scanf("%s%d%d",S1,&h,&w);
                name[i] = S1;
                suitor_h[i] = h;
                suitor_w[i] = w;        
            //scanf("%d%d",&h,&w);          
            //suitor_h[i] = h;
            //suitor_w[i] = w;
        }
int j,k,l;
        for(j=0;j<suitors;++j){
                diff_h[j] = (HEIGHT - suitor_h[j]);
                if(diff_h[j]<0){diff_h[j] = 0 - diff_h[j]; }
                diff_w[j] = (WEIGHT - suitor_w[j]);
                if(diff_w[j]<0){diff_w[j] = 0 - diff_w[j]; }
        }

Sort(diff_h,suitors-1);
Sort(diff_w,suitors-1);
        for(k=0;k<suitors;++k){
                printf("%d  ",diff_h[k]);
                printf("%d\n",diff_w[k]);
        }

}

Here's my new code.
How do I link the names inputed together with the height and width???
so they can also be sorted accordingly.

format your code better. It very difficult to follow.

How do I link the names inputed together with the height and width???

Well, if you need to swap the name[i] with name[j], what else do you need to do?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.