Jump to content

Need help sorting 2 arrays.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
posto2012

posto2012

    Newbie

  • Members
  • Pip
  • 4 posts
Hello everyone.

I am just starting to learn C. I am trying to write a program where the user will input x and y coordinates. The x's and y's will go into separate arrays. The program is then supposed to sort the coordinates based on their distance from the origin, using the formula sqrt (x*x + y*y), and display them from least to greatest. I have been unable to find any information on this, either in my books, or on the net.

This is what I have so far.

#include <stdio.h>

#include <math.h>


#define MAX_LIST_SIZE 10



float calc_z (int x, int y);

void sort_xy (int x[], int y[]); 

 

int  i,j, choice, coordNumber;

  int x[MAX_LIST_SIZE], y[MAX_LIST_SIZE];



void main () {



  while (1) {

    printf ("Enter (1) to run (2) to quit: ");

    scanf ("%d", &choice);

    if (choice == 2) return;

    else {

      printf("How many coordinates do you want to enter in: ");

      scanf ("%d", &coordNumber);

	

	

	

	  for (i = 0; i < coordNumber; i++) {

	printf ("enter x,y: ");

	scanf ("%d,%d", &x[i], &y[i]);

	  }

  

	  

void sort_xy (int x[], int y[]);

	  

	  

	  for (i = 0; i < coordNumber; i++) {

		  printf("%d %d\n", x[i], y[i]);}

	  }

      

    }

  }



void sort_xy (int x[], int y[], int j)  {

int hold1, hold2;

  for (j = 1; j < coordNumber; j++)  {

    for (i = 0; i< coordNumber-1; i++)  {         

           

      if (sqrt((float)(x[i]*x[i]+y[i]*y[i])) > sqrt((float)(x[i+1]*x[i+1]+y[i+1]*y[i+1])))  {

	hold1= x[i];                         

	x[i]=x[i+1];

	x[i+1] = hold1;

	hold2= y[i];                         

	y[i]=y[i+1];

	y[i+1] = hold2;

     }

    }

  }}

However it prints them in the same order they were typed. I tried writing a separate function to calculate distance from the origin, but I couldn't figure out how to make it work with the sorting function.
If anyone can point me in the right direction it would be greatly appreciated.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
First issue, your indentation needs to be cleaned up for readability:
#include <stdio.h>
#include <math.h>

#define MAX_LIST_SIZE 10


float calc_z (int x, int y);
void sort_xy (int x[], int y[]); 

int  i,j, choice, coordNumber;
int x[MAX_LIST_SIZE], y[MAX_LIST_SIZE];


void main () {
  while (1) {
    printf ("Enter (1) to run (2) to quit: ");
    scanf ("%d", &choice);
    if (choice == 2) return;
    else {
      printf("How many coordinates do you want to enter in: ");
      scanf ("%d", &coordNumber);
      for (i = 0; i < coordNumber; i++) {
        printf ("enter x,y: ");
        scanf ("%d,%d", &x[i], &y[i]);
      }
      void sort_xy (int x[], int y[]);
      for (i = 0; i < coordNumber; i++) {
        printf("%d %d\n", x[i], y[i]);
      }
	  }
  }
}

void sort_xy (int x[], int y[], int j)  {
  int hold1, hold2;
  for (j = 1; j < coordNumber; j++)  {
    for (i = 0; i< coordNumber-1; i++)  {         
      if (sqrt((float)(x[i]*x[i]+y[i]*y[i])) > sqrt((float)(x[i+1]*x[i+1]+y[i+1]*y[i+1])))  {
        hold1= x[i];                         
        x[i]=x[i+1];
        x[i+1] = hold1;
        hold2= y[i];                         
        y[i]=y[i+1];
        y[i+1] = hold2;
      }
    }
  }
}
Second issue, this is NOT valid C code. You have two different parameter lists for sort_xy.
Third issue, as C++ code, you have not called the version of sort_xy that does the work. Actually, you do not call sort_xy anywhere!
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Since your are starting to learn C... First learn the basic steps to how to code.. It will be useful in finding faults easily...

1. Use indentation so that you can differentiate the nesting in your code. I have used Tab indentation...
2. Add some comment lines in-order to understand the purpose for function or variable.. Comment lines are not necessary for small programs but it will help you in understanding the role of it..
3. proper closure of braces.. try using one style of braces.. for example
do {

....

} while (condition);


and 


if( condition ) {

} else if (cond ) {

} else {

}

These are the things that I know.. But there are lot more to learn..

I have commented your code to show how to code. but I haven't solved your problem ...
#include <stdio.h>

#include <math.h>


#define MAX_LIST_SIZE 10


float calc_z (int x, int y); /* don't declare unneceesary functions*/

void sort_xy (int x[], int y[]);


int  i,j, coordNumber;

int x[MAX_LIST_SIZE], y[MAX_LIST_SIZE];



int main () /* don't use void main(), main should return a value.. use int main() */

{


        int choice; /* unless and until you need variables to be available

                         to all the function, dont use them globally */


        while (1) {  /* I have used tab indentation.. */

                printf ("Enter (1) to run (2) to quit: ");

                scanf ("%d", &choice);

                if (choice == 2) {

                        return 0; /* return value should be either 0 or 1

                                         1 - failure, 0 - success */

                } else {

                        printf("How many coordinates do you want to enter in: ");

                        scanf ("%d", &coordNumber);

                } /* you missed this out.. */


                for (i = 0; i < coordNumber; i++) {

                        printf ("enter x,y: ");

                        scanf ("%d,%d", &x[i], &y[i]);

                }

                /*      void sort_xy (int x[], int y[]);


                are you trying to call this function... this is not the correct procedure to call */


                sort_xy(x, y); /* this is the way to call a function */

                for (i = 0; i < coordNumber; i++) {

                        printf("%d %d\n", x[i], y[i]);

                }

        } /* end of while */

} /* end of main */



void sort_xy (int x[], int y[])  { /* function definition should be same as the function declaration */


        int hold1, hold2;

        for (j = 1; j < coordNumber; j++)  {

                for (i = 0; i< coordNumber-1; i++)  {


                        if (sqrt((float)(x[i]*x[i]+y[i]*y[i])) > sqrt((float)(x[i+1]*x[i+1]+y[i+1]*y[i+1])))  {

                                hold1= x[i]; /* dont give extra spaces after ";".. compiler wont show any warnings

                                                but it is not needed */

                                x[i]=x[i+1];

                                x[i+1] = hold1;

                                hold2= y[i];

                                y[i]=y[i+1];

                                y[i+1] = hold2;

                        }

                }

        }

}


I didn't solve because I got an error while compiling
/tmp/ccYREgiP.o: In function `sort_xy':

tes.c:(.text+0x1bf): undefined reference to `sqrt'

tes.c:(.text+0x223): undefined reference to `sqrt'

even though I used math.h, I got this error..

Do anyone know what is the problem.. Or should I have to write a separate function to find the square root..

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Did you add -lm to your compile command?
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz