String arrays
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ArraysExampleMorning { class Program { static void Main(string[] args) { //declare a string array. an array is //a variable that can store more than //one value at a time. It is marked by //the use of squlare brackets [] after //the data type string[] members; //before you can use an array //you must make it new and give //it a length //each member of an array //has an index number to identify //it. Indexes always start at 0 members = new string[7]; members[0] = "Brad"; members[1] = "Josephine"; members[2] = "Paul"; members[3] = "Alice"; members[4] = "Lewis"; //here we added the last member //but not the fifth members[6] = "Sally"; //you can access members by means of their index Console.WriteLine("the third member is " + members[2]); //this loops through the array members for (int i = 0; i < members.Length;i++ ) { //if the value for the index is not empty if(members[i] != null) { //print it Console.WriteLine(members[i]); } } //an alternate way of initializing an array. //just less typing string[] colors = new string[] { "red", "green", "blue", "purple", "yellow" }; //you can use a variable for the size of the array //as long as you assign a value to the variable //before you initialize the array Console.WriteLine("How many books do you want to enter"); int numberOfBooks = int.Parse(Console.ReadLine()); string[] books = new string[numberOfBooks]; //loop through the array to add values for (int i = 0; i < books.Length; i++ ) { Console.WriteLine("Enter a Book"); books[i] = Console.ReadLine(); } Console.WriteLine("******************"); //loop through the array to display //its contents for (int i = 0; i < books.Length; i++ ) { Console.WriteLine(books[i]); } Console.ReadKey(); } } }
Number Arrays
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NumberArraysMorning { class Program { /// <summary> /// this program shows how to manipulate number /// arrays /// </summary> static void Main(string[] args) { //make a new random number Random rand = new Random(); //declare an array. an array is a variable //that can store more than one value //at a time. An array is marked by using //square brackets [] after the data type //an array must always have a value fo length //before you can use it. Arrays are also //objects and must be made new to use int[] numberArray = new int[50]; int ones = 0, twos = 0, threes = 0, fours = 0; //populate the array for(int i=0;i<numberArray.Length;i++) { //at each index (represented by the //counter i )place a random number //between 1 and 4 numberArray[i] = rand.Next(1, 5); } //loop through the array and count how //many of each number there is for (int i = 0; i < numberArray.Length; i++) { if (numberArray[i] == 1) { ones++; } if (numberArray[i] == 2) { twos++; } if (numberArray[i] == 3) { threes++; } if (numberArray[i] == 4) { fours++; } } //display the results Console.WriteLine("ones {0}, twos {1}, threes {2}, fours {3} ", ones,twos,threes,fours); //make string with the same number of astrics //as the number of ones Console.Write("\nOnes\t"); for (int i = 0; i < ones; i++ ) { Console.Write("*"); } //twos Console.Write("\nTwos\t"); for (int i = 0; i < twos; i++) { Console.Write("*"); } //threes Console.Write("\nThrees\t"); for (int i = 0; i < threes; i++) { Console.Write("*"); } //fours Console.Write("\nFours\t"); for (int i = 0; i < fours; i++) { Console.Write("*"); } //new array of 50 elements int[] numberArrayB = new int[50]; //populate the array with numbers between //1 and 999 for (int i = 0; i < numberArrayB.Length; i++ ) { numberArrayB[i] = rand.Next(1, 1000); } //declare a vaiable to store the maximum int max = 0; //loop through the array for (int i = 0; i < numberArrayB.Length; i++) { //check to see if the number at the index //is bigger than the current max //if it is replace max with the new number if (numberArrayB[i] > max) { max = numberArrayB[i]; } } //declare variables int sum = 0; double average = 0; //loop and accumulate values into a sum for (int i = 0; i < numberArrayB.Length; i++) { sum += numberArrayB[i]; //same as sum = sum + numberArrayB[i]; } //get the average average = (double)sum / numberArrayB.Length; //display the average Console.WriteLine("\nthe average is {0}", average); //two ways to do max--our loop above //and using the Max() function built into arrays //Console.WriteLine("\nthe max is " + max); Console.WriteLine("\nthe max is " + numberArrayB.Max()); Console.ReadKey(); } } }
No comments:
Post a Comment