Access into global variable by Console.Readline() in C# - c#

I'm trying to improve my program for Fibonacci numbers using of memoization:
public class MyGlobals
{
public long TotNum { get; set; }
public long[] MyNumbers { get; set; }
public void GetParam()
{
Console.Write("n = ");
this.TotNum = long.Parse(Console.ReadLine());
this.MyNumbers = new long[this.TotNum + 1];
// set all numbers to -1
for (int i = 0; i < this.MyNumbers.Length; i++)
{
this.MyNumbers[i] = -1;
}
}
}
class Program
{
static void Main(string[] args)
{
MyGlobals globVariable = new MyGlobals();
globVariable.GetParam();
long n = globVariable.TotNum;
Console.WriteLine("Fib ({0}) = {1}", n, Fibonacci(n));
Console.ReadKey();
}
static long Fibonacci(long n)
{
MyGlobals globVariable = new MyGlobals();
if (n <= 1)
{
return 1;
}
if (globVariable.MyNumbers[n] != -1)
{
return globVariable.MyNumbers[n];
}
else
{
globVariable.MyNumbers[n] = Fibonacci(n - 1) + Fibonacci(n - 2);
}
return globVariable.MyNumbers[n];
}
}
I'm trying to do something like feed an array by -1 in MyGlobals class for further using MyNumbers array in Fibonacci static method.
Until line where I'm starting to call recursive fibonacci method it holds MyNumbers array in memory. But in Fibonacci method, when I create new instance of MyGlobals class for calling MyNumbers array is this array empty... What I'm doing wrong. Can you anybody help me on this, please. Thank you very much in forward.

Declare globVariable as a static member of the Program class like so:
class Program
{
static MyGlobals globVariable = new MyGlobals();
static void Main(string[] args)
{
globVariable.GetParam();
long n = globVariable.TotNum;
Console.WriteLine("Fib ({0}) = {1}", n, Fibonacci(n));
Console.ReadKey();
}
static long Fibonacci(long n)
{
if (n <= 1)
{
return 1;
}
if (globVariable.MyNumbers[n] != -1)
{
return globVariable.MyNumbers[n];
}
else
{
globVariable.MyNumbers[n] = Fibonacci(n - 1) + Fibonacci(n - 2);
}
return globVariable.MyNumbers[n];
}
}

There is no such thing as global variables in C#. The problem you're having relates to instances of nonstatic classes.
You effectively have three separate units in your code:
One class that asks for input, holds this input and holds an array of result variables (MyGlobals). This is in fact way too much for a single class and should ultimately be split up.
One method that calculates Fibonacci numbers and stores them into the previous class (Fibonacci).
A Program class and Main() method which host your console application.
Now your problem is that you don't know how to access the array of inputs stored in 1 from method 2. There are various ways to solve that, each with their own cons and pros. The most obvious one is to pass a reference.
But before that, clean up your code: give classes and methods meaningful names, and extract logic into separate classes.
Here you'll remain with three classes:
public class FibonacciInput
{
public void GetParam()
{
// Your "MyGlobals" logic
}
}
Then the calculation logic:
public class FibonacciCalculator
{
public long Fibonacci(long index, long[] range)
{
// Your "Fibonacci()" logic
}
}
And the program:
class Program
{
static void Main(string[] args)
{
FibonacciInput input = new FibonacciInput();
FibonacciCalculator calculator = new FibonacciCalculator();
input.GetParam();
long n = input.TotNum;
Console.WriteLine("Fib ({0}) = {1}", n, calculator.Fibonacci(n, input.MyNumbers));
Console.ReadKey();
}
}
Now your calculator doesn't know anything about your input, and the need for "global variables" goes away.
The point is that the Fibonacci() method needs two things: the index (the Nth Fibonacci number it should calculate) and an array to work with (which you initialized on beforehand).
So by calling calculator.Fibonacci(n, input.MyNumbers), you solve all problems at once.

Well, may be it's not really answers your question but i'd refactor your code dividing it to logical parts where each part is only responsible for one thing :
UI
Global variables
Class that knows how to work with fibo sequence
Program (entry point)
Refactored code may look something among the lines of :
// Globals should be static
public static class MyGlobals
{
public static long TotNum { get; private set; }
public static long[] MyNumbers { get; private set; }
public static void SetNum(long num)
{
TotNum = num;
MyNumbers = new long[TotNum + 1];
}
}
// interacts with UI
public static class UIHelper
{
public static long GetParam()
{
Console.Write("n = ");
var number = long.Parse(Console.ReadLine());
return number;
}
}
// Knows how to calc fibo
static class Fibo
{
static long Calc(long[] nums, long n)
{
... calc fibonacci logic
}
}
class Program
{
static void Main(string[] args)
{
// now we can use them all
// first lets get value from console
var num = UIHelper.GetParam();
// set global variables with this value
MyGlobals.SetNum(num);
// output result :
Console.WriteLine("Fib ({0}) = {1}", n, Fibo.Calc(MyGlobals.MyNumbers, MyGlobals.TotalNum));
Console.ReadKey();
}
}
P.S.
Whether to send global values as parameters to Fibo.Calc() method or to access them directly from inside of it it's up to you. I vote for first option because it makes it easier to test this method by passing mock data.

