Answer the question
In order to leave comments, you need to log in
How to form an array, the length of which will be equal to the number of elements that satisfy the condition?
using System;
namespace lab5
{
class Program
{
static void Main()
{
static double SearchAverage(double[] array, int n)
{
double s = 0;
for (int i = 0; i < n; i++)
{
s += array[i];
}
double a = Convert.ToDouble(s / array.Length);
return a;
}
int n;
Console.Write("Введите значение a: ");
double a = double.Parse(Console.ReadLine());
Console.Write("Введите значение b: ");
double b = double.Parse(Console.ReadLine());
Console.Write("Введите размер массива: ");
n = Convert.ToInt32(Console.ReadLine());
double[] numbers = new double [n];
Console.WriteLine("Введите элементы исх. массива: ");
for (int i = 0; i < n; i++)
{
numbers[i] = double.Parse(Console.ReadLine());
}
Console.WriteLine("Элементы исходного массива: ");
for (int i = 0; i < n; i++)
{
Console.Write(numbers[i] + " ");
}
Console.WriteLine("\nЗначение среднего арифметического исх. массива: " + SearchAverage(numbers, n));
double[] n_numbers = new double [n];
int k = 0;
for (int i = 0; i < n; i++)
{
if ((numbers[i] > 0) && (numbers[i] > a) && (numbers[i] < b)){
n_numbers[k] = numbers[i];
}
k++;
}
Console.WriteLine("\nЭлементы нового массива: ");
for (int i = 0; i < n; i++)
{
Console.Write(n_numbers[i] + " ");
}
Console.WriteLine("\nЗначение среднего арифметического нового массива: " + SearchAverage(n_numbers, n));
}
}
}
Answer the question
In order to leave comments, you need to log in
The laziest fix is to replace n with k in two places.
Console.WriteLine("\nЭлементы нового массива: ");
for (int i = 0; i < n /* тут заменить на k */; i++)
{
Console.Write(n_numbers[i] + " ");
}
Console.WriteLine("\nЗначение среднего арифметического нового массива: " + SearchAverage(n_numbers, n /* тут заменить на k */));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question