C# - Read numbers from a textfile and output averages - c#

I've looked at previous posts and none have the answer that I'm looking for.
I'm new to C# and trying to get this little application to work.
I have a file named "hours.txt" with 30 numbers in it and I want to read the file and output the Average and the Highest Number, but I can't seem to get it to work. I've tried changing the array to a string but I'm just really stuck on how to get this to work. Any help or tips would be greatly appreciated.
int[] hoursArray = new int[30];
StreamReader fileSR = new StreamReader("hours.txt");
int counter = 0;
string line = "";
line = fileSR.ReadLine();
while (line != null)
{
hoursArray[counter] = line;
counter = counter + 1;
line = fileSR.ReadLine();
}
fileSR.Close();
int total = 0;
double average = 0;
for (int index = 0; index < hoursArray.Length; index++)
{
total = total + hoursArray[index];
}
average = (double)total / hoursArray.Length;
int high = hoursArray[0];
for (int index = 1; index < hoursArray.Length; index++)
{
if (hoursArray[index] > high)
{
high = hoursArray[index];
}
}
Console.WriteLine("Highest number is: " + high);
Console.WriteLine("The average is: " + average);
Console.ReadLine();

This is wrong since your are putting a string into a int place.
hoursArray[counter] = line;
Parse string to int first.
hoursArray[counter] = int.Parse(line);

As an alternative method, using LINQ, the code could be replaced with the more concise:
string[] lines = System.IO.File.ReadAllLines("hours.txt");
int[] values = lines.Select((l) => int.Parse(l)).ToArray();
double average = values.Average();
int peak = values.Max();

Related

Error from the 94th Fibonacci number in C#

I am calculating Fibonacci numbers in C# and I am getting wrong numbers since the number 94.
This is my code.
static void Main(string[] args)
{
string N = Console.ReadLine();
ulong n = ulong.Parse(N);
ulong[] fibonacci = new ulong[n+1];
fibonacci[0] = 0;
if (n == 1)
{
fibonacci[1] = 1;
}
if (n>=2)
{
fibonacci[1] = 1;
for (ulong i = 2; i < n+1; i++)
{
fibonacci[i] = (ulong)fibonacci[i-1] + (ulong)fibonacci[i-2];
}
}
}
Console.WriteLine(fibonacci[n]);
I am ok until the 93th number which is 12200160415121876738, but I am getting 1293530146158671551 with the 94th, the real one is 19740274219868223167.
I don't know what could be wrong with my code.
I think you don't need to store the number of N in the ulong type!
It can be stored as an int as well!
The most massive part is to store the value of the Nth Fibonacci number.
You can calculate it more concise without considering a large array for constructing the last number!
public static void Main(string[] args)
{
string N = Console.ReadLine();
int n = int.Parse(N);
BigInteger[] fibonacci = new BigInteger[3];
fibonacci[0] = 0;
fibonacci[1] = 1;
fibonacci[2] = 1;
if (n>=3)
{
for (int i = 3; i < n+1; i++)
{
fibonacci[0]=fibonacci[1];
fibonacci[1]=fibonacci[2];
fibonacci[2]=fibonacci[1]+fibonacci[0];
}
}
Console.WriteLine(fibonacci[2]);
}
The maximum value of ulong is 18,446,744,073,709,551,615.
You're trying to store 19,740,274,219,868,223,167, which is larger than that. By default a ulong won't throw an exception when it overflows, but you can force it to by adding a checked { } around the code:
checked
{
fibonacci[i] = (ulong)fibonacci[i-1] + (ulong)fibonacci[i-2];
}
Then running your code, sure enough, we get:
System.OverflowException: Arithmetic operation resulted in an overflow.
Fortunately, there is a type in .NET for working with really big integers. It's called BigInteger.
Changing your code to use it is relatively trivial:
string N = Console.ReadLine();
ulong n = ulong.Parse(N);
BigInteger[] fibonacci = new BigInteger[n+1];
fibonacci[0] = 0;
if (n == 1)
{
fibonacci[1] = 1;
}
if (n>=2)
{
fibonacci[1] = 1;
for (ulong i = 2; i < n+1; i++)
{
fibonacci[i] = fibonacci[i-1] + fibonacci[i-2];
}
}
Your code now works as expected and outputs:
19740274219868223167
Even changing the input to 900 still works:
54877108839480000051413673948383714443800519309123592724494953427039811201064341234954387521525390615504949092187441218246679104731442473022013980160407007017175697317900483275246652938800
Try it online

How to share random numbers out evenly in C#

I am looking at sharing out a fixed number of 32 teams between a varied number of people.
Of course, 32 may not always be evenly divisible, but for the sake of this exercise, lets say I am looking to share the 32 teams between 4 people, so a maximum number of 8 teams per person.
int max = 32 / numb;
foreach (string value in wcteams)
{
//Assigning teams to players
int selection = random.Next(0, numb);
int[] counter = new int[max];
counter[selection] = counter[selection] + 1;
if (counter[selection] < max)
{
Console.WriteLine(inputtedNames[selection] + " has drawn " + value);
}
}
Right now, I can run that code and I will get a list back of randomly chosen people along with their team. But the limit will not be implemented and some players will end up with more teams than others.
I understand that the following code:
counter[selection] = counter[selection] + 1;
Is not working to add up the number of teams that the user has received, am I on the right track here with how to tally up the number of times a player has been randomly selected or is there another method that I should be doing?
One problem in your code is you are initializing counter inside the loop. Also what happens if the count[selection] > max? you leave the team and don't assign it to anyone else.
Try the following code.
int numb = 4;
int max = 32 / numb;
int[] counter = new int[max];
foreach (string value in wcteams)
{
bool selectionComplete = false;
while(!selectionComplete)
{
int selection = random.Next(0, numb);
counter[selection] = counter[selection] + 1;
if (counter[selection] <= max)
{
selectionComplete = true;
Console.WriteLine(selection + " has drawn " + value);
}
}
}
I cannot figure your code but this should work.
public static Random randomT = new Random();
public static List<List<string>> DivideTeams(string[] teams, int personCount)
{
List<List<string>> divideTeams = new List<List<string>>();
if (teams.Length % personCount != 0)
{
throw new ArgumentOutOfRangeException();
}
//shuffle teams
for(int k = teams.Length -1; k > 0; k--)
{
int trade = random.Next(k + 1);
string temp = teams[trade];
teams[trade] = teams[k];
teams[k] = temp;
}
for (int j = 0; j < personCount; j++)
{
divideTeams.Add(new List<string>());
for (int i = 0; i < teams.Length / personCount; i++)
{
divideTeams[j].Add(teams[i]);
}
}
return divideTeams;
}

Almost Ordered not sorting the exact amount of values i give it

this is a really easy question but i cant figure out a way around it. Apparently the almost ordered has a bug that it might randomize a little bit more than you ask it. the code is rather simple:
public void Section1Task1AlmostOrdered(int arraySize, int percentage)
{
int[] testArray = new int[arraySize];
Console.WriteLine("Ordered List: ");
for (int i = 1; i <= testArray.Length; i++)
{
testArray[i-1] = i;
Console.Write(i + "\t");
}
Console.WriteLine("Almost Ordered List: ");
testArray = shuffler.AlmostOrdered(arraySize, percentage);
for (int i = 0; i < testArray.Length; i++)
{
Console.Write(testArray[i] + "\t");
}
}
The shuffler is this part of the code:
public int[] AlmostOrdered(int n, double p)
{
if (p > 100)
{
throw new InvalidOperationException("Cannot shuffle more than 100% of the numbers");
}
int shuffled = 0;
//Create and Populate an array
int[] array = new int[n];
for(int i = 1; i <= n; i++)
{
array[i-1] = i;
}
//Calculate numbers to shuffle
int numsOutOfPlace = (int) Math.Ceiling(n * (p / 100));
int firstRandomIndex = 0;
int secondRandomIndex = 0;
do
{
firstRandomIndex = this.random.Next(n-1);
// to make sure that the two numbers are not the same
do
{
secondRandomIndex = this.random.Next(n - 1);
} while (firstRandomIndex == secondRandomIndex);
int temp = array[firstRandomIndex];
array[firstRandomIndex] = array[secondRandomIndex];
array[secondRandomIndex] = temp;
shuffled++;
}
while (shuffled < numsOutOfPlace);
return array;
}
When i enter values 10 for array size and 40 for percentage to be shuffled, it is shuffling 5 numbers instead of 4. Is there a way to perfect this method to make it more accurate?
Likely the problem is with the calculation:
int numsOutOfPlace = (int)Math.Ceiling(n * (p / 100));
So if p=40 and n=10, then in theory you should get 4. But you're dealing with floating point numbers. So if (p/100) returns 0.400000000001, then the result will be 4.000000001, and Math.Ceiling will round that up to 5.
You might want to replace Math.Ceiling with Math.Round and see how that works out.

Add up al numbers in a string C#

I want to add up all numbers in a string, I am sure this can be done easy with a for loop.
I have:
int numbers = 1234512345;
for (int i = 0 ; i numbers.Length ; i++)
{
int total;
total = int [i];
}
But it won't work for a reason, I am puzzled a lot.
For one, the "string" you're trying to iterate over is an int. You probably meant something along the lines of
string numbers = "1234512345"
After that, there are several ways to do this, my favorite personally is iterating over each character of the string, using a TryParse on it (this eliminates any issues if the string happens to be alphanumeric) and totaling the result. See below:
static void Main(string[] args) {
string numbers = "1234512345";
int total = 0;
int num; // out result
for (int i = 0; i < numbers.Length; i++) {
int.TryParse(numbers[i].ToString(), out num);
total += num; // will equal 30
}
Console.WriteLine(total);
total = 0;
string alphanumeric = "1#23451!23cf47c";
for (int i = 0; i < alphanumeric.Length; i++) {
int.TryParse(alphanumeric[i].ToString(), out num);
total += num; // will equal 32, non-numeric characters are ignored
}
Console.WriteLine(total);
Console.ReadLine();
}
Like others have posted though, there are several ways to go about this, it's about personal preference most of all.
this should do what you want
int total = 0;
foreach(char numchar in numbers)
{
total += (int)char.GetNumericValue(numchar);
}
EDIT:
1 line solution:
int total = numbers.Sum(x=> (int)char.GetNumericValue(x));
PS: Why the downvotes?

Get number inside of a string and do a loop

I want to get a number of a string, and separate the string and the number, and then, do a loop and call a method the number of times the string says.
The string has to have this structure: "ABJ3" (Only one number accepted and 3 characters before it)
This is my code, but it repeat hundred of times, I don't know why
int veces = 0;
for (int i = 0; i < m.Length; i++)
{
if (Char.IsDigit(m[i]))
veces = Convert.ToInt32(m[i]);
}
if (m.Length == 4)
{
for (int i = 0; i <= veces; i++)
{
m = m.Substring(0, 3);
operaciones(m, u, t);
Thread.Sleep(100);
}
}
operaciones(m,u,t);
if (u.Length >= 14)
{
u = u.Substring(0, 15);
}
Some help please?
You have to convert your m[i] ToString() right now you are sending the char value to Convert.ToInt32 and that is a much higher value (9 = 57 for example)
char t = '9';
int te = Convert.ToInt32(t.ToString());
Console.WriteLine(te);
This gives us a result of 9 but
char t = '9';
int te = Convert.ToInt32(t);
Console.WriteLine(te);
Gives us a result of 57
So you need to change
veces = Convert.ToInt32(m[i]);
to
veces = Convert.ToInt32(m[i].ToString());
Hope it helped.
Best regards //KH.
You cannot convert the digits like this. You're overwriting them and taking only the last one. Moreover, you're taking its ASCII code, not digit value. You have to extract all digits first then convert them:
int position = 0;
int veces = 0;
string temp = ""
for (int i = 0; i < m.Length; i++) {
if (Char.IsDigit(m[i]))
position = i;
else
break;
}
veces = Convert.ToInt32(m.SubString(0, i + 1));
Alternatively, you can use regex instead.

Categories

Resources