Related

Reactive Extension Simple Example not Working with Number Generator

Trying to setup Reactive Extension for a simple example. Eg: there is random Number Generator, and anytime Number Divisible by three is spotted, "Write Hello" and Observe the Event. However it does not seem to be working. Feel free to edit line, or rewrite code to make work correctly. Its a quick two page copy into Console Program.
Eventually, want number generator to work in a stream , while SeeNumber observes in a Console program for one minute.
Code follows this resource: Reactive Extensions
Goals which can be incorporated in code:
Make the Number Generator streaming
Also reading, are Subjects are good in RX? Feel free to modify code without subjects if true,
Main.cs
static void Main(string[] args)
{
var generateNumber = new GenerateNumber();
var seeNumber = new SeeNumber(generateNumber);
generateNumber.DivideByThreeSpotted.Subscribe(seeNumber.OnDivideByThreeSpotted);
Console.WriteLine(generateNumber.NumberValue);
Console.ReadKey();
}
Produce Number:
public class GenerateNumber
{
public int NumberValue { get; set; }
public GenerateNumber()
{
NumberValue = CreateData();
}
public Subject<GenerateNumber> divideByThreeSpotted = new Subject<GenerateNumber>();
public IObservable<GenerateNumber> DivideByThreeSpotted
{
get { return this.divideByThreeSpotted; }
}
public int CreateData()
{
Random random = new Random();
return random.Next(0, 100);
}
public void SpotDivideByThree(GenerateNumber generateNumber)
{
try
{
if (NumberValue % 3 == 0)
{
Console.WriteLine("Number Can be Divided by 3");
this.divideByThreeSpotted.OnNext(generateNumber);
}
}
catch (Exception exception)
{
this.divideByThreeSpotted.OnError(exception);
}
}
}
See Number:
public class SeeNumber : IDisposable
{
private IDisposable divideByThreeSpottedSubscription;
public SeeNumber(GenerateNumber generateNumber)
{
this.divideByThreeSpottedSubscription = generateNumber.DivideByThreeSpotted.Subscribe(this.OnDivideByThreeSpotted);
}
public void Dispose()
{
this.divideByThreeSpottedSubscription.Dispose();
}
public void OnDivideByThreeSpotted(GenerateNumber generateNumber)
{
GenerateNumber spottedDivisibleByThree = generateNumber;
}
}
Creating a simple example from tutorial,

Loop through a list of integers - only returning 1 number

Trying to simply loop through a list of ints, and printing each number via returnNumbers() however it only loops through the list one, returning the first element. Can anyone detect the minor mistake I'm making?
class Program
{
static void Main(string[] args)
{
ListHolder List = new ListHolder();
List.addNumber(6);
List.addNumber(3);
List.addNumber(2);
List.returnNumbers();
}
}
class ListHolder
{
List<int> numbers = new List<int>();
public void addNumber(int val)
{
numbers.Add(val);
}
public void returnNumbers()
{
foreach (int n in numbers)
{
Console.WriteLine(n);
Console.ReadLine();
}
}
}
Move the Console.Readline() out of your loop.
It's only displaying once because the program is waiting for your input.
The way you've written it, you'd have to input something (let's say just hitting the Enter key) to see each output.
using System.IO;
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
ListHolder List = new ListHolder();
List.addNumber(6);
List.addNumber(3);
List.addNumber(2);
List.returnNumbers();
Console.ReadLine();
}
}
class ListHolder
{
List<int> numbers = new List<int>();
public void addNumber(int val)
{
numbers.Add(val);
}
public void returnNumbers()
{
foreach (int n in numbers)
{
Console.WriteLine(n);
//Console.ReadLine();
}
}
}

Can't Call arrays in main method

I'm practicing methods, but the problem is I want to separate inputting and sorting, the display method will be the main, I'm having trouble fixing this calling from other class.
This is my script :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaseProblem
{
class Method
{
static void MethodInput()
{
int[] array = new int[5];
int i;
// loop for accepting values in array
for (i = 0; i < 5; i++)
{
Console.Write("Enter number:\t");
array[i] = int.Parse(Console.ReadLine());
}
}
public static void MethodSort()
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
}
class Program
{
static void Main(int[]array)
{
//sorting array value;
Array.Sort(array); //use array's sort function
Method.MethodSort(array);
Console.ReadLine();
}
}
}
Thank you for your help
First of all Main() ( or "EntryPoint" as we should call it ) cannot have int[] as an input parameter but string[] instead you should learn basics of programming before starting to actually code something.
Second thing :
I want to separate inputting and sorting
You can create an object called Input
public class Input
{
public static void Write(string message)
{
Console.WriteLine(message);
}
public static int? ReadInt(string reason)
{
Write(reason);
string userInput = Console.ReadLine();
int parsed = 0;
if(int.TryParse(userInput, out parsed))
return (int?)parsed;
return null;
}
}
This will be your "InputLogic" which you can use as Input.ReadInt("Please specify your age: ");
Next you can make an Operations object :
public class Operations
{
public void Display(int[] arr)
{
foreach(int i in arr)
{
// and since you have "Input" class that can display things
Input.Write(i.ToString());
}
}
public void Sort(ref int[] arr)
{
Array.Sort(arr);
}
}
Now the last thing is to combine it within your Program
class Program
{
static void Main(string[] args)
{
int[] arr = new int[5];
for(int i = 0; i < arr.Length; i++)
{
int? input = null;
while( !( input = Input.ReadInt("Give me number") ).HasValue ) { }
arr[i] = input.Value;
}
Operations op = new Operations();
op.Display(arr);
op.Sort(ref arr);
op.Display(arr);
}
}
Well, you are missing the parameter for the called function.
public static void MethodSort(int[] array)
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
note: not tested
Okay, you have two errors, both in this method:
public static void MethodSort()
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}
as well as how you call it:
Method.MethodSort(array);
The first problem is that the method uses the variable array, which doesn't exist in that method's scope.
The second problem is that you are passing array to the call to Method.MethodSort, but that method isn't configured to take a parameter.
There are two ways to approach solving this: remove array entirely, or change the method to accept it. Now, you obviously cannot remove array, as the whole point is to do stuff with it. Therefore, the logical solution is to add array as a parameter to your method:
public static void MethodSort(int[] array)
{
foreach (int i in array)
{
Console.Write(" {0}", i);
}
}

Can I bypass Console.ReadKey() issue when input is redirected?

