Thursday, October 18, 2012

Loops and arrays 1

Here is a first look at creating arrays and for loops:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArraysandLoops
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
            Console.ReadKey();
        }

        private void Run()
        {
           
            DisplayArray();
            InitializedArray();
        }

        private string[] CreateArray()
        {
            //declare an array of strings
            string[] cheese = new string[5];
            return cheese;
           // FillArray(cheese);
        }

        private string[] FillArray()
        {
            //create an array variable to store
            //the array returned by create array
            string[] queso = CreateArray();
            //this loop starts at zero, and loops
            //until it is less than the length of the array (5)
            //i++ increments the counter by 1
            for (int i = 0; i < queso.Length; i++)
            {
                Console.WriteLine("enter a cheese");
                string cheeseName = Console.ReadLine();
                //assigns it to the current index of the array
                queso[i] = cheeseName;
            }//end for loop

            return queso;
        }//end fill Array

        private void DisplayArray()
        {
            
            string[] fromage = FillArray();
            Console.WriteLine("************************\n");
   
            for (int i = 0; i < fromage.Length; i++)
            {
                Console.WriteLine(fromage[i]);
            }//end for

            Console.WriteLine(fromage[3]);
        }//end displayArray

        private void InitializedArray()
        {
            //create an array of integers and 
            //assign them immediately
            int[] numbers = new int[] { 1, 3, 5, 6, 8, 2 };
            //loop through the array to display them
            for (int i = 0; i < numbers.Length; i++)
            {
                Console.WriteLine(numbers[i]);
            }
        }




    }//end class
}//end namespace

Here is the example of summing and averaging arrays
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SumandAverageArrays
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.CreateArray();
            Console.ReadKey();
        }

        private void CreateArray()
        {
            double[] numbers = new double[] { 2, 3.21, 4, 5.234, 1.2, 6 };
            GetSum(numbers);

        }

        private void GetSum(double[] numberArray)
        {
            double sum=0;
            for(int i=0;i<numberArray.Length;i++)
            {
                //+= equivalent to sum = sum + numberarray[i]
                sum+=numberArray[i];
            }

            double average = sum / numberArray.Length;
            Console.WriteLine("The sum is {0}", sum);
            Console.WriteLine("The average is {0}", average);
            //these built in methods do the same thing
            Console.WriteLine("**********************");
            Console.WriteLine(numberArray.Sum());
            Console.WriteLine(numberArray.Average());
        }
    }
}

No comments:

Post a Comment