We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Inputing a File of 2d points and then sorting

Jaziar
Jaziar asked
on
Medium Priority
255 Views
Last Modified: 2012-05-06
I have a program that will take in 2d points and then compare the points. Example in the code

struct Point2D p1 = {1,2};
struct Point2D p2 = {1,2};

if (lessThan(&p1, &p2)) {
       puts("one point is less than the other");
   }
   else {
    puts("Equal");
   }

This code is working great.  But what I need is to take a file of points.  
Example:
1,2
1,4
1,3
2,4
1,6
2,6
3,1

As the file is parsed the values are ran through the  lesThan function.  
When the file is finsihed the values are in order from the smalles to the largest.

Example  
1,2
1,3
1,4
1,6
2,4
2,6
3,1


I am very rusty with C so I am not sure how I would do this.  I can parse a file of chars or strings, but not sure how to handle struct point values.
struct Point2D {
   int x;
   int y;
};
 
 int comparePoint2D ( struct Point2D *p1, struct Point2D *p2) {
   int result = p1->x == p2->x && p1->y == p2->y;
   return result;
}
 
int lessThan(const struct Point2D *p1, const struct Point2D *p2) {
   int result = (p1->x < p2->x) || (p1->x == p2->x && p1->y < p2->y);
   return result;   
} 
 
 
 
int main (void) {
   struct Point2D p1 = {1,2};
   struct Point2D p2 = {1,2};
  
   if (comparePoint2D(&p1, &p2)) {
       puts("Points are equal");
   } 
   else {
    puts("Points are not equal");
   }
   
   if (lessThan(&p1, &p2)) {
       puts("one point is less than the other");
   } 
   else {
    puts("Equal");
   }
   return 0;
}

Open in new window

Comment
Watch Question

Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
Hi Jaziar,

Sorting a linked list is a bit challenging.  The program will typically waste a lot of cycles with list management and list position for most kinds of sorts.  (Sorting a linked list can certainly be done.)

It might be easiest to generate a table of Point2D structures and populate it with the points.  Then call one of the available sort routines (like qsort) to sort the points.  Then it's a simple matter of putting the points back into list form.


Kent
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
int cmpPoint2D ( const void *p1, const void *p2) {
  return lessThan((struct Point2D *)p2,(struct Point2D *)p1)-lessThan((struct Point2D *)p1,(struct Point2D *)p2);
}


int main (void) {
struct Point2D list[]={
  {1,2},
  {1,4},
  {1,3},
  {2,4},
  {1,6},
  {2,6},
  {3,1},
};
 qsort(list,sizeof(list)/sizeof(list[0]),sizeof(list[0]),cmpPoint2D);
 for( int i=0;i<sizeof(list)/sizeof(list[0]); i++ ){
   printf("%d,%d\n",list[i].x,list[i].y);
 }
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> I can parse a file of chars or strings, but not sure how to handle struct point values.

Write a function that reads one Point2D struct from the file. The function reads one line from the input file, and then parses that line to get two integer values from it, separated by a comma. It uses those two integer values to construct a Point2D object, and returns it.

This function can be called in a loop until the whole file is read. Each Point2D object read from the file can be stored into an array.

You can make use of fgets to read a line from the file, and sscanf or strtok (with atoi) or strtol to extract the integer values from the line :

        http://www.cplusplus.com/reference/clibrary/cstdio/fgets.html

        http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html

        http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
        http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

        http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.