Mengurutkan data Ascending dan descending dengan Microsoft Visual 2005

Dalam salah satu mata kuliah Struktur Data, saya menggunakan Microsoft visual Studio 2005. Saya mendapatkan tugas untuk mengurutkan data secara Ascending dan Descending. Berikut ini coding yang saya gunakan:

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

namespace Iswanti
{
    class Program
    {
        public static string[] npm = new string[4];
        public static string[] nama = new string[4];
        public static int[] ipk = new int[4];
        public static void inputData()
        {
            for (int a = 0; a <= 3; a++)
            {
                Console.WriteLine("\nMasukan Data ke-" + (a + 1) + " :");
                Console.Write("NPM : ");
                npm[a] = Console.ReadLine();

                Console.Write("Nama : ");
                nama[a] = Console.ReadLine();

                Console.Write("IPK : ");
                ipk[a] = Convert.ToInt16(Console.ReadLine());
            }
        }
        public static void tampilkanData()
        {
            Console.WriteLine("\nNPM\t\tNama\t\tNilai IPK");
            for (int i = 0; i <= 3; i++)
            {
                Console.Write("\n"+npm[i]+"\t");
                Console.Write(nama[i] + "\t\t");
                Console.Write(ipk[i] + "\n");
            }
            Console.WriteLine();
        }
        public static void urutkanDataAsc()
        {
            int ip;
            string nam, no;
            for (int y = 0; y < 3; y++)
            {
                for (int z = y + 1; z <= 3; z++)
                {
                    if (ipk[y] > ipk[z])
                    {
                        ip = ipk[y];
                        ipk[y] = ipk[z];
                        ipk[z] = ip;

                        nam = nama[y];
                        nama[y] = nama[z];
                        nama[z] = nam;

                        no = npm[y];
                        npm[y] = npm[z];
                        npm[z] = no;
                    }
                }
            }
        }
        public static void urutkanDataDesc()
        {
            int ip;
            string nam, no;
            for (int y = 0; y < 3; y++)
            {
                for (int z = y + 1; z <= 3; z++)
                {
                    if (ipk[y] < ipk[z])
                    {
                        ip = ipk[y];
                        ipk[y] = ipk[z];
                        ipk[z] = ip;

                        nam = nama[y];
                        nama[y] = nama[z];
                        nama[z] = nam;

                        no = npm[y];
                        npm[y] = npm[z];
                        npm[z] = no;
                    }
                }
            }
        }
        static void Main(string[] args)
        {           
            inputData();
            Console.WriteLine("\n======Data sebelum diurutkan======");
            tampilkanData();
Console.WriteLine("\n====Data setelah diurutkan Ascending====");
            urutkanDataAsc();
            tampilkanData();
Console.WriteLine("\n====Data setelah diurutkan Descending====");
            urutkanDataDesc();
            tampilkanData();
        }
    }
}


Setelah itu run program, dan menghasilkan tampilan seperti berikut:

 
 Semoga Bermanfaat ^_^....

0 comments:

Post a Comment

 

Menuai Sesuatu Dengan Berani Mencoba [Positif] Copyright © 2011-2012 | Powered by Blogger