CSE 143 Spring 2001

Homework 2.5

Due: Bring written portion and printout of code to sections  
on Tuesday April 17
.


Overview

This assignment consists of several short problems involving pointers and dynamic memory management.  The last problem requires writing a short program, but most of the assignment should be done on paper.

Obviously, you can answer several of the paper question by pasting the code into your favorite compiler and watch what happens when you try to run it.  But you won't learn very much if you do that, instead of thinking the problem through for yourself.

Paper Exercises

1.Find the bug in the following code.  Identify the line number and describe the error in words.

double n1;
double *n2;
double *n3;
n2 = &n1;
n3 = &n2; 
*n3 = 10;

2.  Find the error in the following lines of code.  Identify the line number and describe the error in words.

int * p;
int * q;
p = new int;
q = &p;

3.  What is the output of the following program?  (Notice that the constructors print messages to cout.  This is not normally done in real code, but it is helpful here to see when the various constructors are executed relative to other statements in the program.)

#include <iostream>
using namespace std;

class Foo{  
public:
   Foo(); 
   Foo(int number);            
private:
   int x;
};

Foo::Foo() {
    cout << "Hello" << endl; 
}

Foo::Foo(int number) {
    x = number;  
    cout << "Number is: " << x << endl; 
}

main() {
    Foo foo(10);
    Foo foo2;
    Foo *foo3;
    cout << "Almost done..." << endl;
    Foo foo4(3);
    foo3 = new Foo;
    delete foo3;
    return 0;
}
4.  Draw a boxes 'n arrows diagram showing the contents of variables and pointers after this sequence of statements has been executed. What are the values of x, y, *x, *y, and z at the end of execution?
int *x, *y;
int z = 10;
x  = new int;
y  = x;
*x = 2*z;
x  = &z;
*x = 20;

Programming Exercise

An extremely common use for dynamic memory allocation is to create data structures for numeric problems where the size of the data is not known when the program is compiled.  For this exercise, write a small program to read the length of two vectors, allocate arrays of that size, read the data, then compute and print the vector sum and scalar dot product of the two vectors.  The vector sum and dot product should be computed by defining and calling suitable functions, described below.

Note:  This is a simple exercise involving pointers to arrays of doubles, not a full-blown vector class.  We'll eventually explore how to embed these operations in a suitable class with appropriate member functions.  For this assignment, it's perfectly okay to directly use pointers and functions with simple pointer parameters.

Data format:  The data consists of an integer n giving the length of the two vectors, followed by 2*n doubles giving the contents of the two input vectors.  It's probably easiest to just type the data directly from the keyboard.  Example:  the input

        4  3.2  12.0  -5   19.93   3.14  2.718  143  224

describes two 4-element vectors [3.2, 12.0, -5, 19.93] and [3.14, 2.718, 143, 224]

a.  Write code in function main to read the size of the vectors, allocate two arrays of that size, then read the data into those arrays.  There should be no array variables in the main program - just pointers.  (Of course, you should use subscripts normally to access array elements after the arrays have been allocated.)

b.  Write a function dotProduct whose parameters are the two arrays read in part (a) and their size (number of elements).  The function should return their dot product which, for two 4-element vectors [a1, a2, a3, a4] and  [b1, b2, b3, b4] is the single number a1*b1 + a2*b2 + a3*b3 + a4*b4.  The function should work for vectors of any length, not just length 4.

c.  Write a function whose parameters are the two arrays and their length.  This function should allocate a third array of the same size, store in it the pairwise sum of the two parameter vectors, and return a pointer to the new array containing the result.  The pairwise sum of two 4-element vectors [a1, a2, a3, a4] and  [b1, b2, b3, b4] is [a1+b1,a2+b2,a3+b3,a4+b4] (and, of course, this should work for vectors of any length).

d.Complete the main program so it reads the vectors, then prints their dot product and sum by calling the functions defined in parts (b) and (c).  The main program should print both input vectors and the function results on the console.  You should run your program using several sets of input data and print the results to turn in with the rest of this assignment.

Turnin

There will be no electronic turnin for this assignment, instead bring a printout of your code along with written solutions to the paper exercises to section on the due date.