writing a pair of numbers to an array

This is a discussion on writing a pair of numbers to an array within the C Programming forums, part of the General Programming Boards category; So im trying to read in a certain amount of numbers as pairs. Im having a little trouble getting started. ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    53

    writing a pair of numbers to an array

    So im trying to read in a certain amount of numbers as pairs. Im having a little trouble getting started. So far I have this function:

    Code:
    int read(double *x, double *y)
    printf("enter number:\n");
    scanf("%lf", &x);
    printf("enter number:\n");
    scanf("%lf", &y);
    How do I put these values into an array that will keep the numbers paired together? Im going to be read several pairs and will then be sorting them and stuff like that, and i need the numbers to stay as pairs. Can anyone get me started?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You make a struct that represents your pairs, and make an array of those structs.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use a struct.

    You can use a struct "straight" up, or with a typedef "chaser". For this example, try one straight up:
    Code:
    struct mystruct {  //mystruct is the name for the *definition* of this struct
       double x;
       double y;
    } NameofStruct; //NameofStruct is the name for the first declaration or instance, of this struct.
    Now make an array of that type of struct, and you're good to go. You'll use the "." operator, to access the x and the y portions of it.

    You'll really be moving your programming skills up a big jump, with this. It's the right way to go, however.
    Last edited by Adak; 06-20-2009 at 02:45 PM.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by Adak View Post
    Use a struct.

    You can use a struct "straight" up, or with a typedef "chaser". For this example, try one straight up:
    Code:
    struct mystruct {  //mystruct is the name for the *definition* of this struct
       double x;
       double y;
    } NameofStruct; //NameofStruct is the name for the first declaration or instance, of this struct.
    Now make an array of that type of struct, and you're good to go. You'll use the "." operator, to access the x and the y portions of it.

    You'll really be moving your programming skills up a big jump, with this. It's the right way to go, however.
    I dont know how you guys learn these things, it seems impossible

    I took you and tabstops advice and created the following struct:

    Code:
    struct coordinates
    {
    double x;
    double y;
    };
    and to put it into an array, im using the following line:

    Code:
    struct coordinates table[19];
    when I compile however it gives me an error that says: "array has incompatible element type". Is this related to the pointers or something completely different?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You won't get an error on that line, I don't think? More context, please.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Heres the code so far:

    Code:
    #include <stdio.h>
    
    //function prototype
    int read_location(double *x, double *y);
    
    struct coordinates {
    	double x;
    	double y;
    };
    
    
    int main ()
    {
     	int choice;	
    	struct coordinates table[19];
    	read_location;
    
    	//loop menu as long as valid option is chosen
    	do
    	{
    		printf("Please choose one of the following actions:\n");
    		printf("1.) Quit\n");
    		printf("2.) Sort by x:\n");
    		printf("3.) Sort by y:\n");
    		printf("4.) Sort by distance:\n");
    		printf(":");
    		scanf("%d", &choice);
    		switch(choice)
    		{
    			case 1:
    				return 0;
    			case 2:
    				//sort by x
    				break;
    			case 3:
    				//sort by y
    				break;
    			case 4:
    				//sort by distance between two points
    				break;
    			}
    	} while (choice <=4);
    
    	return 0;
    }
    
    
    
    int read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", &x);
    	printf("Enter y:\n");
    	scanf("%lf", &x);
    }
    Also im compiling this on linux using gcc, not sure if that matters or not just throwing that out there.
    Last edited by wankel; 06-20-2009 at 03:48 PM. Reason: rearrange code

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to define the type before you use it.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by tabstop View Post
    You have to define the type before you use it.
    haha that was really stupid, I moved the struct to the top and now it compiles fine. But for some reason it wont let me read in the values, it completely skips the read_location function and goes straight to the menu

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should maybe call the read_location function. (Hint: in your current code, you don't.)

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    437
    If you know how many pairs of numbers you will have, you could make a 2d array, where array[i][0] and array[i][1] are paired.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    I think I got it now, but before I continue can you guys take a look at it and let me know if im on the right track? Now it allows me to read the numbers before it goes to the menu, but im not sure how to stick it into an array.

    Code:
    #include <stdio.h>
    
    //function prototype
    int read_location(double *x, double *y);
    
    struct coordinates
    {
    	double x;
    	double y;
    };
    
    struct coordinates number1;
    
    int main ()
    {
     	struct coordinates *heyyo;
            heyyo = &number1;
            number1.x=0;
            number2.y=0;
            int choice;	
    	double num1, num2;
    	struct coordinates table[19];
    	read_location(&num1, &num2);
    
    	//loop menu as long as valid option is chosen
    	do
    	{
    		printf("Please choose one of the following actions:\n");
    		printf("1.) Quit\n");
    		printf("2.) Sort by x:\n");
    		printf("3.) Sort by y:\n");
    		printf("4.) Sort by distance:\n");
    		printf(":");
    		scanf("%d", &choice);
    		switch(choice)
    		{
    			case 1:
    				return 0;
    			case 2:
    				//sort by x
    				break;
    			case 3:
    				//sort by y
    				break;
    			case 4:
    				//sort by distance
    				break;
    			}
    	} while (choice <=4);
    
    	return 0;
    }
    
    
    int read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", &number1.x);
    	printf("Enter y:\n");
    	scanf("%lf", &number1.y);
    }
    Last edited by wankel; 06-20-2009 at 09:41 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", &number1.x);
    	printf("Enter y:\n");
    	scanf("%lf", &number1.y);
    }
    This function should work one of two ways:

    1 - Pass it a pointer to a structure, and fill it in.
    2 - Pass it pointers to individual doubles, and fill those in.

    You are doing half of each.
    Code:
    void foo( stuct bar *baz )
    {
        read into baz->x;
        read into baz->y;
    }
    Or...
    Code:
    void foo( double *bar, double *baz )
    {
        read into bar
        read into baz
    }
    If doing the latter, you need to assign those values to your structure (or pass the structure's elements individually). That might be done like so:
    Code:
    foo( &astruct.x, &astruct.y );
    Or perhaps...
    Code:
    foo( &x, &y );
    astruct.x = x;
    astruct.y = y;
    Pick one.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by quzah View Post
    Code:
    int read_location(double *x, double *y)
    {
    	printf("Enter x:\n");
    	scanf("%lf", &number1.x);
    	printf("Enter y:\n");
    	scanf("%lf", &number1.y);
    }
    This function should work one of two ways:

    1 - Pass it a pointer to a structure, and fill it in.
    2 - Pass it pointers to individual doubles, and fill those in.

    You are doing half of each.
    Code:
    void foo( stuct bar *baz )
    {
        read into baz->x;
        read into baz->y;
    }
    Or...
    Code:
    void foo( double *bar, double *baz )
    {
        read into bar
        read into baz
    }
    If doing the latter, you need to assign those values to your structure (or pass the structure's elements individually). That might be done like so:
    Code:
    foo( &astruct.x, &astruct.y );
    Or perhaps...
    Code:
    foo( &x, &y );
    astruct.x = x;
    astruct.y = y;
    Pick one.


    Quzah.
    Im not quite sure I follow. So assuming I go with the second option, I'll have the following:

    Code:
    read_location(&coordinates.x, &coordinates.y);
    Or am I looking at this completely wrong?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You have the right idea and syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    You have the right idea and syntax.
    Im getting a "coordinates undeclared, first use in function" error when I try to compile it.

    I changed it to:

    read_location(&number1.x, &number1.y);

    it compiles fine but im not sure if thats the same thing or not
    Last edited by wankel; 06-21-2009 at 12:53 AM.

Page 1 of 4 1234 LastLast
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, 12: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, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21