Whats going on: So I've written a program that manages equivalence relations and it does not include a main. I'm supposed to test it in a separate file where I make an ER (equivalence relation), merge some things, and show the result.
What I need help with: I'm not sure how to write up the testing file. I don't think I'm doing it right because it isn't working, but I don't know whats wrong. I'm not sure if I should just be running the function or using a print statement to show what it returns?
Code I'll be displaying: I'll show equiv.h (which is code for a separate file I need to use and given to me by my professor), equiv.cpp (the code with all the functions in it that I've been working on and need to test), and the testequiv.cpp (the file I'm supposed to use to test each function) in that order.
equiv.h :
// These #ifndef and #define lines make it so that, if this file is
// read more than once by the compiler, its body is skipped on all
// but the first time it is read.
#ifndef EQUIV_H
#define EQUIV_H
// An equivalence relation is an array of integers.
// So ER abbreviates int*.
typedef int* ER;
// Public functions
ER newER (const int n);
void destroyER (ER R);
bool equivalent (ER R, const int x, const int y);
void merge (ER R, const int x, const int y);
// The following is advertised here solely for debugging. These must
// only be used for debugging.
void showER(const ER R, const int n);
int leader(ER R, const int x);
#endif
equiv.cpp :
// This program is a tool that manages an equivalence relation that can be
// changed in a specific way by the program
#include <cstdio>
using namespace std;
// newER(n) returns an equivalence relation that can handle set {1, 2, …, n}.
int* newER(int n)
{
int* R = new int[n];
for(int i = 1; i < n; i++)
{
R[i] = i;
}
return R;
}
//showER(R, n) prints the entire contents of array R (of size n) in a
//readable form for debugging.
void showER(int* R, int n)
{
for (int i = 1; i <= n; i++)
{
printf("%d",R[i]);
}
printf("\n");
}
//leader(R, x) returns the leader of x in equivalence relation R.
int leader(int* R,int x)
{
if (R[x] == 0)
{
return x;
}
else
{
return leader(R, R[x]);
}
}
//equivalent(R, x, y) returns true if x and y are currently in the same
//equivalence class in equivalence relation R.
bool equivalent(int* R,int x,int y)
{
if (leader(R, x) == leader(R, y))
{
return true;
}
else
{
return false;
}
}
//merge(R, x, y) merges the equivalence classes of x and y in R.
void merge(int* R, int x, int y)
{
if(!equivalent(R, x, y))
{
int xLeader = leader(R,x), yLeader = leader(R, y);
R[xLeader] = yLeader;
}
}
//destroyER(R) deallocates R.
int destroyER(int* R)
{
delete R;
}
int main()
{
return 0;
}
testequiv.cpp Set up to test the function newER:
#include <cstdio>
#include "equiv.h"
using namespace std;
int main()
{
newER(7);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire