Overclock.net banner
1 - 4 of 4 Posts

·
Registered
Joined
·
1,655 Posts
Discussion Starter · #1 ·
Okay, in this the instructor gave us some base code and had us to do a few things. One of the things is "Add method Swap(int id1, int id2) to swap the location of id1 and id2's record in the list". I am having a bit of trouble figuring this portion out...

Here is my function code so far...

Code:

Code:
//Implementation of ArrayListRecord Class

#include <iostream>
#include "ArrayListRecord.h"
using namespace std;

ArrayListRecord::ArrayListRecord()
{
        length = 0;
        currentPos = -1;
}

void ArrayListRecord::MakeEmpty()
{
        length = 0;
        currentPos = -1;
}

bool ArrayListRecord::IsFull()
{
        if(length == MAX_ITEMS) return 1;
        else return 0;
}

int ArrayListRecord::LengthIs()
{
        return length;
}

const StudentRecord* ArrayListRecord::RetrieveItem(int item)
{
        int location = 0;
        while(location < length)
        {
                if(item == list[location].id) return &list[location];
                else location++;
        }
        return 0;
}

bool ArrayListRecord::InsertItem(int item, char* fName, char* lName, double grade, char*phonenum)
{
        if(IsFull()) return 0;
        list[length].id = item;
        list[length].firstName = fName;
        list[length].lastName = lName;
        list[length].gpa = grade;
        list[length].phone = phonenum;
        length++;
        return 1;
}

bool ArrayListRecord::ModifyItem(int item, char* fName, char* lName, double grade, char*phonenum)
{
        list[length].id = item;
        list[length].firstName = fName;
        list[length].lastName = lName;
        list[length].gpa = grade;
        list[length].phone = phonenum;
        return 1;
;}

void ArrayListRecord::Swapitem(int id1, int id2)
{
        int temp = list[id1].id;
         list[id1].id = list[id2].id;
         list[id2].id = temp;

;}

void ArrayListRecord::DeleteItem(int item)
{
        int location = 0;
        while(location<length)
        {
                if(item == list[location].id)
                {
                        list[location] = list[length-1];
                        length--;
                        return;
                }
                location++;
        }
}

void ArrayListRecord::ResetList()
{
        currentPos = -1;
}

bool ArrayListRecord::IsLastItem()
{
        return (currentPos==length-1);
}

const StudentRecord* ArrayListRecord::GetNextItem()
{
        if(IsLastItem()) return 0;
        currentPos++;
        return &list[currentPos];
}

void ArrayListRecord::PrintAll()
{
        for(int i=0;i<length;i++) cout<<list[i].id<<"  "<<list[i].firstName<<"   "<<list[i].lastName<<"  "<<list[i].gpa<<"  "<<list[i].phone<<endl;
}[/code]

And my header file..

Code:

Code:
//Specification for ArrayListRecord class

#ifndef ARRAYLISTRECORD_H
#define ARRAYLISTRECORD_H
#define MAX_ITEMS 100
#include <string>
using namespace std;

struct StudentRecord
{
        int id;
        string lastName;
        string firstName;
        double gpa;
        string phone;
};

class ArrayListRecord
{
protected:
        StudentRecord list[MAX_ITEMS];
        int length;
        int currentPos;
public:
        ArrayListRecord();
        void MakeEmpty();
        bool IsFull();
        int LengthIs();
        const StudentRecord* RetrieveItem(int);
        bool InsertItem(int,char*,char*, double, char*);
        bool ModifyItem(int,char*,char*, double, char*);
        void DeleteItem(int);
        void Swapitem(int id1, int id2);
        void ResetList();
        bool IsLastItem();
        const StudentRecord* GetNextItem();
        void PrintAll();
};

#endif
And my main program... (mainly for testing the functions)

Code:

Code:
#include "ArrayListRecord.h"
#include <iostream>
using namespace std;

