# Thread: writing a pair of numbers to an array

1. ## 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. You make a struct that represents your pairs, and make an array of those structs.

3. 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.

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. You won't get an error on that line, I don't think? More context, please.

6. Heres the code so far:

Code:
```#include <stdio.h>

//function prototype

struct coordinates {
double x;
double y;
};

int main ()
{
int choice;
struct coordinates table[19];

//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;
}

{
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.

7. You have to define the type before you use it.

8. Originally Posted by tabstop
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. You should maybe call the read_location function. (Hint: in your current code, you don't.)

10. 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. 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

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];

//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;
}

{
printf("Enter x:\n");
scanf("%lf", &number1.x);
printf("Enter y:\n");
scanf("%lf", &number1.y);
}```

12. 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 )
{
}```
Or...
Code:
```void foo( double *bar, double *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.

13. Originally Posted by quzah
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 )
{
}```
Or...
Code:
```void foo( double *bar, double *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. You have the right idea and syntax.

15. Originally Posted by laserlight
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: