问题如下:给定矢量大小和该大小的两个矢量“a”和“b”(全部由用户输入), 计算这两个向量的和与差作为向量“c”。要做到这一点, 加/减两个向量的对应坐标。 您必须实现以下功能:
void addVectors(double a [],double b [],double c [],int size);
void subtractVectors(double a [],double b [],double c [],int size);
我写的是:
#include <stdio.h>
void addVectors(double a[], double b[], double c[], int size){
printf("Adding the vectors we get: \n");
int i;
for(i=0; i<size; i++){
c[i] = b[i] + a[i];
printf("%f, ", c[i]);}}
void subtractVectors(double a[], double b[], double c[], int size){
printf("\nSubtracting the vectors we get: \n");
int i;
for(i=0; i<size; i++){
c[i] = b[i] - a[i];
printf("%f, ", c[i]);}}
int main()
{
int size, i;
printf("Please enter the size of your vectors: ");
scanf("%d", &size);
double a[size], b[size], c[size];
printf("Please enter the coordinates of vector A: ");
for (i = 0; i < size; i++){
scanf("%f", &a[i]);}
printf("Please enter the coordinates of vector B: ");
for (i = 0; i < size; i++){
scanf("%f", &b[i]);}
addVectors(a, b, c, size);
subtractVectors(a, b, c, size);
return 0;
}
如果有人能让我知道为什么我的输出全部为零,那将非常感激。