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);
}
}
Related
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();
}
}
}
In this code I made two output functions and one main.
When I call output function in main the program give the error.
using System;
public class InitArray
{
public static void Main()
{
int[,] rectangular = { { 1, 3, 4 }, { 5, 2, 4 } };
int[][] jagged = { new int[] { 2, 3, 4 }, new int[] { 3, 4, 5 }};
}
public void OutputArray(int [,]array)
{
for(int row=0;row<array.GetLength(0);row++)
{
for (int column = 0; column < array.GetLength(1); column++)
Console.Write("{0} ", array[row, column]);
Console.WriteLine();
}
}
public void OutputArray(int [][]array)
{
foreach(var row in array)
{
foreach (var element in row)
Console.Write("{0} ", element);
Console.WriteLine();
}
}
}
This is main function and have two arrays one is jagged and other is rectangular type.
The defined function is without static keyword and I could not access in main function.
This second output function is also a non static function and this is also not access in main.
Anyone can tell me the the reason?
Non-static methods want instance; that's why either mark the method as static:
public static void OutputArray(int[][] array) {
...
}
public static void Main() {
...
OutputArray(...);
...
}
or create and provide the instance:
public void OutputArray(int[][] array) {
...
}
public static void Main() {
...
var instance = new InitArray();
instance.OutputArray(...);
...
}
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.
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());
}
Im trying to learn the 4 types of loops, for, foreach, while and do. I've made this code so far:
Loops.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoopFrame
{
class Loops
{
// attribute (not property)
private List<string> names;
// constructor
public Loops()
{
// initilize
names = new List<string>();
//insert values
names.Add("Peter");
names.Add("Susanne");
names.Add("Steen");
names.Add("Mohammed");
names.Add("Poul");
names.Add("Ebbe");
names.Add("Henrik");
names.Add("Per");
names.Add("Anders");
names.Add("Lars");
names.Add("Vibeke");
names.Add("Mogens");
names.Add("Michael");
}
//
// 4 loop methods
//
// all should print out the whole list 'names'
//
public void WhileLoop()
{
int x = 0;
while (x < names.Count)
{
Console.WriteLine(names[++x]);
}
}
public void DoWhileLoop()
{
int x = 0;
do
{
Console.WriteLine(names[++x]);
x++;
} while (x < names.Count);
}
public void ForLoop()
{
for (int x = 0; x < 0; x++)
{
Console.WriteLine(names[++x]);
}
}
public void ForeachLoop()
{
int[] names = new int[] { 0 };
foreach (int element in names)
{
System.Console.WriteLine(element);
}
System.Console.WriteLine();
}
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LoopFrame
{
class Program
{
static void Main(string[] args)
{
Loops loops = new Loops();
Console.WriteLine();
Console.ReadLine();
}
}
}
I think there is some faults, but the program can currently compile, but it just makes a black screen.
Thank you for your time.
As it stands, you're constructing the Loops instance, but you've not called any methods.
static void Main(string[] args)
{
Loops loops = new Loops();
loops.WhileLoop();
loops.DoWhileLoop();
loops.ForLoop();
loops.ForeachLoop();
Console.WriteLine();
Console.ReadLine();
}
You may want to read up on Methods.
As an aside, you can populate your list with much less code:
names = new List<string> { "Peter", "Suzanne", "Steen" ... };
As another aside, in two of your loop methods, you're incrementing the index variable twice per iteration, which should only print out every other element.
You are not calling any method that should print the list content.
Add:
static void Main(string[] args)
{
Loops loops = new Loops();
//Call methods
loops.WhileLoop();
loops.DoWhileLoop();
//...
Console.WriteLine();
Console.ReadLine();
}
Your foreach will not work. As you declared a new names as int[]. To make your foreach workable with declared list. You should use
foreach(string s in names)
{
Console.WriteLine("Name is : "+s);
}