Thread: writing a pair of numbers to an array

  1. #31
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Heres what I have after all your wonderful advice. Everythings works awesome, thanks everyone, time to move on to sorting and all that fun stuff

    Code:
    #include <stdio.h>
    
    //function prototype
    void read_location(double *x, double *y);
    
    //main program
    int main ()
    {
    	struct coord{
    		double x;
    		double y;
    	};
    	
    	struct coord array[20];
    
    	int index=0;
    	do
    	{
    		read_location( &array[index].x, &array[index].y);
    		index++;
    	}while(index < 5);
    	
    	printf("%lf", array[3].x);
    	return 0;
    	}
    
    void read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", x);
    	printf("Enter y:\n");
    	scanf("%lf", y);
    }

  2. #32
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Adak View Post
    I'll be back, shortly.

    What compiler are you using?

    Change %d in printf() to a double format.

    your void function should be void, so remove the int return type. Add a prototype for the function, above main().

    I've got to look up something about doubles, grrrrr!
    Im using a gcc compiler on linux. Could you explain the whole void instead of int thing? Im not finding the info I need on google. They both seem to work fine in this code, is there any advantage to using void?

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    void return type just means there is no return from that function.

    C (especially older compilers), assume that any function with no prototype, will have an int return type.

    Then when there is no return in the function, the compiler should give you an error message.

    This is my take on your program:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    //don't call this - just forces floating point formats to link in 
    //my old Borland Turbo C/C++
    static void forcefloat(float *p)
    { float f = *p;
      forcefloat(&f);
    }
    
    //The is is the function prototype - you should always use them.
    //It's just the first line of the function, put before main();
    void read_location(double *x, double *y);
    
    struct coord{
    	double x;
    	double y;
    };
    
    struct coord array[20];
    
    int main ()
    {
       int index=0, i;
       putchar('\n');
       do
    	{
    		read_location( &array[index].x, &array[index].y);
    		index++;
    	}while(index < 5);
    	
       for(i = 0; i < 5; i++)
    	   printf("\n%d: x = %6lf, y = %6lf", i, array[i].x, array[i].y);
    
       printf("\n\n\t\t\t     Press Enter When Ready ");
       while((i = getchar()) != '\n' );
       i = getchar();
    	return 0;
    }
    
    void read_location(double *x, double *y) 
    {
    
    	printf("Enter x:\n");
    	scanf(" %lf", x);
    	printf("Enter y:\n");
    	scanf(" %lf", y);
    }
    Glad you got it working. Well done!

  4. #34
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Thanks Adak, I couldnt have done it without you guys I have one very last question. I wrote a sorting program that will sort the x's and y's in ascending order:

    Code:
    int pass;
    int a;
    int hold;
    
    for(pass=1; pass< index; pass++)
    {
    	for (a=0; a < index -1; a++)
    	{
    		if (array[a].x > array[a+1].x)
    		{
    			hold=array[a].x;
    			array[a].x = array[a+1].x;
    			array[a+1].x=hold;
    		}
    	}
    }
    
    printf("ordered x's:\n");
    for (a=0; a< index; a++)
    	{
    		printf("%4lf", array[a]);
    	}
    This kind of goes back to one of my original question, how do I display the ys that were paired with the x's?

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Oh you can't just swap the x's. That's the whole point. Swap the entire struct.

  6. #36
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    Oh you can't just swap the x's. That's the whole point. Swap the entire struct.
    My issue is that I will have to sort them by the y's as well. So if I swap the entire struct, will i still be able to sort the y's later on? And to swap the struct, i just remove the ".x" right?

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Of course.

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your hold variable has to be changed to a struct, however. An int won't do.

  9. #39
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    Of course.
    Im getting this error when I remove the ".x"

    [CODEgps.c: In function ‘main’:
    grid.c:61: error: invalid operands to binary > (have ‘struct coord’ and ‘struct coord’)
    grid.c:63: error: incompatible types when assigning to type ‘int’ from type ‘struct coord’
    grid.c:65: error: incompatible types when assigning to type ‘struct coord’ from type ‘int’][/CODE]

  10. #40
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't remove it from the check, just from the swap.

  11. #41
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    You can't remove it from the check, just from the swap.
    Im still getting errors for the swap part:

    Code:
    			hold=array[a];
    			array[a] = array[a+1];
    			array[a+1]=hold;

  12. #42
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you change hold to be a struct instead of just an int?

  13. #43
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Adak View Post
    Did you change hold to be a struct instead of just an int?
    Im assuming this isnt as easy as just writing "struct hold;"?

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your struct has a name that defines it's contents. Use struct and then the name of the type of struct, and then hold.

    Try that!

  15. #45
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Adak View Post
    Your struct has a name that defines it's contents. Use struct and then the name of the type of struct, and then hold.

    Try that!
    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.
    Last edited by wankel; 06-21-2009 at 08:49 PM.

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