1. (30%) Write a function to convert a string of binary digits to its corresponding decimal value.
  2. (30%) Write a function to convert a string of hexadecimal digits to its corresponding decimal value.
  3. (30%) Design a function
    int gcd(int a, int b)
    for the following program:
    #include <iostream>
    using std::cout;
    using std::endl;

    int gcd(int a, int b);

    void show_gcd(int a, int b)
    {
        cout << "gcd(" << a << ',' << b << ")=";
        cout << gcd(a, b) << endl;
        return;
    }

    int main()
    {
        int data[] = { 4, 2, 2, 4, 6, 4, 12, 8, 39, 6, 39, 7, 48, 36 };
        int* p = data;
        for (int i=0; i< sizeof(data)/sizeof(data[0]); i+=2)
            show_gcd(data[i], data[i+1]);
        return 0;
    }
    1. Save the above program in "m2-3f.cpp".
    2. Save the gcd() function you developed in "gcd.cpp".
    3. Compile the program with the command "g++ m2-3f.cpp gcd.cpp".  By default, the executable file will be "a.out".
  4. (30%) Consider a tranposition cipher called "Scytale" which was popularly adopted by the ancient Greeks and Spartans.
    1. Each general was given a stick of the same diameter. To encrypt a message, he wraps a strip of paper (parchment in ancient time) around the stick (avoid overlapping and gaps).
    2. Then he starts writing his message along the length of the stick, one character per pass of the paper.
    3. This is equivalent to have a matrix with a fixed height. The general writes his message across the rows. When the receiver got the unwrapped strip, the strip actually shows characters along the columns. For example,
      H E L P M
      E I A M U
      N D E R A
      T T A C K
      will be sent as "HENTEIDTLAEAPMRCMUAK".
    4. To decrypt it, the receiver wraps the strip along a stick with the same diameter, and read the message along the rows.
    5. Nowadays, this task can be quickly done by computers. Suppose you receive a message "Ataaohecb tmfrseot e o nuhG Tnse" and your know the height of the matrix is 4, write a program to decrypt it.
    6. You may test your program with a message "ABCDABCDABCDABCD". For key = 4, it should be decrypted as "AAAABBBBCCCCDDDD".