Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_1
	{
	class Program
		{
		static void Main(string[] args)
			{//start main
			
			int number = 0;
			int min = 0;
			int max = 0;
			
			//start calling modules
			calculateMinMax(ref min, ref max, ref number);
			getInput(ref number);
		  display (min, max, number);
			//end calling modules
			 
			
			Console.ReadKey(); //keep program running
			}//end main	
				
	
	//start CalculateMinMax
			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;
				}

			}//end calculateMinMax

	//start getInput
			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.");
					}
				}//end getInput
			
			
	//start display
			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);
			}//end display

	

			}//end class
		}//end program



Any help will be appreciated! Thanks! :)
Posted
Updated 25-Feb-13 17:14pm
v4
Comments
Sergey Alexandrovich Kryukov 25-Feb-13 22:02pm    
None of your code seems to make sense. You parse numbers entered by the user but they are lost, you don't store them. What are you trying to do?
—SA
Lakshay Chhabra 25-Feb-13 22:07pm    
I parsed the numbers so that if the user enters anything other than an integer, they'll get an error message.

I'm sorry if this doesn't make any sense. I believe I need two loops- one to ask user to keep entering the numbers and one to get the maximum and the minimum values.


You are pretty far from any working code so far.

First of all, your code getting strings from the user and parsing as integer shows data in the console and looses it. Why did you added uses System.Collections.Generic;? Create an instance of System.Collections.Generic.List<int> and add your data there, so you could have where to find your maximum and minimum.

You ref parameters make no sense. Read about ref and out parameters, passing parameters by value and by reference, and learn how each case works.

Now, a bit more "tricky" part. Your initialization of maximum and minimum to 0 makes no sense at all. Instead, initialize minimum to int.MaximumValue, maximum — to int.MinimumValue and update its value in the loop.

Now, this is pretty much all you need. Rewrite the code using these ideas.

—SA
 
Share this answer
 
Comments
Lakshay Chhabra 25-Feb-13 23:16pm    
Thanks for the suggestions. I attached the updated code in the post above.

I didn't add the "uses System.Collections.Generic;" statement. That was there when I started the program. I'm using Visual Studio 2010.
Sergey Alexandrovich Kryukov 25-Feb-13 23:21pm    
"using", not "uses". This is not a statement.
—SA
Sergey Alexandrovich Kryukov 25-Feb-13 23:22pm    
OK, calculateMinMax make no sense. You did not fix your problem with 0. You also don't need to compare with 0. 0 is no special. Try again. What's the problem? I think I explained you all you need.
—SA
Sergey Alexandrovich Kryukov 25-Feb-13 23:26pm    
Also, you are not working with a list. Are you paying attention for the answer or not. If you don't understand something, please ask a question. The problem is very simple, you are know how to write methods, but you cannot get something really basic. Maybe you don't understand how data is stored in objects, parameter passing, classes vs. instances, or something as basic?
—SA
Lakshay Chhabra 27-Feb-13 18:57pm    
Thanks Sergey. I know it's "using" and not "uses". I just copied what you posted in the original comment.

I tried it with your suggestions and it still doesn't work. I initialized the min and the max variables like you mentioned above but I still get the answer as 0 on both min and max.
I have the basic idea and some understanding of how this works but my code isn't working. What do you think is wrong with the calculateMinMax method?

thanks again!
First Store the Numbers in an array.

Then set the first number as Maximum and as minimum.

Then use any looping technique to loop through the array and if the number in the array is greater than Maximum, assign it to maximum and if it is less than minimum then assign it to minimum.

Try it out.

:)
 
Share this answer
 
Comments
Lakshay Chhabra 25-Feb-13 22:33pm    
Hi! Thanks for the help! The arrays haven't been covered yet. I'm only on chapter 5 and that's about loops. The arrays come later in the book.
I know I can use them but I want to learn how to do this without the arrays.
:)
Sergey Alexandrovich Kryukov 25-Feb-13 23:23pm    
Array is not possible. Should be a list.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900