K
K
Kakeru2021-11-15 19:21:34
C++ / C#
Kakeru, 2021-11-15 19:21:34

How to form an array, the length of which will be equal to the number of elements that satisfy the condition?

My code
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));
        }
    }
}


I need that in the new array, its length is equal to the number of elements that satisfy the condition, and that this length is also passed to SearchAverage, for example:
The original array, length 5, looks like this: [12 -1 50 -23 43]; a = 10, b = 45; Arithmetic mean: 16.2

Then the new one should look like [12 43]; Arithmetic mean: 27.5

Now I have the arithmetic mean of the new array with an error due to the length, and the array itself does not look the way I need. Help please, after python one headache with arrays in sharpe ..

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alexalexes, 2021-11-15
@kakeru

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 */));

A
ayazer, 2021-11-15
@ayazer

it's easier

var min = 10;
var max = 45;

var array = new int[]
{
    12, -1, 50, -23, 43
};
var avg = array.Average(); //16.2

var newArray = array.Where(c => c > min && c < max && c > 0).ToArray(); // [12, 43]
var newAvg = newArray.Average(); //27.5

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question