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

Closed due to the fact that off-topic participants default locale , LLENN , 0xdb , freim , entithat May 22 at 13:47 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - default locale, LLENN, 0xdb, freim, entithat
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Something sr not 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 locale
  • @default locale, the part where I left working with files because there were suspicions that there was an error somewhere. In essence, the cycle algorithm for finding the arithmetic mean is correct, but for some reason it ends up as zero. Code corrected. - StonedJes
  • string q = string.Join(" ", b); is this variable equal to you? - tym32167 pm
  • @ tym32167 It is equal to the array b, which in turn is equal to the array allnumbers - StonedJes
  • in your case, what exactly is the string written? - tym32167

1 answer 1

The problem is here:

 for (int i = 0; i < symbols.Length; i++) if (!char.IsNumber(symbols[i])) symbols[i] = ' '; else c++; 

I understand why you are doing this: to remove all intermediate characters between numbers altogether?

But the condition of char.IsNumber automatically cuts off the sign "-", that is, you make all negative numbers positive, therefore the sum of negative numbers turns out to be 0.

To separate, you can use a regular expression:

 var matches = Regex.Matches(input, @"[+-]?\d*\.?\d+"); 

In case only whole numbers are needed:

 var matches = Regex.Matches(input, @"[+-]?\d+"); 
  • That's exactly the problem. Can you tell me how else to make sure that only numbers are entered? - StonedJes
  • @StonedJes regex with the pattern "[+ -]? \ D * \.? \ D +". I will add an example. - John
  • @StonedJes Regex you use instead of your separation. Your input string is string r , and run it: Regex.Matches(r, @"[+-]?\d+") . At the output you will receive a Match collection , where Match.Value is a string ready for conversion to a number. You don't need character arrays, you don't need Replace(" "," ") . After Regex, you immediately start to step with the conversion into a number. - John
  • Everything earned. Thank! Only this did not save the exception if the user entered not a number, but a symbol. - StonedJes 5:02 pm