Three Homework Problems

March 22, 2016

I’m back home after visiting my daughter in Houston. The beginning-programming forums that I track, and the email that I receive (I can’t imagine what goes through the head of someone who sends me an email and tells me I must do their programming homework for them) suggests that we are in the part of the semester where beginning programmers have to write their first program. So, today we have three programs of the type that beginners need to write for their homework:

  1. Write a program that computes the sum of the integers in an array.
  2. Write a program that reverses the elements of an array.
  3. Write a program that sorts an array of integers, using insertion sort.

Your task is to assist our readers in completing their homework by writing the three programs described above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2

2 Responses to “Three Homework Problems”

  1. Paul said

    In C.

    int sum(int *arr, int n) {
        int i, thesum = 0;
        for (i=0; i< n; i++)
            thesum += arr[i];
        return thesum;
    }
    
    static void swap(int *a, int *b) {
      int tmp;
      if (a != b) {
          tmp = *a;
          *a = *b;
          *b = tmp;
      }
    }
    
    void rev(int *arr, int n) {
        int *a, *b;
        a = arr;
        b = arr + n - 1;
        while (a < b)
            swap(a++, b--);
    }
    
    void isort(int *arr, int n) {
        int i, *a, *b;
        for (i=0; i < n - 1; i++) {
            a = arr + i;
            b = a + 1;
            while (a >= arr && *b < *a)
                swap(a--, b--);
        }
    }
    
    
  2. matthew said

    We can represent an array as a function from integers to values, together with a size, eg. in Javascript { f: f, length: n } can represent an array [f[0],f[1],..,f[n-1]]. We can then define the required operations as:

    function fromarray(a) {
        return {
            f: function(i) { return a[i] },
            length: a.length
        }
    }
    
    function toarray(s) {
        var a = [];
        for (var i = 0; i < s.length; i++) {
            a.push(s.f(i));
        }
        return a;
    }
    
    function sum(s) {
        function aux(i,j) {
            if (i+1 == j) return s.f(i)
            var k = i + Math.floor((j-i)/2);
            return aux(i,k) + aux(k,j);
        }
        return aux(0,s.length);
    }
        
    function reverse(s) {
        return {
            f: function(i) { return s.f(s.length-i-1); },
            length: s.length
        }
    }
    
    function insert(s,n) {
        var a = s.f(n);
        return {
            f: function(i) {
                if (i < n) {
                    return s.f(i);
                } else if (i > n && s.f(i) >= a) {
                    return s.f(i);
                } else if (i == s.length-1 || s.f(i+1) >= a) {
                    return a;
                } else {
                    return s.f(i+1);
                }
            },
            length: s.length
        }
    }
    
    function sort(s,n) {
        if (n == undefined) n = s.length;
        if (n == 0) return s;
        return sort(insert(s,n-1),n-1);
    }
    
    var a = fromarray([3,10,2,5,6,8,9,4,1,7]);
    console.log(toarray(a))
    console.log(sum(a))
    console.log(toarray(reverse(a)))
    console.log(toarray(sort(a)))
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: