Ok so I have to swap two letters in an array....specifically position i and j WITHOUT screwing up the the position of everything ELSE.
Now here's the problem I have to do it in a specific way because it's what our teacher wants:
I am using BloodShed Dev C++ for the C++ programming language.
int List::translate(int index) const
{
return index-1;
}
and finally the main program:
#include <iostream>
using namespace std;
#include "List.h"
int swap();
int showlist(int index, ListItemType& dataItem, bool& success);
int main()
{
List aList;
ListItemType dataItem;
bool success;
char var='a';
for(register int start=1; var<='z'; var++, start++)
aList.insert(start, var, success);
// BEGIN OUTPUTLIST
aList.retrieve(1, dataItem, success);
// init | end cond | steps toward end cond
for(int index =1; index <= aList.getLength(); index++)
{
aList.retrieve(index, dataItem, success);
cout<<dataItem<<endl;
}
// END OUTPUTLIST
system("pause");
return 0;
}
int swap()
{
for(register int start=1; var<='z'; var++, start++)
aList.insert(i, var, success);
}
I've got it to print the list but I can't figure out how to swap I and J and it's just bugging me.
I know it should be easy once it's in the module but it's got me stumped....any help would be appreciated.
retrieve is used to get the data out of your array, however your class has no function to simply replace a value at a specific index with another value. So, your options are either to insert and delete the value immediately after (which is kind of silly), to make swap a friend function (so you can access the array directly), make swap a member function of List, or add another function to list that would allow direct replacement.
Believe it or not the insert/delete is exactly what she wants us to do.....followed by replace.
The problem is I dont get how I would do that? insert is clear enough I know how to do that for the ENTIRE list but how do I delete the necessary characters and then replace them with each other?
you'll need to delete at an index, and then the new value at that same index. It should look something like this, but of course your teacher is going to want you to actually check if each function call was successful.
1 2 3 4 5 6 7 8 9 10
void swap(List& l, int index1, int index2) {
bool success;
ListItemType a, b;
l.retrieve(index1, a, success);
l.retrieve(index2, b, success);
l.remove(index1, success);
l.insert(index1, b, success);
l.remove(index2, success);
l.insert(index2, a, success);
}