Thread: writing a pair of numbers to an array

  1. #46
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    perhaps it's time to post the updated code

  2. #47
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by wankel View Post
    will "struct coord hold;" do the trick? Im currently using that right now and its giving me the same error so I think im doing it wrong haha

    EDIT: nevermind, that worked

    But when I sort the x's, it still only gives me the x's and doesnt display there corresponding y points.
    You have to specify what you want to print. If you want to print the x's and the y's, then you have to say as much in your print statement.

  3. #48
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by itCbitC View Post
    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);
    }

  4. #49
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    You have to specify what you want to print. If you want to print the x's and the y's, then you have to say as much in your print statement.
    I feel so dumb. I added the y's to the printf statement and everything is showing up as it should.

  5. #50
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I can't begin to tell you how much I hate this type of indentation:

    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;
    					}
    					}
    					}
    All the training that a programmer's eye/brain have built up from scanning conventionally indented code, goes right out the window - Poof!

    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.

  6. #51
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    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.


    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);
                                             }
    the last printf gives me the values of zero for a, x2 for c, and zero for distance.

  7. #52
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is a supposed to be, and why do you think it's related to distance?

  8. #53
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  9. #54
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Writing to my 2D Array
    By qwertysingh in forum C Programming
    Replies: 0
    Last Post: 04-12-2009, 01:16 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM