Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Good evening, Before I get into my question, I\'ll post a link to the assignment

ID: 3795805 • Letter: G

Question

Good evening,

Before I get into my question, I'll post a link to the assignment for background information:

http://www.cs.ecu.edu/~karl/2530/spr17/Assn/Assn3/assn3.html

At this point and time, attempting to go for the extra credit would be pushing my luck. I'll submit what I've done so far in a link, as well as, in this message:

https://code.sololearn.com/ch18psVNKEy8/#cpp (contains the function definitions)

- Link code-

// CSCI 2530
// Assignment: 3
// Author:
// File: equiv.cpp
// Tab stops: 4

//
//

#include "equiv.h"
#include <iostream>
#include <cstdio>
using namespace std;

/*
// int* newER(int n)
// Returns an array of n+1 integers.
*/
int* newER(int n)
{
int *R = new int[n + 1];
  
for(int index = 1; index <= n; index++)
{
R[index] = 0;
}
return R;
}

/*
// int leader(int* R, int 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])
}
}

/*
// bool equivalent(int* R, int x, int 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(int* R, int x) == leader(int* R, int y)
{
return true;
}

}

/*
// void merge(int* R, int x, int y)
// Merges the equivalence classes of x and y in R.
*/
void merge(int* R, int x, int y)
{
if(equivalent(int* R, int x, int y))
{
return;
}
else
{
  
  
  
}
}

/*
// void destroyR(int* R)
// Deallocates R.
*/
void destroyER(int* R)
{
delete []R;
}

/*
// void showER(int* R, int n)
// Prints the entire contents of array R.
*/
void showER(int* R, int n)
{
for (int index = 1; index <= n; index++)
{
cout << index << " " << R[index] << endl;
}
}

/////////Practice////////////////////////

{
  
if(equivalent(int* R, int x, int y))
{
return;
}

bool sx = false;
bool sy = false;

if(R[n-1] > 0)
{
for(int index = 0; index < arraySize; index++)
{
if(x == R[index])
{
sx = true;
}   
  
if(y == R[index])
{
sy = true;
}
  
if(sx && sy)
{
break;
}
}
}
else
{
int temp = R[n-1];
  
R[n-1] = 0;
  
R = new int[temp + 1];
}

if(!sx && !sy)
{
  
if(x < y)
{
e[arraySize] = x;
e[arraySize+1] = y;
}
else
{
e[arraySize] = y;
e[arraySize+1] = x;
}
  
sx = sy = true;
arraySize += 2;
}
  
if(!sx)
e[arraySize++] = x;
if(!sy)
e[arraySize++] = y;
}

////////Practice///////////////////////


bool testOf_x = false, testOf_y = false;
  
for(int index = 0; index < R[n-1]; index++)
{
if(R[index] == x)
{
testOf_x = true;
}
  
else if(R[index] == y)
{
testOf_y = true;
}
else if(testOf_x && testOf_y)
{
return true;
}
}

-End-

https://code.sololearn.com/ch18psVNKEy8/#cpp (contains the function prototypes)

-Code-

// CSCI 2530
// Assignment: 3
// Author: ***
// File: equiv.h
// Tab stops: ***

// 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 (int n);
void destroyER (ER e);
bool equivalent (ER e, int x, int y);
void merge (ER e, int x, int y);

// The following is advertised here solely for debugging. These must
// only be used for debugging.

void showER(ER e, int n);
int leader(ER e, int x);

#endif

-End-

How does this code look to you so far? I'm having an issue with the equivalence function, as well as, the merge function. Not sure how to go about ensuring they are doing the required job perfectly. The professor stated that each function should be fairly simple with just a few lines of code, but having a hard time sticking to what he desires. Any help would be much appreciated.

v/r

Explanation / Answer

The code looks good except for these observations -

When you are defining the functions, you are supposed to use the datatype in the function arguements, when you are calling you should not.

Change the below code at line #54: if (leader(int* R, int x) == leader(int* R, int y)

as if (leader(R, x) == leader(R, y)

and also at line #67 - you have if(equivalent(int* R, int x, int y))

change this to if(equivalent(R, x, y)).

--------------------------------

bool equivalent(int* R, int x, int y)
{
if (leader(int* R, int x) == leader(int* R, int y)
{
return true;
}
return false

}

-------------------------------------------------------

void merge(int* R, int x, int y)
{
if(equivalent(R, x, y))
{
return;
}
else
{
// to change the leader of x.
// have not tested it, but it should work.
// please check.
// find the leader of x.
int xLeader = leader(R,x);
  
// once you get the leader, replace the same with y.
R[x] = y
}
}

-----------------------------------------

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote