Your code is unnecessarily complex. use default .Net implementations to make your code readable and understandable.
string skaiciai = "skaiciai.txt";
string[] lines = File.ReadAllLines(skaiciai); // use this to read all text line by line into array of string
List<int> numberList = new List<int>(); // use list instead of array when length is unknown
for (int i = 0; i < lines.Length; i++)
{
//if (s == string.Empty) continue; // No need to check for that. Split method returns empty array so you will never go inside inner loop.
string[] line = lines[i].Split(' ');
for (int j = 0; j < line.Length; j++)
{
string number = line[j];
int n;
if (int.TryParse(number, out n)) // try to parse string into integer. returns true if succeed.
{
numberList.Add(n); // add converted number into list
}
}
}
// Other way using one line linq to store numbers into list.
//List<int> numberList = lines.SelectMany(x => x.Split(' ')).Select(int.Parse).ToList();
int totalNumbers = numberList.Count;
int sum = numberList.Sum();
int product = numberList.Aggregate((a, b) => a*b);
int min = numberList.Min();
int max = numberList.Max();
double average = sum/(double)totalNumbers;
Tell me if your text file contains double numbers because then you have to use double type instead of int.
Also try to use suitable names for variables. names like t
or t1
instead of lines
and line
does not really describe anything and makes your code harder to understand.
If you have large list of numbers you probably have to use long
type or double
if they have decimal part.
solved C# array of numbers