From September 09, 2019

## Max and Min

To start working with arrays, we can do a simple task to find the indices of minimum and maximum elements in an array.

Given the size of the array, the program fills it with random numbers and creates n tasks according to the argument received by the function startWithTasks.

The actual search is multi-threaded and when done results are displayed with time execution.

``````using System;
using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;

namespace M1
{
public class Program2
{
private static int n = 20000000;
private static int[] a = new int[n];

private static List < int > mins = new List < int > ();
private static List < int > maxs = new List < int > ();

private static Stopwatch stopwatch = new Stopwatch();

public static void Main()
{
Random rand = new Random();

/*
do
{
Console.WriteLine("Enter the size of the array");

{
n = 0;
}
} while (n < 3);
*/

for (int i = 0; i < n; i++)
{
a[i] = rand.Next();
}
});

stopwatch.Reset();
stopwatch.Start();

t1.Start();
t1.Wait();

stopwatch.Stop();

Console.WriteLine(\$"Array filled! in {stopwatch.ElapsedMilliseconds} ms");

/*
for (int i = 1; i <= 8; i++)
{
int temp = i;
int val = i * n / 8;
int fromVal = (i - 1) * n / 8;

Console.WriteLine(\$"from: {fromVal} to: {val}");
{
int tmp = a[0];

for (int j = fromVal; j < val; j++)
{
if (tmp > a[j]) tmp = a[j];
}
});

tMin.Start();
{
int tmp = a[0];

for (int k = fromVal; k < val; k++)
{
if (tmp < a[k]) tmp = a[k];
}

});

tMax.Start();
}

Console.WriteLine(\$"Min: {mins.Min()} found!");
Console.WriteLine(\$"Max: {maxs.Max()} found!");
*/
}

{
stopwatch.Reset();
stopwatch.Start();

for (int i = 1; i <= m; i++)
{
int temp = i;
int val = i * n / m;
int fromVal = (i - 1) * n / m;

int tmp = a[0];

for (int j = fromVal; j < val; j++)
{
if (tmp > a[j]) tmp = a[j];
}
});

tMin.Start();

int tmp = a[0];

for (int k = fromVal; k < val; k++)
{
if (tmp < a[k]) tmp = a[k];
}
});

tMax.Start();
}