Like the title says, I'm wondering why my program is giving me a segmentation fault when I try to run it. For example, in the testequiv.cpp file I try to call the merge function and it triggers a segmentation fault. This also applies to the leader and equivalence function as well. I would greatly appreciate any feedback!
usingnamespace std;
#include "equiv.h"
#include <cstdio>
#include <iostream>
// newER(n) returns an array of n+1 integers.
ER newER (int n)
{
ER boss = newint[n+1];
int i = 1;
while (i <= n)
{
boss[i] = i;
i++;
}
return boss;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// leader(boss, x) returns the leader of x in equivalence relation boss.
int leader(ER boss, int x)
{
if (boss[x] == 0)
{
return x;
}
else
{
return leader(boss, boss[x]);
}
}
Anyways here is the output I got, the program freezes but no segmentation fault :
--- Current ER ---
At index 1 the array value is 1
At index 2 the array value is 2
At index 3 the array value is 3
At index 4 the array value is 4
At index 5 the array value is 5
At index 6 the array value is 6
The debugger identifes this as problematic: return leader(boss, boss[x]); // finds the boss of boss[x] so on forth.
The array has no element == 0, so the recursion never ends
We're creating a module that will later be used in the next assignment. The reason for this is so that we can check if something is equivalent or maybe even if we need to debug an array. On codeblocks (which is what I normally use) it crashes, but on Linux (what we must code in and then turn it in via submission command to the terminal) it gives a segmentation fault.
So we're told to define the function newER to return an array of n+1 integers.
My Professor told us to use that exact function, but since it causes infinite recursion, is there a case that I can use in a scenario that there is NO value of 0's within the array itself? Or is that causing the recursive case to be redundant?