//The following program manipulates array of integer numbers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace p0Random
{
class Program
{
static void Main(string[] args)
{
const int MAX_SIZE = 20;
Random rand = new Random(0);
int[] a = new int[MAX_SIZE];
for (int i = 0; i < a.Length; i++)
{
a[i] = rand.Next(1, 101);
}
DisplayArray(a);
int largest;
float Ave = Average(a);
Console.WriteLine("Average:{0}\n", Ave);
Console.Write("Enter a number to search for: ");
int x = int.Parse(Console.ReadLine());
int position = Search(a, x);
if (position == -1)
Console.WriteLine("Not exists ");
else
{
Console.WriteLine("{0} is found at position {1}", a[position], position);
}
}
static float Average(int[] arr)
{
float sum = 0;
for (int i = 0; i < arr.Length ; i++)
{
sum = sum + arr[i];
}
return sum / arr.Length; ;
}
static int Search(int[] arr, int target)
{
int i = 0;
while (arr[i] != target)
{
i = i + 1;
if (i == arr.Length)
return -1;
}
return i;
}
static void DisplayArray(int[] a)
{
Console.WriteLine("The array is: ");
foreach (int element in a)
{
Console.Write("{0} ", element);
}
Console.WriteLine();
}
}
}
Program 0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace RunningTimeTest
{
class P0_0
{
static int size = 10000;
static void Main(string[] args)
{
int[] X = new int[size];
double[] A = new double[size];
Random rand = new Random(1);
for (int i = 0; i < size; i++)
X[i] = rand.Next(size);
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine("Program is running, please wait...\n\n");
//Call Solution_1 to compute prefixAverages here
//Call Solution_2 to compute prefixAverages here
sw.Stop();
Console.WriteLine("\aTime elapsed: {0}ms", sw.Elapsed.TotalMilliseconds.ToString());
sw.Reset();
}
static void solution1(/*add parameters as needed */)
{
//implement this method according to the the prefix average algorithm on slide 44
}
static void solution2(/*add parameters as needed */)
{
//implement this method according to the the prefix average algorithm on slide 45
}
}
}