Artha

Cek Ke-EXIST-an Index array Yang Ditukar

Posted on: March 31, 2011

code program sebagai berikut :



using System;
using System.Text;
using System.Threading;

namespace split
{
    public class split
    {

        //fungsi untuk menukar index array
        public string swap(string arr)
        {
            string hasil;
            int posisikoma = arr.IndexOf(",");
            string a = arr.Substring(0, posisikoma);
            string b = arr.Substring(posisikoma + 1);
            string x = b;
            string y = a;
            hasil = x + "," + y;

            return hasil;
        }

        //pengecekan nilai array tertentu apakah ketika di tukar index nya sudah pernah ada

        public bool IsExist(string str, string[] arrstr, int index)
        {
            split s = new split();
            bool x = false;
            for (int i = 0; i < index; i++)
            {
                if (str == s.swap(arrstr[i]))
                {
                    x = true;
                    break;
                }
            }

            return x;
        }

        //merubah index array menjadi string
        public string CreateHistory(int i, int j)
        {
            return i + "," + j;
        }

        public static void Main(string[] args)
        {

            int[,] arrval = new int[6, 6];
            string[] history = new string[36];
            bool cek;

            int k = 0;
            split s = new split();

            //pengisian nilai array dengan nilai 4
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    arrval[i, j] = 4;//r.Next(0,10);
                }
            }

            //mengoperasikan array (bila index array di swap sudah exist maka tidak dikalikan 100 , apabila belum dikalikan 100)

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 6; j++)
                {

                    history[k] = s.CreateHistory(i, j);
                    cek = s.IsExist(history[k], history, k);
                    if (cek == true)
                    {
                        Console.Write(arrval[i, j] + "\t");
                    }
                    else
                    {
                        Console.Write((arrval[i, j] + 100) + "\t");
                    }
                    k++;
                }
                Console.WriteLine();
            }
            Console.WriteLine();
            Console.ReadLine();

        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: