Tuesday, October 27, 2015

arrays

Here is the code from class such as it is.

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

namespace arraysExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //let someone enter test scores
            //let them enter as many as they want
            // enter -1 to quit
            double[] grades;
            double grade = 0;
           // double sum = 0;
            int counter = 0;
            /*
            every value is distinquished by an index

                    double[] grades = new double[20];
                    grades[0]=100;
                    grades[1]=98;
                    grades[2]=10;
                    grades[3]=84;
                    grades[19]=34;
            this is an alternate way to declare an array
            double grades[] = new double[] {90, 30, 24.5, 60, 21.5};
            */
            Console.WriteLine("about how many grades do think you want to enter");
            int number = int.Parse(Console.ReadLine());

            grades = new double[number];


            Console.WriteLine("Use -1 to exit");
            do
            {
                Console.WriteLine("Enter Grade");
                grade = double.Parse(Console.ReadLine());

                if (grade != -1)
                {
                    if (counter < grades.Length)
                    {
                        //sum += grade;
                        grades[counter] = grade;
                        counter++;
                    }
                }

            } while (grade != -1);

            for (int i = 0; i < grades.Length; i++)
            {
                Console.WriteLine(grades[i]);
            }

            double average = grades.Sum() / counter;
            Console.WriteLine("you entered {0} grades", counter);
            Console.WriteLine("The average is {0}", average);

            Console.WriteLine("the Fifth Element is {0}", grades[4]);
            grades[4] = 90;
            Console.WriteLine("the fifth element is now {0}", grades[4]);

            Random rand = new Random();
            int[] randomNumbers = new int[10];

            for (int i = 0; i < randomNumbers.Length; i++)
            {
                randomNumbers[i] = rand.Next(1, 101);
            }

            for (int i = 0; i < randomNumbers.Length; i++)
            {
                Console.WriteLine(randomNumbers[i]);
            }

            Console.WriteLine("the total is {0}", randomNumbers.Sum());
            Console.WriteLine("The Average is {0}",
                randomNumbers.Average());


            string[,] songs = new string[3, 2];
            songs[0, 0] = "Royals";
            songs[0, 1] = "Lorde";
            songs[1, 0] = "Hard Days Night";
            songs[1, 1] = "Beatles";
            songs[2, 0] = "Satisfaction";
            songs[2, 1] = "Rolling Stones";

            Console.WriteLine("Give me an artist");
            string artist = Console.ReadLine();

            for (int i = 0; i < 3; i++)
            {
                if (artist.Equals(songs[i, 1]))
                {
                    Console.WriteLine(songs[i, 0]);
                }

            }






                Console.WriteLine("Press any key to exit");
            Console.ReadKey();
            

        }
    }
}


No comments:

Post a Comment