Monday, October 13, 2014

Arrays (Evening)

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

namespace ArrayExamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //an array is a variable can store more than one value
            //this declares a string array that has 4 elements
            //the square brackets [] are the mark of an array
            //rather than just a string variable
            //arrays must always be made new before they
            //can be used
            string[] members =new string[4];
            members[0] = "Rebecca";//arrays values are indexed
            members[1] = "George";//indexes begin at 0
            members[2] = "Karen";
            members[3] = "Joe";

            //you can access the array's members
            //by their index
            Console.WriteLine("the Third member is {0}", members[2]);

            string[] colors;
            Console.WriteLine("How many colors do you want to enter");
            int number = int.Parse(Console.ReadLine());

            //the length of an array can be a variable
            //but the variable must have a value
            //before you declare the array
            colors = new string[number];
            for (int i = 0; i < colors.Length; i++ )
            {
                //prompt the user for the values
                //to store in the array
                //the i is the for loop counter
                //it can substitute as a variable
                //for the array index
                Console.WriteLine("enter Color");
                colors[i] = Console.ReadLine();
            }

            Console.WriteLine("*******************");
            //loop through and write out the array values
            for (int i = 0; i < colors.Length; i++)
            {
                Console.WriteLine(colors[i]);
            }

            //another way to declare an array
            //the literal values are placed betwee
            //curley braces {}
            //you don't have to give the lenth
            //it can figure it out
            string[] dogs = new string[] 
            { "spaniel", "pug", "golden retreiver", "huskey", "Yorkie" };

            //can still access the values by index value
            Console.WriteLine("Choose a number 1 to 5");
            int dog = int.Parse(Console.ReadLine());

            //minus one to adjust for the 0 index
            Console.WriteLine("Your dog is {0}", dogs[dog-1]);

                Console.ReadKey();
        }
    }
}

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

namespace NumberArrays
{
    class Program
    {
        static void Main(string[] args)
        {
            //declare and initialize variables
            int ones = 0, twos = 0, threes = 0, fours = 0;
            //create a random object
            Random rand = new Random();
            //create a new integer array with 50 elements
            int[] numberArray = new int[50];

            // populate array by looping through and 
            //assigning a random number to each index
            for(int i=0;i<numberArray.Length;i++)
            {
                numberArray[i] = rand.Next(1, 5);
            }

            //loop through the array and count
            //how many times each number occurs
            foreach (int i in numberArray)
            {
                if (i == 1) { ones++; }
                else if (i == 2) { twos++; }
                else if (i == 3) { threes++; }
                else { fours++; }
        
            }

            //create an asteric graph
            //for each number
            Console.Write("\nOnes:\t");
            for (int i = 0; i < ones; i++)
            {
                Console.Write("*");
            }

            Console.Write("\nTwos:\t");
            for (int i = 0; i < twos; i++)
            {
                Console.Write("*");
            }

            Console.Write("\nThrees:\t");
            for (int i = 0; i < threes; i++)
            {
                Console.Write("*");
            }

            Console.Write("\nFours:\t");
            for (int i = 0; i < fours; i++)
            {
                Console.Write("*");
            }

            // new array declaration
            int[] numbersArray2 = new int[50];
            for (int i = 0; i < numbersArray2.Length; i++)
            {
                numbersArray2[i] = rand.Next(1, 1000);
            }

            //declare max variable
            int max = 0;
           
            //if the current number is larger than
            //the stored maximum number
            //then make it the maximum
            foreach(int i in numbersArray2)
            {
                if (i > max) { max = i; }
            }

            //or you could just use the built in
            //max function
            int maxb=numbersArray2.Max();

            Console.WriteLine("\nthe max is " + max);

            //*************************
            //here is a two dimensional array
            //as declared it has 3 rows and 2 columns
            string[,] books=new string[3,2];
            books[0, 0] = "The Lord of the Rings";
            books[0, 1] = "Tolkein";
            books[1, 0] = "The Grapes of Wrath";
            books[1, 1] = "Steinbeck";
            books[2, 0] = "The martian chronicles";
            books[2, 1] = "Ray Bradbury";

            Console.WriteLine("Enter an author");
            string author = Console.ReadLine();
            //all of our titles are in the 0 column
            //all our authors are in the 1 column
            for (int i = 0; i < 3; i++)
            {
                //see if the authors match
                //i is the counter for the current row
                //1 is the author column
                if (author.Equals(books[i, 1]))
                {
                    //return the title
                    Console.WriteLine(books[i, 0]);
                }
            }
                Console.ReadKey();
        }
    }
}


No comments:

Post a Comment