int main()
{
        ArrayListRecord myList;

        myList.InsertItem(111,"Smith","John", 3.87, "555-967-5309");
        myList.InsertItem(222,"Lie","Qian", 2.45, "555-555-5555");
        myList.InsertItem(333,"Mit","Romney", 3.45, "555-245-8956");
        myList.InsertItem(444,"Barak","Obama", 3.97, "555-644-9873");
        myList.InsertItem(555,"Michael","Jordan", 4.00, "555-234-1231");
        myList.InsertItem(666,"Jane","White", 2.34, "555-595-6300");
        myList.InsertItem(777,"Lee","Johnson", 3.44, "555-333-5757");

        myList.PrintAll();
        myList.Swapitem(222, 555);
        myList.DeleteItem(444);
        cout<<"========================"<<endl;
        myList.PrintAll();
}
Any help would be appreciated. I kinda think my idea for swapping is... wrong.
 

·
Premium Member
Joined
·
1,563 Posts
Since you're only asking for help with the Swap() function, it would be a good idea to remove the extra bits of code, so that it's easier to read.
redface.gif


Now, I haven't used much of C++, but I'll try to help you to the best of my knowledge.

The ArrayListRecord class stores each item or record in the form of the struct "StudentRecord" that you have defined.
To swap two items at two locations, you will first need to store one of the items in a temporary variable that is of the type, "StudentRecord". You cannot store a StudentRecord in a temporary variable of int type, obviously.

1. You will retrieve the item in the position, id1, and store it in a temp StudentRecord.
2. Then, you can delete that item using the DeleteItem() function. This will reduce the length of the array, I think.
3. Then, retrieve a copy of the item at id2 and insert it into the position id1. This will increase the length of the array back to its original size.
4. Then, delete the item at id2.
5. Insert the copy of the StudentRecord at id1 originally, at the position id2.

Done. This can probably be done in a better, faster way but this is what I came up with right now.
redface.gif
I think what you're doing is just switching the ids of the two items without actually swapping their positions.

If I have understood this incorrectly, please do tell me.
 

·
Premium Member
Joined
·
10,774 Posts
Quote:
Originally Posted by {Unregistered} View Post

Since you're only asking for help with the Swap() function, it would be a good idea to remove the extra bits of code, so that it's easier to read.
redface.gif

Now, I haven't used much of C++, but I'll try to help you to the best of my knowledge.
The ArrayListRecord class stores each item or record in the form of the struct "StudentRecord" that you have defined.
To swap two items at two locations, you will first need to store one of the items in a temporary variable that is of the type, "StudentRecord". You cannot store a StudentRecord in a temporary variable of int type, obviously.
1. You will retrieve the item in the position, id1, and store it in a temp StudentRecord.
2. Then, you can delete that item using the DeleteItem() function. This will reduce the length of the array, I think.
3. Then, retrieve a copy of the item at id2 and insert it into the position id1. This will increase the length of the array back to its original size.
4. Then, delete the item at id2.
5. Insert the copy of the StudentRecord at id1 originally, at the position id2.
Done. This can probably be done in a better, faster way but this is what I came up with right now.
redface.gif
I think what you're doing is just switching the ids of the two items without actually swapping their positions.
If I have understood this incorrectly, please do tell me.
He should be able to directly copy values instead without any deletions/array size changes
smile.gif
Although I haven't had my caffeine, so I read it with half-closed eyes LOL.

Code:

Code:
const int ArrayListRecord::GetItemIndex(int itemId)
{
        int i;
        for ( i = 0; i < length; ++i )
        {
                if ( (&list[i]) != NULL && list[i].id == itemId )
                {
                        return i;
                }
        }

        return -1;
}

void ArrayListRecord::Swapitem(int id1, int id2)
{
        int firstIndex = GetItemIndex(id1);
        int secondIndex = GetItemIndex(id2);

        if ( firstIndex != -1 && secondIndex != -1 )
        {
                StudentRecord swappy = list[secondIndex];
                list[secondIndex] = list[firstIndex];
                list[firstIndex] = swappy;
        }
}
Although it is not terribly efficient for larger structures.

Or, seeing as it is C++ we can take advantage of std::swap

Code:

Code:
void ArrayListRecord::Swapitem(int id1, int id2)
{
        int firstIndex = GetItemIndex(id1);
        int secondIndex = GetItemIndex(id2);

        if ( firstIndex != -1 && secondIndex != -1 )
        {
                std::swap(list[firstIndex], list[secondIndex]);
        }
}
 
1 - 4 of 4 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top