#include #include using namespace std; const int SIZE = 10; int main() { float x[SIZE]; float y[SIZE]; float sum_x2 = 0, sum_y2 = 0; float lengthx, lengthy; int i, n; // See how many elements we have cout << "How many elements in x and y?" << endl; cin >> n; // Check we have room for them if (n > SIZE) { cout << "We don't have room for that many" << endl; return (0); } // Read in x cout << "Enter the elements of x:" << endl; for (i = 0; i < n; i++) { cin >> x[i]; } // Read in y cout << "Enter the elements of y:" << endl; for (i = 0; i < n; i++) { cin >> y[i]; } // Sum the square of each element in x and y for (i = 0; i < n; i++) { sum_x2 += x[i] * x[i]; sum_y2 += y[i] * y[i]; } // The length is the square root of the sum of the squares lengthx = sqrt(sum_x2); lengthy = sqrt(sum_y2); // Print out the lengths cout << "Length x: " << lengthx << endl; cout << "Length y: " << lengthy << endl; // Divide each element by the length of its vector for (i = 0; i < n; i++) { x[i] /= lengthx; y[i] /= lengthy; } // Reset sums sum_x2 = sum_y2 = 0; // Recompute the sum of the squares for (i = 0; i < n; i++) { sum_x2 += x[i] * x[i]; sum_y2 += y[i] * y[i]; } // Recompute the lengths lengthx = sqrt(sum_x2); lengthy = sqrt(sum_y2); // Printf out the lengths (they should both be 1) cout << "Length x: " << lengthx << endl; cout << "Length y: " << lengthy << endl; return (0); }