Last active
October 28, 2018 15:22
-
-
Save sh-zam/07af223e553d64b6677db55ee69f6616 to your computer and use it in GitHub Desktop.
l2 norms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by sh_zam on 28/10/18. | |
// | |
#include <iostream> | |
#include <vector> | |
#include <cmath> | |
using namespace std; | |
int main() { | |
char c = '0'; | |
vector<int> ar1, ar2; | |
string s1, s2; | |
cout << "Enter the numbers on two separate lines\n" | |
"End line with number other than 0 or 1\n"; | |
// fills array 1 with binary data | |
while (c == '0' || c == '1') { | |
cin >> c; | |
s1.push_back(c); | |
} | |
cin.clear(); | |
cin.ignore(INT64_MAX, '\n'); | |
// fills array 2 with binary data; | |
c = '0'; | |
while (c == '0' || c == '1') { | |
cin >> c; | |
s2.push_back(c); | |
} | |
// converts code from string to a vector | |
for (int i = s1.length() - 2; i >= 0; --i) { | |
ar1.push_back(s1[i] - '0'); | |
} | |
for (int i = s2.length() - 2; i >= 0; --i) { | |
ar2.push_back(s2[i] - '0'); | |
} | |
// if the size of either is greater then zeroes are added to front | |
// to prevent segementation fault and reduce complexity | |
if (ar1.size() > ar2.size()) { | |
for (int i = ar2.size(); i < ar1.size(); ++i) { | |
ar2.push_back(0); | |
} | |
} | |
else { | |
for (int i = ar1.size(); i < ar2.size(); ++i) { | |
ar1.push_back(0); | |
} | |
} | |
// compute square root | |
int sum = 0; | |
for (int i = 0; i < ar1.size(); ++i){ | |
if (ar1[i] != ar2[i]) { | |
sum += pow(abs(ar1[i] - ar2[i]), 2); | |
} | |
} | |
cout << endl; | |
cout << sqrt(sum) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment