Swapping letters from an array in a very specific way

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.

So here is my header file .h

#include <iostream>
using namespace std;

const int MAX_LIST = 26;

typedef char ListItemType;


class List
{
public:
List();
bool isEmpty() const;
int getLength() const;
void insert(int index, const ListItemType& newItem, bool& success);
void remove (int index, bool& success);
void retrieve (int index, ListItemType& dataItem, bool& success) const;

private:
ListItemType items[MAX_LIST];
int size;
int translate (int index) const;
};


the implementation file
#include "list.h"

List::List() : size(0)
{
}

bool List::isEmpty() const
{
return size == 0;
}

int List::getLength() const
{
return size;
}

void List::insert(int index, const ListItemType& newItem, bool& success)
{
success=(index>=1)&&(index<=size+1)&&(size<MAX_LIST);
if (success)
{
for(int pos=size; pos>=index; --pos)
items[translate(pos+1)]=items[translate(pos)];
items[translate(index)]=newItem;
++size;
}
}

void List::remove(int index, bool& success)
{
success=(index>=1)&&(index<=size);
if(success)
{
for(int fromPosition = index +1; fromPosition <= size; ++fromPosition)
items[translate(fromPosition-1)]=items[translate(fromPosition)];
--size;
}
}

void List::retrieve(int index, ListItemType& dataItem,
bool& success) const
{
success= (index >= 1) && (index <= size);
if (success)
dataItem= items[translate(index)];
}

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.
swapping works like this:
1
2
3
4
5
swap(char& a, char& b) {
    char temp = a;
    a = b;
    b = temp;
}

or with pointers
1
2
3
4
5
swap(char *a, char *b) {
    char temp = *a;
    *a = *b;
    *b = temp;
}
Hmm how do I pull I and J out of the array when they are only assigned numbers at the moment?

I've never done a program quite like this one so it's got me stumped as to how to pull the information and switch it and then re-print the list.
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.
Last edited on
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);
}
Last edited on
Topic archived. No new replies allowed.