summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Abed <michaelabed@gmail.com>2012-02-12 14:59:45 -0500
committerMichael Abed <michaelabed@gmail.com>2012-02-12 14:59:45 -0500
commitd3ca38be90bd3def29e5bd0afedbd3f6b5dc7e3f (patch)
tree94504ea00b97bfbb521830510809c9c8359af5dc
parent76b977d44a517c2f7aade04a45093c5d150ee05b (diff)
downloadec327-lab1-d3ca38be90bd3def29e5bd0afedbd3f6b5dc7e3f.tar.gz
ec327-lab1-d3ca38be90bd3def29e5bd0afedbd3f6b5dc7e3f.tar.bz2
ec327-lab1-d3ca38be90bd3def29e5bd0afedbd3f6b5dc7e3f.zip
solve problem3
-rw-r--r--Makefile7
-rw-r--r--problem3.cpp32
2 files changed, 37 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index fd70bc0..69b5a6e 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
-all: problem1 problem2
+all: problem1 problem2 problem3
problem1: problem1.cpp
g++ -o problem1 problem1.cpp
@@ -7,5 +7,8 @@ problem1: problem1.cpp
problem2: problem2.cpp
g++ -o problem2 problem2.cpp
+problem3: problem3.cpp
+ g++ -o problem3 problem3.cpp
+
clean:
- rm problem1 problem2
+ rm problem1 problem2 problem3
diff --git a/problem3.cpp b/problem3.cpp
index e69de29..518ec0f 100644
--- a/problem3.cpp
+++ b/problem3.cpp
@@ -0,0 +1,32 @@
+
+#include <iostream>
+
+using namespace std;
+
+int main()
+{
+ unsigned int n1, n2;
+ bool good;
+ do {
+ cout << "Enter two integers between 0 and 999999: ";
+ cin >> n1 >> n2;
+ good = n1 <= 999999 && n1 >= 0 && n2 <= 999999 && n2 >= 0;
+ if (!good)
+ cout << "The numbers must be between 0 and 999999" << endl;
+ } while (!good);
+
+ int distance = 0;
+ int d1, d2;
+ int no1 = n1; //original
+ int no2 = n2;
+ while (n1 != 0 || n2 != 0) {
+ d1 = n1 % 10;
+ d2 = n2 % 10;
+ if (d1 != d2)
+ distance++;
+ n1 /= 10;
+ n2 /= 10;
+ }
+ cout << "Hamming distance between " << no1 << " and " << no2 << " is " << distance << endl;
+ return 0;
+}