简体   繁体   English

将变量分配给数组位置[C]

[英]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. 我希望用户插入A,B,C和D,然后将值存储为矢量,以便以后可以使用线斜率进行操作。

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! 非常感谢!

You have to declare AB, CD, AC, BD as arrays: 您必须将AB,CD,AC,BD声明为数组:

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

2 is the array size. 2是数组大小。 I chose it as I did not see an element index greater that 1 in your code. 我选择它是因为在您的代码中没有看到大于1的元素索引。

It is the basics of C. You have to study language if you want to use it. 这是C的基础知识。如果要使用它,则必须学习语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM