Reputation: 187
The goal is to make a program that when a series of numbers is entered, the minimum and maximum are displayed. -99 will be used as a sentinel.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
double number = 0, maxValue = 0, minValue = 0;
string goOn = "Y";
Console.WriteLine("Please enter a series of numbers, when you wish to stop entering numbers please enter -99.");
Console.WriteLine("The smallest and largest values will then be displayed.");
Console.WriteLine("Remember not to enter -99 unless you want the series to end.");
do
{
while (!double.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Please enter whole numbers only");
while (number != -99)
{
process(ref number, ref minValue, ref maxValue);
while (!double.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Please enter whole numbers only");
}
Console.WriteLine("The smallest value is {0} and the largest value is {1}.", minValue, maxValue);
Console.WriteLine("Do you want to enter another series of numbers?");
Console.WriteLine("If so enter y, if you want to end press any other key");
goOn = Console.ReadLine();
if (goOn.ToUpper() == "Y")
{
Console.WriteLine("Please enter your set of numbers.");
Console.WriteLine("Remember not to enter -99 unless you want the series to end.");
}
} while (goOn.ToUpper() == "Y");
}
static void process(ref double minValue, ref double maxValue, ref double number)
{
if (number > maxValue)
{
number = maxValue;
}
if (number < minValue)
{
number = minValue;
}
}
}
}
I haven't learned arrays or bubbles or classes yet, but here are the issues:
Probably because of the method of getting the min and max values, if I type in say 3, 22, 7, 15, 1 I will get 15 instead of 22 because the value 22 has been thrown out and replaced by 7.
The minimum value isn't being displayed and is instead showing up as 0
when a non-number is entered, and after the error message is displayed, then more numbers are entered, then the minValue shows up the same as the maxValue.
Upvotes: 1
Views: 3097
Reputation: 580
This is a simple mistake :
static void process(ref double minValue, ref double maxValue, ref double number)
{
if (number > maxValue)
{
maxValue = number;
}
if (number < minValue)
{
minValue = number;
}
}
You must affect maxValue and minValue, not number.
Upvotes: 6