Ataxia
Ataxia

Reputation: 1

Assign a variable to an array position [C]

I'm starting to code, and I've been scratching my head for a while now. How can I assing a variable to an array position?

I want the user to insert the A, B, C and D, and then store the values as vectors, so I can operate with the line slope later.

int a, b, c, d, e, f, g, h;
int AB, CD, AC, BD;

printf("\n\n  A ___________________ B");
printf("\n   |                   |    ");
printf("\n   |                   |    ");
printf("\n   |                   |    ");
printf("\n   |                   |    ");
printf("\n   |___________________|    ");
printf("\n  C                     D    ");
printf("\n\n\nA(a, b):");
printf("\n-> ");
scanf("%d", &a);
printf("\n-> ");
scanf("%d", &b);
printf("\n\nB(c, d): ");
printf("\n-> ");
scanf("%d", &c);
printf("\n-> ");
scanf("%d", &d);
printf("\n\nC(e, f): ");
printf("\n-> ");
scanf("%d", &e);
printf("\n-> ");
scanf("%d", &f);
printf("\n\nD(g, h): ");
printf("\n-> ");
scanf("%d", &g);
printf("\n-> ");
scanf("%d", &h);

printf("\n\nA = (%d, %d)", a, b);
printf("\n\nB = (%d, %d)", c, d);
printf("\n\nC = (%d, %d)", e, f);
printf("\n\nD = (%d, %d)", g, h);

//VECTOR AB
AB[0]=c-a;
AB[1]=d-b;

//VECTOR CD
CD[0]=g-e;
CD[1]=h-f;

//VECTOR AC
AC[0]=e-a;
AC[1]=f-b;

//VECTOR BD
BD[0]=g-c;
BD[1]=h-d;

The last part of that code does not work, so I assume I can't assign the variables just like that. What can I do to make this work?

Thanks a lot!

Upvotes: 0

Views: 1471

Answers (1)

GMichael
GMichael

Reputation: 2776

You have to declare AB, CD, AC, BD as arrays:

 int AB[2], CD[2], AC[2], BD[2];

2 is the array size. I chose it as I did not see an element index greater that 1 in your code.

It is the basics of C. You have to study language if you want to use it.

Upvotes: 2

Related Questions