1

Im trying to write a program that take two arrays on different printf input, and then compute the dot product, so far this is where i get, i need help

#include <stdio.h>
#include <stdio.h>
#define SIZE 8
float droprod(float x[], float y[], int size[]);

int main()
{
    int i ;
    int Vx[SIZE], Vy[SIZE] ;

    printf("Enter 1st vector (q to quit) ");
    for (i=0;i<SIZE;i++)
    { 
                scanf("%d", &Vx[i]);
    }

    printf("Enter 2nd vector (q to quit) ");
    for (i=0;i<SIZE;i++)
    {
                    scanf("%d", &Vy[i]);
    }

printf("vectors [%d] [[%d] ", Vx[SIZE], Vy[SIZE]); // to double check my input, and it is not giving me the right input.

return 0;
4

1 回答 1

2

printf cannot directly print arrays. You must print each element manually using, say, a for loop:

printf("vectors [");
for(i = 0; i < SIZE; i++) {
    if(i != 0) {
        printf(", ");
    }
    printf("%d", Vx[i]);
}
printf("] [");
/* same for the other array */
printf("]");

You could even wrap that logic in a function:

void print_vector(int vec[SIZE]) {
    printf("[");
    for(int i = 0; i < SIZE; i++) {
        if(i != 0) {
            printf(", ");
        }
        printf("%d", vec[i]);
    }
    printf("]");
}

Then your code would look like this:

printf("vectors ");
print_vector(Vx);
printf(" ");
print_vector(Vy);
于 2013-04-03T01:23:09.513 回答