Hi! I'm trying to understand loops and I'm using this book. There's a question in it that I can't seem to figure out.
It's asking for a program with a loop that lets the user enter a series of numbers. After all the numbers have been entered, the program should display the largest and the smallest number entered. The user should enter -99 to signal the end of the series.
I can't figure out the main loop that will give me the minimum and the maximum value of the series.
Here's what I have right now:
EDIT 1: I changed the code and now I can get the user to enter numbers till they enter -99. Now I just need the loop that will give me the max and min values of the series.
Hope this is easier to read!
EDIT 2: I changed the code again. I figured out pretty much everything (thanks to all the suggestions!) and have this now. The code is running, but for the answer I get only 0 for some reason, not any number in the series. Is there a reason that this is happening? I think it has something to do with the parameters but I'm not sure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Project_1
{
class Program
{
static void Main(string[] args)
{
int number = 0;
int min = 0;
int max = 0;
calculateMinMax(ref min, ref max, ref number);
getInput(ref number);
display (min, max, number);
Console.ReadKey();
}
static void calculateMinMax ( ref int min, ref int max, ref int number)
{
if (min == 0 && max == 0)
{
min = number;
max = number;
}
if (number < min)
{
min = number;
}
if (number > max)
{
max = number;
}
}
static void getInput(ref int number)
{
while (number != -99)
{
Console.WriteLine("Please enter the numbers in the series. Enter '-99' to end the series.");
while (!int.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Error. Please enter a number.");
}
}
static void display (int min, int max, int number)
{
Console.WriteLine("The largest number in the series you entered is {0}, and the smallest number in the series you entered is {1}.", max, min);
}
}
}
Any help will be appreciated! Thanks! :)