How to link 2 arrays

This is a discussion on How to link 2 arrays within the C++ Programming forums, part of the General Programming Boards category; I am faced with writing a C++ program that needs to prompt for an employee number and find the employee ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    1

    How to link 2 arrays

    I am faced with writing a C++ program that needs to prompt for an employee number and find the employee in the employee number arrary using a search. (I have this section completed.). However, once I find the employee number, I need to use the associated record location and perform a random access read of the employee file. I do not know how to associate the elements in the record location to the employee array. Perhaps, I need to create a function? Please help

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    If possible, the best (easiest) solution would be to keep the employee data corresponding to a given number at the same index in its own array as the number is in the employee number array. That way, if you know one index, then you automatically know the number.

    The other solution that comes to mind is a keyed hash table, but that seems like it might be a bit overkill for this problem.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    jayexpect
    Guest
    Zach,
    I should have mentioned that the project requires me to sort the employee number array, as well as the record location (index).

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    in your array store pairs of emloyee id's and a pointer to the record info... like this.

    Code:
    struct Pair {
      int           id;
      Record*  info;
    };
    
    // then make a Record class to store the info with any needed functions
    
    class Record {
      char*     name;
      Date      bday;
      float      wage;
      etc...
    public:
      float calcPay(int numHours);
      etc...
    };

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Perspective's method is probably the best way to go.

    Another (less elegant, and less efficient) way, would be, when sorting to id array, to always swap corresponding elements in the record array.

    Just out of curiosity, do you have to be able to search the records as well?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    jayexpect
    Guest
    Zach,

    Below is the code that I have functioning so far. Could you take a look at it and let me know where I need to go from there?

    Additionally, I have another program which I did not include which simply writes the data to an employee file.

    Thanks


    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    //This program creates an index to the employee file, sort the index
    // and use a binary search to access the data.

    int bubbleSort(int[], int); //function declaration prototype
    int binarySearch(int [], int, int);//function declaration prototype
    int selectionSort(int [], int[], int);//function declaration prototype

    struct employee
    {
    int number;
    char name;
    int age;
    float salary;
    };
    void selsort(employee [], int); // 1st parameter is an array of records

    int main()
    {

    const int MAXNAME = 30; // maximum no. of characters in a name
    const int ARRAYSIZE = 5; // maximum no. of records in the array

    const int NUMEL = 10; //maximum number of records in the array
    int i,moves,location;;
    int EmpRec[NUMEL];
    int EmpNum[NUMEL];
    long offset, last;

    fstream outFile("c:\\employee.txt", ios::in | ios:ut); //open the file

    employee emp;

    for (i = 0; i < 10; i++)
    {
    cout << endl << "Press " << i + 1 << " to enter employee #"<< i + 1<< "->";
    cin >> EmpRec[i];
    cout << endl << "Enter employee number " << i + 1 << " employee number ";
    cin >> emp.number;
    EmpNum[i] = emp.number;
    cout << endl << "Enter employee number " << i + 1 << " name ";
    cin >> emp.name;
    cout << endl << "Enter employee number " << i + 1 << " age ";
    cin >> emp.age;
    cout << endl << "Enter employee number " << i + 1 << " salary ";
    cin >> emp.salary;
    }


    moves = bubbleSort(EmpNum,NUMEL); //use the bubbleSort function

    cout << endl << emp.number << " " << emp.name << " " << emp.age << " "<< emp.salary;
    cout << "\nThe sorted list, in ascending order, is:\n";

    for (i = 0; i < NUMEL; ++i)
    cout << " " << EmpNum[i];

    cout << endl << moves << " were made to sort this list\n";
    int item;

    cout << "\nEnter the item you are searching for: ";
    cin >> item;
    location = binarySearch(EmpNum, NUMEL, item);

    if (location > -1)
    cout << "The item was found at index location "
    << location << endl;

    else
    cout << "The item was not found in the array\n";

    int search;
    char c;

    cout << "Enter record number (1-10) to be found->";
    cin >> search;


    outFile.seekg((search-1) * sizeof(emp));
    outFile.read((char *) &emp, sizeof(emp));

    cout << endl << emp.number << " " << emp.name
    << " " << emp.age << " " << emp.salary << endl;

    outFile.close();

    return 0;
    }

    int bubbleSort(int num[], int numel)
    { //start of function body
    int i, j, temp, moves = 0; //variable declaration

    for ( i = 0; i < (numel - 1); i++)
    {
    for(j = 1; j < numel; j++)
    {
    if (num[j] < num[j-1])
    {
    temp = num[j];
    num[j] = num[j-1];
    num[j-1] = temp;
    moves++;
    }
    }
    } //end of function body
    return moves;
    }

    // this function returns the location of key in the list
    // a -1 is returned if the value is not found
    int binarySearch(int list[], int size, int key)
    {
    int left, right, midpt;

    left = 0;
    right = size - 1;

    while (left <= right)
    {
    midpt = (int) ((left + right) / 2);
    if (key == list[midpt])
    {
    return midpt;
    }
    else if (key > list[midpt])
    left = midpt + 1;
    else
    right = midpt - 1;
    }

    return -1;
    }

  7. #7
    jayexpect
    Guest
    Zach,
    The exact instructions for the program is
    1) Read in the records and store the employee# in 1 arrary and the record number (offset) in another array.
    2)Sort the employee number array, making sure to move (i.e., exchange) the elements in the record location as well.
    3) Prompt for an employee # and find the employee in the employee number array using a search. Once the employee # is found, use the ASSOCIATED record location and perform a random access read of the employee file. Display the employee info. (number, name, age, salary)

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Okay. Basically, what it is asking you to do is to create a third array which stores indices (so when you read the data in, Array[0] = 0, Array[4] = 4, etc...) Then, each time you swap an element in your id array, you swap the corresponding element in the index array. That way, the index array maps id indices into record indices.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    jayexpect
    Guest
    Zach,

    I want to thank you for your superior experience and expertise. The method you gave me worked like a charm. You are outstanding!!

    May God continue to richly bless you and your family.

    Peace

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. link list error
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 04-13-2006, 07:35 PM
  3. Data structure implementation
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-31-2003, 07:44 AM
  4. Undefined Structure in Link List
    By _Cl0wn_ in forum C Programming
    Replies: 1
    Last Post: 03-22-2003, 04:57 PM
  5. OpenGL drawing w/ Arrays or not?
    By tegwin in forum Game Programming
    Replies: 2
    Last Post: 01-14-2003, 02:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21