Tuesday, October 15, 2013

Arrays and loops Morning

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

namespace loopyExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            //set up a string variable for the while loop
            string status = "yes";
            //loop as long as the status variable = "yes"
            while(status.Equals("yes"))
            {
                //call methods
                p.ArrayBasedOnVariable();
                p.CreateArray();
                p.AnotherWayToCreateAnArray();
                //check to see if user wants to continue
                Console.WriteLine("Continue yes/no");
                status = Console.ReadLine();
                
              status.ToLower();
            }
            //don't need readkey with the loop
            //Console.ReadKey();
        }

        void CreateArray()
        {
            //this just looks at how an array is structured
            //declare it with a type and []
            string[] movies = new string[4];
            //4 is the number of elements the array can contain
            //each array item is indexed
            movies[0] = "The Princess Bride";
            movies[1] = "Die Hard";
            movies[2] = "Breakfast at Tiffany's";
            movies[3] = "Galaxy Quest";
            //you can access an particular array item
            //by its index
            Console.WriteLine(movies[2]);

        }

        void AnotherWayToCreateAnArray()
        {
            //another way to declare and array
            string[] dogBreeds = new string[] { "collie", "poodle", "Labrador", "spaniel", "bull dog" };

            //a for loop to read through the array's items
            //int i is a counter we set it to 0 (all arrays begin with zero)
            //we keep looping until i is no longer less than the length
            //of the array. With every loop we increment i (add 1)
            for (int i = 0; i < dogBreeds.Length; i++)
            {
                Console.WriteLine(dogBreeds[i]);
            }
        }

        void ArrayBasedOnVariable()
        {
            //here we get the length of the array from the user
            Console.WriteLine("how many integers do you want to enter?");
            int size = int.Parse(Console.ReadLine());

            //size must have a value before you can use
            //the variable in the array
            int[] numbers=new int[size];

            //loop through the array and add numbers to it
            for (int i = 0; i < size;i++)
            {
                Console.WriteLine("enter an Integer");
                numbers[i] = int.Parse(Console.ReadLine());
           }
            Console.Clear(); //this just clears the stuff above

            //I calculate the total in two different ways
            //you only need to use one of them
            //the .Sum() and .Average() methods
            //exist only in C# . In most languages you
            //have to accumulate the values itself
            int total = 0;
            for (int i = 0; i < size;i++)
            {
               
                Console.WriteLine(numbers[i]);
                //the += is the same as total + total + numbers[i];
                total += numbers[i];
            }

            //this is the same as the Average() method
            double average = (double)total / numbers.Length;

            //this displays both sets of results. they are the same
            Console.WriteLine("total manually {0}", total);
            Console.WriteLine("Average Manually {0}", average);
            Console.WriteLine("*********************");
            Console.WriteLine("the total using Sum is {0}",numbers.Sum());
           Console.WriteLine("the average using Average is {0}",numbers.Average());


        }
    }

}

No comments:

Post a Comment