I am a C# teacher and I wrote some automated HW checker for my students.
The students write C# Console Applications. My HW checker is based on input redirection so I can test their code on my own generated input.
The problem is that students sometimes end their program with a Console.ReadKey() instruction (They do so just to make the execution window not close when they ran the program under F5 - Debug). The Console.ReadKey() crashes when ran under input redirection with the following exception:
System.InvalidOperationException: Cannot read keys when either application does not have a console or when console input has been redirected from a file.
Do I have any way to "bypass" this problem (without altering the students code)? Maybe tell Console to ignore ReadKey instructions?
I see a clear case for a Dependency Injection pattern.
Let's build a simple example, with Read, ReadLine and WriteLine functionalities polymorphically: your students must write a homework in which a number given in the Console.ReadLine() must be parsed as int and returned to the Console Window.
Usually a student writes something like:
class Program
{
static void Main(string[] args)
{
var stringValue = Console.ReadLine();
int number;
if (int.TryParse(stringValue, out number))
Console.WriteLine($"The double of {number} is {number * 2}");
else
Console.WriteLine($"Wrong input! '{stringValue}' is not an integer!");
Console.Read();
}
}
Now, instead, create an interface for the Console functionalities:
public interface IOutput
{
void Read();
string ReadLine();
void WriteLine(string text);
}
A student must create a Homework class that wraps all the required homework code, using an IOutput instance in this way:
public class HomeWork
{
private IOutput _output;
public HomeWork(IOutput output)
{
_output = output;
}
public void Run()
{
_output.WriteLine("Give me an integer:");
var stringValue = _output.ReadLine();
int number;
if (int.TryParse(stringValue, out number))
_output.WriteLine($"The double of {number} is {number * 2}");
else
_output.WriteLine($"Wrong input! '{stringValue}' is not an integer!");
_output.Read();
}
}
The Main becomes:
static void Main(string[] args)
{
var h = new HomeWork(new ConsoleOutput());
h.Run();
}
You give them also the ConsoleOutput class:
public class ConsoleOutput : IOutput
{
public void Read()
{
Console.Read();
}
public string ReadLine()
{
return Console.ReadLine();
}
public void WriteLine(string text)
{
Console.WriteLine(text);
}
}
So the use it instead of call directly Console.Read() etc.
The student must pass to you not the entire Application, but only the Homework class.
You can create a test class that use the Homework class with some test implementations of IOutput like the followings:
public abstract class TestOutput : IOutput
{
public TestOutput()
{
Outputs = new List<string>();
}
public void Read()
{
//do nothing?
}
public abstract string ReadLine();
public void WriteLine(string text)
{
Outputs.Add(text);
}
public List<string> Outputs { get; set; }
}
public class TestOutputWithAValidNumber : TestOutput
{
public TestOutputWithAValidNumber(int value)
{
Value = value;
}
public override string ReadLine()
{
return Value.ToString();
}
public int Value { get; }
}
public class TestOutputWithNotValidNumber : TestOutput
{
public TestOutputWithNotValidNumber(string value)
{
Value = value;
}
public override string ReadLine()
{
return Value;
}
public string Value { get; }
}
The test class can be something like this:
[TestClass]
public class TestOutputClass
{
[TestMethod]
public void TestGoodNumber()
{
var testOutput = new TestOutputWithAValidNumber(1234);
var h = new HomeWork(testOutput);
h.Run();
Assert.AreEqual(1234, testOutput.Value);
Assert.AreEqual("Give me an integer:", testOutput.Outputs[0]);
Assert.AreEqual("The double of 1234 is 2468", testOutput.Outputs[1]);
}
[TestMethod]
public void TestWrongNumber()
{
var testOutput = new TestOutputWithNotValidNumber("foo");
var h = new HomeWork(testOutput);
h.Run();
Assert.AreEqual("foo", testOutput.Value);
Assert.AreEqual("Give me an integer:", testOutput.Outputs[0]);
Assert.AreEqual("Wrong input! 'foo' is not an integer!", testOutput.Outputs[1]);
}
}
If you need only to wrap the Console.Read() method, feel free to simplify all this code, but IMHO I thought that a wider view on this possible solution would have been useful anyway.
If the executables are in IL, you can create an easy application that uses ILDASM.
The key point is: disassemble the executable with ILDASM into a text file/stream, look for any call to Console.Read and remove it, than recompile it and run.

Applying a method onto all instances of class inside a list

I am picking up C# and as a beginners exercise I tried to code a simple Tekken Tournament roster. The problem is, that I cannot find a way how to apply a method of class Fighter onto the whole list of fighters, as there are no names of the variable, or at least I think that is the problem.
using System;
using System.Collections.Generic;
namespace TekkenConsoleTournament
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Enter nicknames of players, finished by 0: \n");
int numFighter=0;
String nickname;
List <Fighter> allFighters = new List<Fighter>();
while ((nickname = Console.ReadLine()) != "") {
allFighters.Add(new Fighter(nickname));
numFighter ++;
}
Console.WriteLine(allFighters.ForEach(Fighter.getName()));
Console.WriteLine(foreach(Fighter in allFighters) {getName();});
//for (int counter = 0; counter <= fighter; counter++) {
// Fighter[counter.getName();
//}
}
}
}
And the Fighter.cs
using System;
namespace TekkenConsoleTournament
{
public class Fighter
{
private int matches, won, lost, draw;
String name;
public Fighter (String name)
{
this.matches = this.won = this.lost = 0;
this.name = name;
}
public void wonMatch()
{
matches++;won++;
}
public void lostMatch()
{
matches++;lost++;
}
public void drawMatch()
{
matches++;draw++;
}
public int[] getStats()
{
int[] stats = {this.matches,this.won,this.lost,this.draw};
return stats;
}
public String getName()
{
return this.name;
}
}
}
At first, I d like to print the names of all fighters, then maybe the stats and then as an exercise try making the code remember who fought who and put them in two dimensional matrix based on the result of the fight.
Use a foreach loop ?
foreach(var fighter in allFighters)
{
// do something with fighter
var name = fighter.getName();
Console.WriteLine(name);
}
I assume there is a getName method, if there is not use your field or property name to get current fighter's name.
Your mistake is you are putting ForEach loops inside of Console.WriteLine method.You should do the opposite.And this syntax is wrong:
foreach(Fighter in allFighters)
It should be like this:
foreach(Fighter fighter in allFighters)
Or you can use var as shown in the above code.
You're close, but not quite. Try these options:
Console.WriteLine(string.Join(", ", allFighters.Select(f => f.getName())));
allFighters.ForEach(fighter => Console.Writeline(fighter.getName()));
foreach (Fighter f in allFighters)
{
Console.WriteLine(f.getName());
}

Categories

Resources