Solutions to Assignment 2


// p1
#include 
#include 
// use namespace std;

int
main(int argc, char **argv) {
    int n1, n2;
    ifstream m_in;
    ofstream m_out;

    /* Correct call or not ? */
    if (argc < 3) {
        cerr << "Call: " << argv[0] << " [input_file] [output_file]" <<
endl;
        return -1;
    }

    /* Open input file */
    m_in.open(argv[1]);
    if (!m_in) {
        cerr << "File '" << argv[1] << "' not found." << endl;
        return -2;
    }

    /* Open output file */
    m_out.open(argv[2]);
    if (!m_out) {
        cerr << "Cannot open '" << argv[2] << "' as output." << endl;
        m_in.close();
        return -2;
    }

    /* Execute computations */
    m_in >> n1 >> n2;
    while (!m_in.eof()) {
        m_out << n1 - n2 << endl;
        n1 = n2;
        m_in >> n2;
     }

    /* Close everything */
    m_in.close();
    m_out.close();

}


// p2
#include 
// use namespace std;

// Return 0 if false, 1 if true
int
isPrime(int N) {
    int i;

    if (!(N % 2)) return 0;
    for (i = 3; i < N / 2; i += 2) {
        if (!(N % i)) return 0;
    }
    return 1;
}

int
main() {
    int i, N = 10000;
    int prev = 0;

    for (i = 3; i < N; i += 2) {
        if (isPrime(i)) {
            if (prev) {
                cout << i - 2 << " and " << i << " ";
            }
            prev = 1;
        } else {
           prev = 0;
        }
    }
    cout << endl;
}


//p3
#include 
#include 
// use namespace std;

int
main(int argc, char **argv) {
    int Next;                           /* Store current number */
    int S = 0, MS = 0;              /* Store current sum and the maximal
one */
    int x = 0, y = 0, mx = 0, my = 0;   /* Interval */
    ifstream m_in;                  /* Input file class */

    /* Correct call or not ? */
    if (argc < 2) {
        cerr << "Incorrect call." << endl;
        return -1;
    }

    /* Open the input file for reading */
    m_in.open(argv[1]);
    if (!m_in) {
        cerr << "File not found." << endl;
        return -2;
    }

    /* Do computations */
    m_in >> Next;
    while (!m_in.eof()) {
       if (S + Next > 0) {
           S += Next;
           if (MS < S) {
               MS = S;
               mx = x; my = y;
           }
           y++;
       } else {
          y++; x = y;
          S = 0;
       }
       m_in >> Next;
    }

    /* Close */
    m_in.close();
    cout << "Sum is: " << MS << " and the interval: " \
                             << mx << " - " << my << endl;

}


// p5
#include 
#include 
#include 
//use namespace std;

int
main() {
    int N;
    int i = 0, pw2 = 2, step;
    int K, R;

    cout << "Enter the limit: ";
    cin >> N;

    /* Between ? */
    srand(time(NULL));
    R = rand() % (N - 1) + 1;

    /* Start finding it */
    K = N / 2;
    do {
        i++;
        pw2 *= 2;
        step = N / pw2 ? N / pw2 : 1;
        K = (R >= K) ? K + step : K - step;
        cout << K << endl;
    } while (R != K);

    cout << "Numbers are: " << K << " and " << R
                            << " and # of queries is: " << i << endl;

}