开发者

Comparing array elements in C#

two arraya namely item and tra

item contains

B
C
M
G
D
E

tra array contains

BM
BDGE
MDGC
BMDG
BMDC

I need to find the count of each element in item array that is present in tra array

for e.g. B count is 4 C count is 2

for this task I make use of count() and counter variable.

here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace apriori
{
    class apriori
    {
        static void Main(string[] args)
        {
            int t, n, s, i, j, k, q;
            int counter = 0;
            int[] count = new int[6];

            Console.WriteLine("enter the number of transactions t");

            t = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("enter the number of items n");

            n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("enter minimum support s");

            s = Convert.ToInt32(Console.ReadLine());

            String[] tra = new String[t];
            String[] item = new String[n];

            Console.WriteLine("enter transactions representing one a开发者_如何学编程lphabet each item");

            for (i = 0; i < t; i++)
            {
                tra[i] = Console.ReadLine();
            }

            Console.WriteLine("list of transactions");

            foreach (String it in tra)
            {
                Console.WriteLine(it);
            }

            Console.WriteLine("enter items");

            for (j = 0; j < n; j++)
            {
                item[j] = Console.ReadLine();
            }

            Console.WriteLine("list of items");

            foreach (String ite in item)
            {
                Console.WriteLine(ite);
            }

            for (i = 0; i < n; i++)
            {
               for (j = 0; j < t; j++)
                {
                    if (item[i]==tra[j])
                    {
                      count[i]=counter++;
                    }
                } 

                counter=0;
            }


Your program won't work for n>6, as:

int[] count = new int[6];

 for (i = 0; i < n; i++)
 {
      for (j = 0; j < t; j++)
      {
           if (item[i]==tra[j])
           {
                // this index is out of bounds when i>=6
                count[i]=counter++;
           }
      } 

      counter=0;
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