Last active
February 17, 2016 19:43
-
-
Save fotonmoton/df4670d67e0002a304d7 to your computer and use it in GitHub Desktop.
abs for all random negative elements in array that equals to maximum value of array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find all absolute values of max value of array. | |
*/ | |
using System; | |
using System.Collections.Generic; | |
namespace lab2 | |
{ | |
class Program | |
{ | |
public static void array_fill(int n) | |
{ | |
} | |
static void Main(string[] args) | |
{ | |
int length; | |
int max = 0; | |
Console.Write("please input length of array: "); | |
length = int.Parse(Console.ReadLine()); | |
Console.WriteLine(); | |
// Arrays and lists for storing indexes of max and abs(-max) elements | |
int[] rand_array = new int[length]; | |
List<int> indexes_of_maxes = new List<int>(); | |
List<int> indexes_of_abs_maxes = new List<int>(); | |
// Object for creating random values | |
Random rand = new Random(); | |
// Fill array with random values | |
for (int i = 0; i < rand_array.Length; i++) | |
{ | |
rand_array[i] = rand.Next(-10, 10); | |
} | |
// Put to console rand_array elements | |
Console.WriteLine("random array contain: "); | |
for (int i = 0; i < rand_array.Length; i++) | |
{ | |
Console.WriteLine("rand_array[{0}]={1}", i, rand_array[i]); | |
} | |
Console.WriteLine(); | |
// Finding maximum value of array | |
for (int i = 0; i < rand_array.Length; i++) | |
{ | |
if (rand_array[i] > max) | |
{ | |
max = rand_array[i]; | |
} | |
} | |
// Fiding all elemets with max and abs(-max) value and add they indexes to list. | |
for (int i = 0; i < rand_array.Length; i++) | |
{ | |
if (rand_array[i] == max) | |
{ | |
indexes_of_maxes.Add(i); | |
} | |
if (rand_array[i] < 0 && Math.Abs(rand_array[i]) == max) | |
{ | |
indexes_of_abs_maxes.Add(i); | |
} | |
} | |
Console.WriteLine("biggest value of rand_array is: {0}", max); | |
Console.WriteLine(); | |
// put to console all elements with max value | |
Console.WriteLine("All elements with max value:"); | |
for (int i = 0; i < indexes_of_maxes.Count; i++) | |
{ | |
Console.WriteLine("rand_array[{0}]", indexes_of_maxes[i]); | |
} | |
Console.WriteLine(); | |
// put to console all elements with abs(-max) value | |
Console.WriteLine("All elements with abs(-max) value:"); | |
if (indexes_of_abs_maxes.Count == 0) | |
{ | |
Console.WriteLine("None of the negative elements of rand_array are the abs of max value"); | |
} | |
else | |
{ | |
for (int i = 0; i < indexes_of_abs_maxes.Count; i++) | |
{ | |
Console.WriteLine("rand_array[{0}]", indexes_of_abs_maxes[i]); | |
} | |
} | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment