The essence of the task is to write a number series to the file, then read this data, replace in them the max. and min. elements on the arithmetic mean of negative elements. The problem is that in the final file, instead of the desired average number, it is replaced by zero. There may be a problem in the loop, which counts the sum of negative numbers, but it seems to be correct. Help to understand the error.
static public void COUNT(string r, string path) { int c = 0; char[] symbols = r.ToCharArray(); for (int i = 0; i < symbols.Length; i++) if (!char.IsNumber(symbols[i])) symbols[i] = ' '; else c++; if (c > 0) //если счетчик чисел больше нуля (в строке есть числа), то: { r = new string(symbols); while (r.IndexOf(" ") != -1) r = r.Replace(" ", " "); string[] allnumberstr = r.Split(' '); double[] allnumbers = new double[allnumberstr.Length]; for (int i = 0; i < allnumberstr.Length; i++) allnumbers[i] = int.Parse(allnumberstr[i]); double maxValue = allnumbers.Max(); double minValue = allnumbers.Min(); int max = 0, min = 0; double sum = 0, k = 0; for (int i = 0; i < allnumbers.Length; i++) { if (allnumbers[i] < 0) { sum += allnumbers[i]; k++; } } double sr = sum / k; for (int i = 0; i < allnumbers.Length; i++) if (allnumbers[i] == maxValue) { max = i; } for (int i = 0; i < allnumbers.Length; i++) if (allnumbers[i] == minValue) { min = i; } allnumbers[max] = sr; allnumbers[min] = sr; double[] b = new double [allnumbers.Length]; for (int i = 0; i < allnumbers.Length; i++) b [i] = allnumbers[i]; string q = string.Join(" ", b); using (FileStream fstream = new FileStream(path, FileMode.OpenOrCreate)) { byte[] array = System.Text.Encoding.Default.GetBytes(q); fstream.Write(array, 0, array.Length); Console.WriteLine("Текст \'{0}\' записан в файл. ", q, path); }
srnot used anywhere. Show where you derive the arithmetic mean. In general, a bit too much code, try to leave only the one that relates to the question . For example, if working with files does not apply to the question, then it can be removed, and the desired value displayed in the console. - default localestring q = string.Join(" ", b);is this variable equal to you? - tym32167 pm