perhaps it's time to post the updated code
perhaps it's time to post the updated code
Code:#include <stdio.h> void read_location(double *x, double *y); int main () { struct coord{ double x; double y; }; struct coord array[20]; int index=0; int choice; //read in coordinates do { read_location( &array[index].x, &array[index].y); index++; }while(index < 5); //test array printf("\n"); printf("%lf", array[3].x); printf("\n"); printf("List of entered x's:\n"); for (index=0; index < 5; index++) { printf("%4lf\n", array[index].x); } int pass; int a; struct coord hold; //menu do { printf("Please choose one of the following actions:\n"); printf("1.) Quit\n"); printf("2.) Sort by x's:\n"); printf("3.) Sort by y's:\n"); printf("4.) Sort by distance:\n"); printf(":"); scanf("%d", &choice); switch(choice) { case 1: return 0; case 2: //sort by x for(pass=1; pass< index; pass++) { for (a=0; a < index -1; a++) { if (array[a].x > array[a+1].x) { hold=array[a]; array[a] = array[a+1]; array[a+1]=hold; } } } printf("ordered x's:\n"); for (a=0; a< index; a++) { printf("%4lf\n", array[a]); } break; case 3: //sort by y's; for(pass=1; pass< index; pass++) { for (a=0; a < index -1; a++) { if (array[a].y > array[a+1].y) { hold=array[a]; array[a] = array[a+1]; array[a+1]=hold; } } } printf("ordered y's:\n"); for (a=0; a< index; a++) { printf("%4lf\n", array[a]); } break; case 4: //sort by distance break; } }while (choice <=4); return 0; } //read in the x and y values void read_location(double *x, double *y) { printf("Enter x's:\n"); scanf("%lf", x); printf("Enter y's:\n"); scanf("%lf", y); }
I can't begin to tell you how much I hate this type of indentation:
All the training that a programmer's eye/brain have built up from scanning conventionally indented code, goes right out the window - Poof!Code:for(pass=1; pass< index; pass++) { for (a=0; a < index -1; a++) { if (array[a].x > array[a+1].x) { hold=array[a]; array[a] = array[a+1]; array[a+1]=hold; } } }
You'll miss a lot of errors if you continue to use it.
Great that it's working - now please indent any future code, with a normal style.
Im wrapping this up and all I have left is sort by distance part. I wrote the code but its not giving me the output Im looking for. It doesnt seem to be doing anything.
the last printf gives me the values of zero for a, x2 for c, and zero for distance.Code://sort by distance printf("Please enter x:\n"); scanf("%lf", x2); printf("Please enter y:\n"); scanf("%lf", y2); printf("\n"); printf("List distances:\n"); for (index =0; index < n; index++) { x3=array[index].x; y3=array[index].y; cx=(x3-x2)*radian; cy= (y3-y2)*radian; //test to see y3 and x3 print correct values printf("%.2lf %.2lf\n", y3, x3); a=sin(cx/2)*sin(cx/2)+cos(x2)*cos(x3)*sin(cy/2)*sin(cy/2); c=2*atan2(sqrt(a), sqrt(1-a)); distance=earth*c; //test a, c, and distance values printf("%.2lf %.2lf %.2lf", a, c, distance); }
What is a supposed to be, and why do you think it's related to distance?
You have one test print statement inside that loop. I suggest adding more of those, to test all your variables, through each step of the loop that you need to, until you find your bug.
Indeed, I like to add a ShowIt() function sometimes, just for that purpose, and then just call that function as needed. Saves a lot of lines of code intruding into my loops.
a and c are used to calculate the distance. Its the formula I found online. Ill take another look and see if I can find the issue.