Nov 22, 2009

Parallel Array Matching

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Parallel Array Matching

xboxrulz
Hey guys,

Recently I had my test, but this question completely stumped me. I was wondering how you guys would approach this question:

So basically I get two parallel arrays as such:

userid_t[] 1001, 1002, 1003, 1001, 1001, 1002, 1004, 1005
itemcode_t[] A , A , B , B , C , A , B , F

So basically, I have to go through the parallel arrays and see the frequency of them having two distinct users selecting the item code.

So in that question, the array:

itemcodes[] A, B, C, F <--- this part I got, just sort them and then drop in the ones that aren't duplicated.
frequency[] 2, 3, 1, 1 <--- this is the part that I'm having trouble with.


Many thanks,
xboxrulz

Comment/Reply (w/o sign-up)

turbopowerdmaxsteel
How about frequency being an array of List of integers? That way you can add the userids to it while checking that the same userid has not been added. Finally, just return the count of the list, that is your frequency.

Comment/Reply (w/o sign-up)

docduke
My first question is: What tools can you use? My choice is Python.
As an example, I took your items and made them into a list of characters:
>>> x = ['A' , 'A' , 'B' , 'B' , 'C' , 'A' , 'B' , 'F']
then I got rid of duplicates by converting the list to a set and back to a list:
>>> y = list(set(x))
Printing the result gives
>>> y
['A', 'C', 'B', 'F']
You can then tell how many duplicates there were by counting and subtraction:
>>> print len(x) - len(y)
3
>>>
Extending this example, you can build lists for each item and the corresponding userids, and remove duplicates for each item.

Hope this helps! rolleyes.gif


Comment/Reply (w/o sign-up)

xboxrulz
I can't use any tools =(. It must be in pure C. As for turbomaxsteel's post ... I'm a bit lost at what you mean...

Thanks,
xboxrulz

Comment/Reply (w/o sign-up)

turbopowerdmaxsteel
Here's how I would do it in C#. It'll be a bit difficult to do it in C, especially since you don't have classes.

CODE
using System;
using System.Collections.Generic;

namespace Maxotek
{
    static class Program
    {
        static void Main()
        {
            int[] userid_t = new int[] { 1001, 1002, 1003, 1001, 1001, 1002, 1004, 1005 };
            char[] itemcode_t = new char[] { 'A', 'A', 'B', 'B', 'C', 'A', 'B', 'F' };

            // Remove Duplicate Itemcodes
            List<char> itemcodes = new List<char>();
            for (int i = 0; i < itemcode_t.Length; i++)
                if (!itemcodes.Contains(itemcode_t[i]))
                    itemcodes.Add(itemcode_t[i]);

            // Sort Itemcodes
            itemcodes.Sort();

            List<int>[] frequency = new List<int>[itemcodes.Count];
            for (int i = 0; i < userid_t.Length; i++)
            {
                int j = 0;
                for (; j < itemcodes.Count; j++)
                {
                    if (itemcodes[j] == itemcode_t[i])
                        break;
                }

                if (frequency[j] == null)
                {
                    frequency[j] = new List<int>();
                    frequency[j].Add(userid_t[i]);
                }
                else if (!frequency[j].Contains(userid_t[i]))
                    frequency[j].Add(userid_t[i]);
            }

            // Display the result
            for (int i = 0; i < itemcodes.Count; i++)
            {
                Console.WriteLine(itemcodes[i] + "-" + frequency[i].Count);
            }

            Console.Read();
        }
    }
}


With List<int>[] frequency = new List<int>[itemcodes.Count];, I have declared an array of list of integers. It stores the userids that selected the itemcode at the corresponding index in the sorted itemcodes list. Before adding the userid to the list, we check if it has been already added and skip the step if it is. At the end we can obtain the distinct users by retrieving the count property of the each of the lists.

 

 

 


Comment/Reply (w/o sign-up)

xboxrulz
It looks so much easier in C#, and probably similarly easy on Java (my preferred language)... I'll see if I can translate that back to C since that's what I'm learning this semester. I'm actually hating C >.>".

I'm learning C++ next semester so hopefully, it's gonna make it easier for someone who just loves programming with Java.

xboxrulz

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)


See Also,

*SIMILAR VIDEOS*
Searching Video's for Parallel, Array, Matching
advertisement



Parallel Array Matching

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com