How would you solve that question? Background: beginner in C# who has only studied for a couple of days.
int[] nums= new int[10];
for (int i = 0; i < nums.Length; i )
{
{
Console.WriteLine("Enter number:");
string strnum = Console.ReadLine();
nums[i] = Convert.ToInt32(strnum);
}
}
sum(nums);
Biggest(nums);
...
Got stuck here while doing the above question
CodePudding user response:
Add the following functions to your program and call it a day (if you wanna be cheeky):
static void Sum(int[] nums) => Console.WriteLine($"Sum: {nums.Sum()}");
static void Biggest(int[] nums) => Console.WriteLine($"Biggest: {nums.Max()}");
CodePudding user response:
This should work:
int[] nums= new int[10];
int? maxNum = null;
for (int i = 0; i < nums.Length; i )
{
{
Console.WriteLine("Enter number:");
string strnum = Console.ReadLine();
nums[i] = Convert.ToInt32(strnum);
maxNum = (maxNum == null || maxNum < nums[i]) ? nums[i] : maxNum;
}
}
You can read the new line as: if maxNum is null (it's first empty state) or if maxNum is minor than the last number, then maxNum is the last number. Else, maxNum is still maxNum.
This is just one way to do it. Good luck!