Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Array functions examples
10-25-2009, 06:50 AM
Post: #1
Array functions examples
Code:
using System;

/* This program was coded by Jerome Scott II aka codecaine */

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declaring a array of Strings
            String[] myArray = { "king", "queen", "jack", "ace" };
            //printing out default array
            Console.WriteLine("Orginal Array");
            printArray(myArray);
            //sort and print out array
            Console.WriteLine("\nSorted Array");
            Array.Sort(myArray);
            printArray(myArray);
            //reverse and print out array
            Console.WriteLine("\nReversed Array");
            Array.Reverse(myArray);
            printArray(myArray);
            //print and shuffle array
            Console.WriteLine("\nShuffle Array");
            suffleArray(myArray);
            printArray(myArray);
            //print and search for the word king in array
            Console.WriteLine("\nSearch Array for king");
            //must sort array to do a binary search
            Array.Sort(myArray);
            //if the item is in the array the index of where it is located will be returned
            if (Array.BinarySearch(myArray, "king") >= 0)
                Console.WriteLine("king is in the array!\n");
            else
                Console.WriteLine("king was not found in array\n");
            Console.WriteLine("Press enter to continue or if you have a older keyboard return lol");
            Console.ReadLine();

        }

        //shuffle array
        public static void suffleArray(String[] myArray)
        {
            //initializing Random class
            Random rand = new Random();
            //temprary place holder to do swap
            String placeHolder = "";
            //varible to hold random number value
            int randValue = 0;
            //loop through array
            for (int x = 0; x < myArray.Length; x++)
            {
                //return random array index
                randValue = rand.Next(myArray.Length);
                //if box indexs are not the same go ahead and swap values
                if (randValue != x)
                {
                    placeHolder = myArray[x];
                    myArray[x] = myArray[randValue];
                    myArray[randValue] = placeHolder;
                }
            }

        }

        //print out array
        public static void printArray(String []myArray){
            for(int x = 0; x < myArray.Length; x++)
                Console.WriteLine(myArray[x]);
        }
    }
}


Attached File(s)
.zip  cSharp Arrays Example.zip (Size: 1.73 KB / Downloads: 5)
Visit this user's website Find all posts by this user
Quote this message in a reply
10-26-2009, 03:13 AM
Post: #2
RE: Array functions examples
I must say that C# has very good functions for everything.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
10-27-2009, 05:40 AM
Post: #3
RE: Array functions examples
Arrays sucks hard.

You should really start using generics like List<T> which are type-safe and MORE FASTER Smile
Find all posts by this user
Quote this message in a reply
10-27-2009, 05:47 AM
Post: #4
RE: Array functions examples
(10-27-2009 05:40 AM)cH40z-Lord Wrote:  Arrays sucks hard.

You should really start using generics like List<T> which are type-safe and MORE FASTER Smile

lol i remember this rant on Unk Tongue
It went on for like 4 pages didn't it but ... none the less arrays are useful

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
10-28-2009, 07:38 AM
Post: #5
RE: Array functions examples
In the last discussion I started about generics it just took one page to let them know that they should really use generics instead of arrays.

Posting several links to MSDN where MVPs suggest you to use generics is really useful Wink
Find all posts by this user
Quote this message in a reply
10-29-2009, 03:15 AM
Post: #6
RE: Array functions examples
(10-28-2009 07:38 AM)cH40z-Lord Wrote:  In the last discussion I started about generics it just took one page to let them know that they should really use generics instead of arrays.

Posting several links to MSDN where MVPs suggest you to use generics is really useful Wink

Arrays are fundamentals of most programming languages, and should be learn to get a better understanding on how generics are created.
Visit this user's website Find all posts by this user
Quote this message in a reply
12-16-2009, 09:24 AM
Post: #7
RE: Array functions examples
Imagine if all you knew where generics and you were just hired at a company, and all they use is c and pascal. Where would you be without arrays?
Visit this user's website Find all posts by this user
Quote this message in a reply
12-16-2009, 09:28 AM
Post: #8
RE: Array functions examples
Wow forgot about this thread D: Shouldn't of posted my strings and arrays example this one surpasses mine by far

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
12-16-2009, 09:45 AM
Post: #9
RE: Array functions examples
lol Well it is always nice to see didn't coding styles. We can always learn from each other.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: