I's running this on visual c++ and it runs without error. However, when I tried to run this on Linux, it kept giving me this "segmentation fault" message without any output. There is no pointer and is a quite simple program so can anyone please tell me what is wrong??
here's the program!
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
int positionOfMax(const string a[], int n)
{
if (n <= 0)
return -1;
if (myStrlen(a) == 0)
return -1;
string max = "";
int i = 0;
int keep;
while (i < n)
{
if (max < a[i])
{
max = a[i];
keep = i;
}
i++;
}
return keep;
}
int rotateLeft(string a[], int n, int pos)
{
if(n <= 0 || pos > n || pos < 0)
return -1;
string save = a[pos];
int returnValue = pos;
while((n-1) > pos)
{
a[pos] = a[pos + 1];
pos ++;
}
a[pos] = save;
return returnValue;
}
int partition(string a[], int n, string separator)
{
if (n <= 0)
return -1;
int i = n;
while (i > 0)
{
int pos = positionOfMax(a, i); //find the latest one's position
rotateLeft(a, i, pos); //move the latest one to the end
i--;
pos = 0;
}
int start = 0;
while (start < n)
{
if (separator > a[start])
start++;
else
return start;
}
return n;
}
int main()
{
string folks[6] = { "obiwan", "minnie", "han", "simba", "jabba", "ariel" };
assert(partition(folks, 6, "base") == 1); //return 1 since only the first one “ariel” is less than base
cerr << "Test ok" << endl;
}