Appendix II: Utilities

As is well known, the most challenging and often time consuming part of developing a program is testing and debugging. In this appendix are some simple functions that can provide powerful peeks inside of a running program.

utilities.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Utility functions header file
//
// David John, August 2013
// (c) Wake Forest University, 2013

#ifndef UTILITIES_H
#define UTILITIES_H

#include <iostream>
#include <string>

using namespace std;

int compare_ints(const void *a, const void *b);
string issorted(int xxx[], int xxxStart, int xxxLength);
string issorted(int xxx[], int xxxLength);
void dumpArray(int myid, string arrayName, int array[], int start, int length);
void dumpArray(int myid, string arrayName, int array[], int length);

#endif

utilities.cc

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Utility functions
// David John, August 2013
// (c) Wake Forest University, 2013

#include "utilities.h"


// compare_ints() used by cstdlib::qsort() to compare two
// integers
int compare_ints(const void *a, const void *b)
{
	int myint1 = *reinterpret_cast<const int *>(a);
	int myint2 = *reinterpret_cast<const int *>(b);
	if (myint1<myint2) return -1;
	if (myint1>myint2) return 1;
	return 0;
}

// Utility function to help verify that list is sorted
string issorted(int xxx[], int xxxStart, int xxxLength)
{
	for(int i=xxxStart; i<xxxStart+xxxLength-1;i++)
	{
		if (xxx[i]>xxx[i+1])
			return "is not";
	}

	return "is";
}

string issorted(int xxx[], int xxxLength)
{
	return issorted(xxx,0,xxxLength);
}

// ----------------

// Utility to show array values
void dumpArray(int myid, string arrayName, int array[], int start, int length)
{
	for(int i=start;i<start+length;i++)
	{
		cout << myid << ": " << arrayName << "[" << i << "] = " << array[i] << endl;
	}
	return;
}

void dumpArray(int myid, string arrayName, int array[], int length)
{ 
	dumpArray(myid, arrayName, array, 0, length);
	return;
}
 

Questions

  1. Why might it be a good investment of time to develop simple routines like these?

Table Of Contents

Previous topic

Appendix I: Multimerge

Next topic

Appendix III: Hmmmmmmmmm

This Page