CPSC 332 / Section 500

Dr. Salih Yurttas - March 5, 2008

Test 1


[ You can use either "C++" or "C#" or "Java" code to answer ]

  1. (100 points)
    
    // a_01.cpp
    
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    const string B = "01";
    
    int main(int argc, char* argv[]) {
      // init
      vector<string> given;
    
      given.push_back("01010101");
      given.push_back("11011001");
      given.push_back("10001000");
      given.push_back("00010011");
    
      // compute
      vector< vector<int> > computed;
    
      int n = given.size();
      for(int i=0; i<n; ++i) {
        string t = given.at(i);
    
        // count
        vector<int> counts(2,0);
        char b = B.at(0);
    
        int l = t.length();
        for(int j=0; j<l; ++j)
          if(t.at(j)==b) ++counts.at(0);
          else ++counts.at(1);
    
        computed.push_back(counts);
      }
    
      // output
      int m = computed.size();
      cout << endl;
      for(int i=0; i<m; ++i) {
        vector<int> v = computed.at(i);
        cout << v.at(0) << endl;
      }
    
      cout << endl;
    
      // output
      for(int i=0; i<m; ++i) {
        vector<int> v = computed.at(i);
        cout << v.at(1) << endl;
      }
    }
    
    


    
    // A01.cs
    
    using System;
    using System.Collections;
    
    public class A01
    {
      private static string B = "01";
    
      public static void Main(string[] args)
      {
        // init
        ArrayList Given = new ArrayList();
    
        Given.Add("01010101");
        Given.Add("11011001");
        Given.Add("10001000");
        Given.Add("00010011");
    
        // compute
        ArrayList Computed = new ArrayList();
    
        int N = Given.Count;
    
        for(int i=0; i<N; ++i) 
        {
          string T = (string)Given[i];
    
          // count
          ArrayList Counts = new ArrayList();
    
          char b = B[0];
    
          int[] k = new int[]{0,0};
    
          int L = T.Length;
          for(int j=0; j<L; ++j)
            if(T[j]==b) ++k[0];
            else ++k[1];
    
          Counts.Add(k[0]);
          Counts.Add(k[1]);
    
          Computed.Add(Counts);
        }
    
        // output
        int M = Computed.Count;
    
        Console.WriteLine();
    
        for(int i=0; i<M; i++) {
          ArrayList AL = (ArrayList)Computed[i];
          Console.WriteLine("{0}", AL[0]);
        }
    
        Console.WriteLine();
    
        // output
        for(int i=0; i<M; i++) {
          ArrayList AL = (ArrayList)Computed[i];
          Console.WriteLine("{0}", AL[1]);
        }
      }
    
    }
    
    


    
    // A01.java
    
    import java.util.*;
    
    public class A01 {
    
      private final static String B = "01";
    
      public static void main(String[] args) {
        // init
        ArrayList<String> given = new ArrayList<String>();
    
        given.add("01010101");
        given.add("11011001");
        given.add("10001000");
        given.add("00010011");
    
        // compute
        ArrayList<ArrayList<Integer>> computed = new ArrayList<ArrayList<Integer>>();
    
        int n = given.size();
    
        for(int i=0; i<n; ++i) {
          String t = given.get(i);
    
          // count
          ArrayList<Integer> counts = new ArrayList<Integer>();
    
          char b = B.charAt(0);
    
          int[] k = {0,0};
    
          int l = t.length();
          for(int j=0; j<l; ++j)
            if(t.charAt(j)==b) ++k[0];
            else ++k[1];
    
          counts.add(new Integer(k[0]));
          counts.add(new Integer(k[1]));
    
          computed.add(counts);
        }
    
        // output
        int m = computed.size();
    
        System.out.println();
    
        for(int i=0; i<m; ++i) {
          ArrayList<Integer> aL = computed.get(i);
          System.out.println(aL.get(0));
        }
    
        System.out.println();
    
        // output
        for(int i=0; i<m; ++i) {
          ArrayList<Integer> aL = computed.get(i);
          System.out.println(aL.get(1));
        }
      }
    
    }