I'm writing a simple electrical circuit simulator and came across some logic I just can't get done with.
Problem's the following: I've got a couple of arrays of the same size and type that are defined by user input, both their values and sizes, and I'd like to bind their values to each other, like...
Did you write your own sorting method? If so, just make sure the Swap method swaps both arrays:
1 2 3 4 5 6 7
void Swap(array& a, array& b, constint i, constint j) {
int atemp = a[i], btemp = b[i];
a[i] = a[j];
b[i] = b[j];
a[j] = atemp;
b[j] = btemp;
}
If you're using a built-in sort (or some other blackbox sorting code), it might be easier to actually link the items by wrapping them in a struct, and then sorting an array of objects:
1 2 3 4 5 6 7 8 9
struct myVals {
val1; // Holds "array1" value
val2; // Holds "array2" value
booloperator<(const myVals& rhs) { return val1 < myVals.val1; }
}
myVals values[4];
// initialization code goes here
std::sort(values, values+4); // Default sort uses i < j predicate