Here is the first set of array examples
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ArrayloopExample
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
//p. CreateArray();
// p.CreateArray2();
// p.CreateArrayWithVariable();
p.CreateTwoDimensionalArray();
Console.ReadKey();
}
void CreateArray()
{
//this creates and populates an array of movies
string[] movies = new string[3];
movies[0] = "Gravity";
movies[1] = "Apollo 13";
movies[2] = "Space Cowboys";
Console.WriteLine("Enter a number 1 to 3");
int number = int.Parse(Console.ReadLine());
Console.WriteLine(movies[number - 1]);
}
void CreateArray2()
{
//this is another way to populate an array
string[] books = new string[] { "American Gods", "Alchemist",
"Cat in the Hat", "IQ84", "SnowCraft" };
Console.WriteLine("Enter a number 1 to 5");
int number = int.Parse(Console.ReadLine());
Console.WriteLine(books[number - 1]);
}
void CreateArrayWithVariable()
{
//this give an array a size using a variable
//the variable must have a value before it can
//be used in the array
Console.WriteLine("How many items do you wish to store?");
int number = int.Parse(Console.ReadLine());
int[] myArray = new int[number];
Console.WriteLine("the array has {0} elements", myArray.Length);
}
void CreateTwoDimensionalArray()
{
//this shows a 2 dimensional array
string[,] books = new string[3, 2];
books[0, 0] = "War and Peace";
books[0, 1] = "Tolstoy";
books[1, 0] = "The lord of the Rings";
books[1, 1] = "Tolkein";
books[2, 0] = "Enders Game";
books[2, 1] = "Card";
//Console.WriteLine("Select an author: Tolstoy, Tolkein or Card");
//string author = Console.ReadLine();
//this for loop loops through the array and displays
//its contents in reverse (-- instead of ++)
for (int i = 2; i >=0; i--)
{
//if (books[i, 1].Equals(author))
//{
Console.WriteLine(books[i, 0] + ", " + books[i,1]);
//}
}
}
}
}
this one focuses more on loops
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoopsandStuff
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
string status = "yes";
//the while loop loops as long as status = yes
while (status.Equals("yes"))
{
p.CreateArray();
Console.WriteLine("Continue: yes / no");
status = Console.ReadLine();
status.ToLower(); //sets the reply to all lower case
}
//Console.ReadKey();
}
void CreateArray()
{
Console.WriteLine("How big do you want your array?");
int size = int.Parse(Console.ReadLine());
int[] numbers = new int[size];
//call method populate arrays and passes the array
PopulateArray(numbers);
}
void PopulateArray(int[] num)
{
//loop through the array and add values
for (int i = 0; i < num.Length; i++)
{
Console.WriteLine("Enter an integer");
num[i] = int.Parse(Console.ReadLine());
}
ReadArray(num);
}
void ReadArray(int[] num)
{
Console.Clear();
//loop through the array and display its contents
for (int i=0; i < num.Length; i++)
{
Console.WriteLine(num[i]);
}
//use built in math functions to analyze array values
Console.WriteLine("the sum is {0}", num.Sum());
Console.WriteLine("the Average is {0}", num.Average());
Console.WriteLine("the maximum value is {0}", num.Max());
int range = num.Max() - num.Min();
Console.WriteLine("the range is {0}", range);
// int add = num[1] + num[2];
// Console.WriteLine("the sum of values 2 and 3 is {0}", add);
}
}
}
No comments:
Post a Comment