CSci 4651 Problem set 4. Due Friday, Feb. 24th, at 8pm

Problem 1

Problem 5.1 p. 122.

Problem 2

Problem 5.2 p. 122-123.

Problem 3

You are given a C++ program below:
#include <iostream>  // need this header file to support the C++ I/O system
using namespace std; // telling the compiler to use namespace "std",
// where the entire C++ library is declared.

// function declarations - needed for the program to compile
int min_ii (int n, int m);
int min_pi (int *p, int * q);
int * min_pp (int *p, int * q);

int main()
{
  int n = 6, m = 5;

  // passing two integers to a function
  int min = min_ii(n , m);
  cout << min << endl;

  // passing addresses of n and m to a function
  min = min_pi(&n, &m);
  cout << min << endl;

  // passing addresses of n and m to a function
  int * min_p = min_pp(&n, &m);
  cout << *min_p << endl;

  *min_p = *min_p - 1;
  // changing m via a pointer
  cout << m << endl;

  // call to swap GOES HERE
  cout << "m = " << m << " n = " << n << endl;

  return 0;
}

// passing pointers to functions, returning a pointer
int * min_pp (int *p, int * q) {
   // fill in the code for this function

}


// passing two integers to a function, returning an int
int min_ii (int n, int m) {
   // fill in the code for this function
}

// passing pointers to functions, returning an int
int min_pi (int *p, int * q) {
   // fill in the code for this function
}

To compile a C++ program on a dungeon machine, type g++ myprogram.cpp (replace myprogram by the actual program name). To run it, type ./a.out, this will run the executable produced by the compiler. Note: as is, the program doesn't compile since the functions don't return the right type.
Question 1 The three functions whose names start with min all find the minimum of two integers. However, they differ in how they get their parameters and how they return the value: You need to fill in the code for the three functions (see the comments in the code). Notice that the lines before main are function prototypes (required in C++ for all functions called before they are declared).

Note for those familiar with C++: while there is a different syntax for passing parameters to a function by reference, please use the C style (explicit pointers), as defined in the function prototypes.

Question 2 Write a void function swap to swap the values of two integers in the program. Use this function to swap the values of m and n. Don't forget to add the function prototype in the beginning of the program.


CSci 4651 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.