Wednesday, November 3, 2010

Arrays

using System;
using System.Collections.Generic;
using System.Text;

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

void SimpleArray()
{
//one way to declare and initilize

int[] myArray = { 3, 45, 3, 14, 50 };
//always start counting at 0;
Console.WriteLine(myArray[3].ToString());
myArray[3] = 100;


// Console.WriteLine(myArray.Length);

int[] myArray2 = new int[5];
myArray2[0] = 23;
myArray2[1] = 22;

}

void CollectScores()
{
Console.WriteLine("how many scores do you want enter");
int number = int.Parse(Console.ReadLine());

double[] scores = new double[number];

for (int i = 0; i < number; i++)
{
Console.WriteLine("Enter a score");
scores[i] = double.Parse(Console.ReadLine());
}

CalculateAverages(scores);


}

void CalculateAverages(double[] rawScores)
{
double sum = 0;
//for every double value in the array
//scores which stores doubles
foreach (double score in rawScores)
{
Console.WriteLine(score.ToString());
sum += score;
}
Console.WriteLine("**********************");
Console.WriteLine("The sum is {0} ", sum);
double average = sum / rawScores.Length;
Console.WriteLine("The average is {0}", average.ToString("c"));
}
}
}

No comments:

Post a Comment