plz can anyone help me about this exerxices in information coding theory: Write a program that takes as input any number and returns this number in Haming! Write a program that takes as input a binary number from the user and check if it is correct or incorrect.
what do you mean by correct or incorrect binary number? What is the validation criteria?
Quote: "Write a program that takes as input a binary number from the user and check if it is correct or incorrect." Not sure what you mean by correct or incorrect. Maybe I can help if you clerify.
in which language?
In C++ Hagar. Thank you!
that is code help you to how you calculate hamming number .... #include <iostream> using namespace std; struct NIL{}; template <int H, class T> struct List { enum { HEAD = H}; typedef T TAIL; }; enum COMP_TYPE { kEQ, kLESS, kMORE }; template <int A, int B> struct COMP { enum { val = ( A == B ? kEQ : ( A < B ? kLESS : kMORE ) ) }; }; template <class L> struct PRINTL { static void print() { cout << L::HEAD << " "; PRINTL<typename L::TAIL>::print(); } }; template <> struct PRINTL<NIL> { static void print() { cout << endl; } }; typedef List<1,NIL> START; template <class L, int MUL> struct GROW { typedef List<L::HEAD * MUL, typename GROW<typename L::TAIL, MUL>::RET > RET; }; template <int MUL> struct GROW<NIL, MUL> { typedef NIL RET; }; template <class L1, class L2, int L1_L2> struct MERGE2_; template <class L1, class L2> struct MERGE2 { typedef typename MERGE2_<L1, L2, COMP<L1::HEAD, L2::HEAD>::val >::RET RET; }; template <class L> struct MERGE2<L, NIL> { typedef L RET; }; template <class L> struct MERGE2<NIL,L> { typedef L RET; }; template <class L1, class L2, int L1_L2> // assume EQ struct MERGE2_ { typedef List<L1::HEAD, typename MERGE2<typename L1::TAIL, typename L2::TAIL>::RET > RET; }; template <class L1, class L2> struct MERGE2_<L1, L2, kLESS> // L1::HEAD < L2::HEAD { typedef List<L1::HEAD, typename MERGE2<typename L1::TAIL, L2>::RET > RET; }; template <class L1, class L2> struct MERGE2_<L1, L2, kMORE> // L1::HEAD > L2::HEAD { typedef List<L2::HEAD, typename MERGE2<L1, typename L2::TAIL>::RET > RET; }; template <class L, int IT> struct HAMMING { typedef typename HAMMING< typename MERGE2< typename MERGE2< L, typename GROW<L, 2>::RET >::RET, typename MERGE2< typename GROW<L, 3>::RET, typename GROW<L, 5>::RET>::RET >::RET, IT-1>::RET RET; }; template <class L> struct HAMMING<L, 0> { typedef L RET; }; int main() { PRINTL<HAMMING<START, 5>::RET >::print(); system ("pause"); } hope to help
another code .... #include <stdio.h> #include <stdlib.h> typedef unsigned long long ham; size_t alloc = 0, n = 1; ham *q = 0; void qpush(ham h) { int i, j; if (alloc <= n) { alloc = alloc ? alloc * 2 : 16; q = realloc(q, sizeof(ham) * alloc); } for (i = n++; (j = i/2) && q[j] > h; q[i] = q[j], i = j); q[i] = h; } ham qpop() { int i, j; ham r, t; /* outer loop for skipping duplicates */ for (r = q[1]; n > 1 && r == q[1]; q[i] = t) { /* inner loop is the normal down heap routine */ for (i = 1, t = q[--n]; (j = i * 2) < n;) { if (j + 1 < n && q[j] > q[j+1]) j++; if (t <= q[j]) break; q[i] = q[j], i = j; } } return r; } int main() { int i; ham h; for (qpush(i = 1); i <= 1691; i++) { /* takes smallest value, and queue its multiples */ h = qpop(); qpush(h * 2); qpush(h * 3); qpush(h * 5); if (i <= 20 || i == 1691) printf("%6d: %llu\n", i, h); } /* free(q); */ return 0; }
you can use what you see it easy for you..
Join our real-time social learning platform and learn together with your friends!