88 lines
2.5 KiB
C#
88 lines
2.5 KiB
C#
using System;
|
|
|
|
public class Program
|
|
{
|
|
static int SumPos(int[] a)
|
|
{
|
|
int r = 0;
|
|
foreach (int x in a) if (x > 0) r += x;
|
|
return r;
|
|
}
|
|
|
|
static int SumNeg(int[] a)
|
|
{
|
|
int r = 0;
|
|
foreach (int x in a) if (x < 0) r += x;
|
|
return r;
|
|
}
|
|
|
|
static int SumArr(int[] a) => SumPos(a) + SumNeg(a);
|
|
|
|
static double AvgArr(int[] a) => a.Length == 0 ? double.NaN : (double)SumArr(a) / a.Length;
|
|
|
|
static int SumEvenPos(int[] a)
|
|
{
|
|
int r = 0;
|
|
for (int i = 0; i < a.Length; i += 2) r += a[i];
|
|
return r;
|
|
}
|
|
|
|
static int SumOddPos(int[] a)
|
|
{
|
|
int r = 0;
|
|
for (int i = 1; i < a.Length; i += 2) r += a[i];
|
|
return r;
|
|
}
|
|
|
|
static int MaxIdx(int[] a)
|
|
{
|
|
if (a.Length == 0) return -1;
|
|
int idx = 0, m = a[0];
|
|
for (int i = 1; i < a.Length; i++)
|
|
if (a[i] > m) { m = a[i]; idx = i; }
|
|
return idx;
|
|
}
|
|
|
|
static int MinIdx(int[] a)
|
|
{
|
|
if (a.Length == 0) return -1;
|
|
int idx = 0, m = a[0];
|
|
for (int i = 1; i < a.Length; i++)
|
|
if (a[i] < m) { m = a[i]; idx = i; }
|
|
return idx;
|
|
}
|
|
|
|
static int ProductBetweenMinMax(int[] a)
|
|
{
|
|
int start = MinIdx(a), end = MaxIdx(a);
|
|
if (start == -1 || end == -1) return -1;
|
|
if (start > end) { int t = start; start = end; end = t; }
|
|
if (end - start <= 1) return -1;
|
|
int r = 1;
|
|
for (int i = start + 1; i < end; i++) r *= a[i];
|
|
return r;
|
|
}
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
Console.Write("input an arr length: ");
|
|
int n = int.Parse(Console.ReadLine());
|
|
int[] arr = new int[n];
|
|
|
|
for (int i = 0; i < arr.Length; i++)
|
|
{
|
|
Console.Write("arr[{0}] = ", i);
|
|
arr[i] = int.Parse(Console.ReadLine());
|
|
}
|
|
|
|
Console.WriteLine("sum of the array's elements is {0}", SumArr(arr));
|
|
Console.WriteLine("avg value of the array is {0}", AvgArr(arr));
|
|
Console.WriteLine("sum of positive array values is {0}, sum of negative array values is {1}", SumPos(arr), SumNeg(arr));
|
|
Console.WriteLine("sum of elements on even positions is {0}, sum of elements on odd positions is {1}", SumEvenPos(arr), SumOddPos(arr));
|
|
Console.WriteLine("index of max array element is {0}", MaxIdx(arr));
|
|
Console.WriteLine("index of min array element is {0}", MinIdx(arr));
|
|
Console.WriteLine("product of numbers between min and max is {0}", ProductBetweenMinMax(arr));
|
|
}
|
|
}
|
|
|