/* Two vectors are linearly independent if and only if there is no scalar value by which one can multiply the first vector to get the second vector. If two vectors are not linearly independent, then they are linearly dependent. To illustrate linear dependence, let v1 be the vector (1, 2, -1) and let v2 be the vector (2, 4, -2). The two vectors v1 and v2 are linearly dependent because we can multiply v1 by a scalar and get a result that is equivalent to v2. In this case that scalar is equal to 2: 2 * v1 = 2 * (1, 2, -1) = (2*1, 2*2, 2*(-1)) = (2, 4, -2) = v2 To illustrate linear independence, let v1 be the vector (1, 2, -1), let v2 be the vector (1, 4, -1). The two vectors are linearly independent because there is no scalar by which we can multiply v1 to get v2. Write a C program that prints whether two vectors with the same number of elements are either linearly independent or linearly dependent. The program must first read the size, N, of the vectors. Next, the program must read the elements of the two vectors as doubles. Finally, the program must print the result. See the sample runs shown below. Hint: The vectors are linearly dependent if either vector is all zeros. Watch out for other cases containing zeros. ============= START OF SAMPLE RUN ======================= 3 1 2 -1 1 4 -1 The two vectors are linearly independent. ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= 3 1 0 0 0 1 0 The two vectors are linearly independent. ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= 5 0 2.2 3.0 -0.1 2.1 0 -2.2 -3.0 0.1 -2.1 The two vectors are linearly dependent. ============= END OF SAMPLE RUN ======================= ============= START OF SAMPLE RUN ======================= 8 1 2 2 3.5 4 -1 -1 0 2 4 4 7 8 -2 -2 0 The two vectors are linearly dependent. ============= END OF SAMPLE RUN ======================= */ #include #include #define LIN_DEP_STR "The two vectors are linearly dependent." #define LIN_INDEP_STR "The two vectors are linearly independent." int is_equiv(double, double); int is_zero_vector(double[], size_t); int main(const int argc, const char* argv[]) { int idx; int vector_size = 0; double scalar; scanf("%d", &vector_size); double v1[vector_size]; double v2[vector_size]; // Scan values into the first vector. for (idx = 0; idx < vector_size; idx++) { scanf("%lf", &scalar); v1[idx] = scalar; } // Scan values into the second vector. for (idx = 0; idx < vector_size; idx++) { scanf("%lf", &scalar); v2[idx] = scalar; } // Check for cases contianing the zero-vector. if (is_zero_vector(v1, vector_size) || is_zero_vector(v2, vector_size)) { // One of the vectors is the zero vector; linearly dependent. puts(LIN_DEP_STR); return 0; } // Find `scalar', a ratio between each pair of elements of v2 and v1. for (idx = 0; idx < vector_size; idx++) { if (v1[idx] == 0.0) { if (v2[idx] == 0.0) { // Both values are zero; we cannot set `scalar'. continue; } else { // Exactly one of a pair is zero; independent. puts(LIN_INDEP_STR); return 0; } } else { if (v2[idx] == 0.0) { // Exactly one of a pair is zero; independent. puts(LIN_INDEP_STR); return 0; } else { // Neither of the pair is zero; set scalar. scalar = v2[idx] / v1[idx]; idx++; break; } } } // Check for the rest of the pairs that scalar * v1[idx] = v2[idx]. while (idx < vector_size) { if ( ! is_equiv(scalar * v1[idx], v2[idx])) { puts(LIN_INDEP_STR); return 0; } idx++; } puts(LIN_DEP_STR); return 0; } int is_equiv(double x, double y) { static const double MAX_DELTA = 0.0001; double delta = fabs(x - y); return (delta <= MAX_DELTA ? 1 : 0); } int is_zero_vector(double vector[], size_t length) { // Assume that it is a zero-vector until a non-zero element is found. int idx; for (idx = 0; idx < length; idx++) { if (vector[idx] != 0.0) return 0; } return 1; }