12 Replies - 11491 Views - Last Post: 26 November 2009 - 09:29 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Find the smallest number

Posted 25 November 2009 - 12:08 PM

using System;
using System.Collections.Generic;
using System.Text;

namespace ExerciseOne
{
	class Program
	{
		static void Main(string[] args)
		{
			int grade = 0;
			int smallest = 0;
			int counter;
			bool result;

			for (counter = 1; counter <= 5; counter++)
			{
				do
				{
					Console.Write("\nPlease enter the number: ");
					result = int.TryParse(Console.ReadLine(), out grade);

					if (!result)
						Console.Write("\nPlease enter number only.");

				} while (!result);

				if (grade > smallest)
					smallest = grade;
			}
			Console.Write("\nsmallest: {0}", grade);
			Console.ReadLine();
		}
	}
}


Well...This is the basic a very basic program that finds the smallest number and display the average. I am always having a hard time finding the smallest number. Would you guys please, if possible show me a better way to find the smallest number? Though, the program display the correct output and the calculation is correct, but the way I wrote the code is not logical. Instead of assigning the smallest number to the smallest variable, I use grade, which is not a proper way. If you don't understand, you can take a look at my program, and you know what I am talking about.

if (grade > smallest)
					smallest = grade;
			}
			Console.Write("\nsmallest: {0}", grade);
			Console.ReadLine();


This is incorrect.

Is This A Good Question/Topic? 0
  • +

Replies To: Find the smallest number

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Find the smallest number

Posted 25 November 2009 - 12:17 PM

Here's an example of finding the smallest & largest number that may help you resolve your issue :)
Was This Post Helpful? 0
  • +
  • -

#3 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Find the smallest number

Posted 25 November 2009 - 12:24 PM

Well...What if we just use a very basic if statements without using an array? Because since we are just at a beginner level. The instructor doesn't really want use to use or write complicated codes dealing with largest or smallest numbers. So just a basic if else statement will do good.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Find the smallest number

Posted 25 November 2009 - 03:09 PM

Set smallest to Int32.MaxInt at the beginning. Change your if to if (grade < smallest) {smallest = grade;}. After you are done inputing grades, smallest will have the smallest grade.
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Find the smallest number

Posted 25 November 2009 - 03:19 PM

If you would like the theory behind it. When you are checking to see if something is the largest value you need to start with a really low value to compare it against. If you are going to always be using positive numbers 0 is usually a good choice because the values entered will always be greater than or equal to 0. If they were all 0 then the largest number would still be 0. So, when you check to see if a number is greater than the largest value (number > largest) you will always get a value that would be greater than or equal to zero.

For smallest you have to do the opposite. You have to start with a really large value to compare against. Momerath suggested that you use Int32.MaxValue which is of course the largest Int32, or just int, that there is so that would be a great case. If you were expecting numbers within a range, grades between 0 and 100 then the largest value in the range would also work. So when a number is entered and checked against (number < smallest) it will always be less than or equal to that value.
Was This Post Helpful? 0
  • +
  • -

#6 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Find the smallest number

Posted 25 November 2009 - 03:28 PM

Is it sort of like using the .Net frame or class library? You mean something like:

smallest = Int32.MaxInt;

Then

if (grade < smallest) {smallest = grade;}

This is just sort of like part of the challenging exercise...You are not allowing to use other implementations. You are allow to use the loop and the else-if statement and that's why I get stuck.

The codes I posted above is sort of part of the exercise, not the complete codes.

This is a real challenging exercise question.

- The user is prompted to input 12 grades. Check validity is required. If the user enters something which is not a number, he or she is prompted to enter again until the correct number is entered.
- Then the user has to enter the grade between 0 - 100. If the user enters something out of 0 and 100, he or she is prompted to enter again until the correct grade is entered.
- Must get the largest grade and smallest grade, and then drop the largest and smallest grade to calculate the average.

I problem that I have is getting the smallest grade by using the if statement or the loop if possible to be used to find the smallest grade. And the rest shouldn't be the problem. If you do really want my complete codes, I would post that later.

This post has been edited by kenryuakuma: 25 November 2009 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#7 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Find the smallest number

Posted 25 November 2009 - 03:49 PM

For the actual implementation I would leave that part up to you but here is the theory. Basically, to check for the smallest you must make the first initial comparison against a value that you know will always be greater than or equal to the range you are checking for. In your case, since you are doing grades between 0 and 100, 100 would be a good choice to set smallest to initially. For largest you do the reverse, start with the lowest possible so that when you do the comparison you are sure that you will be able to get a value greater than or equal to that number so 0 would be a good choice to start largest at.

You are going to need 12 values so this would be a good case for a for loop because you know exactly how many values you need. When you are doing error checking you should always do that in a while loop or a do while loop. That is more a choice of preference.

Here is the pseudo-code for this:

for I = 1 to 12
{
   do
	  do
		  prompt user for input
	  while input is not numeric
   while input is not between 0 and 100
   check for largest
   check for smallest
   add score to total
}
calculate average
display largest, smallest, average



You will have to be creative with the calculation of the average because you need to remove the smallest and lowest value from the total score to get the correct average.
Was This Post Helpful? 0
  • +
  • -

#8 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Find the smallest number

Posted 25 November 2009 - 04:12 PM

Thanks for the theory advice SixOfEleven, I think I figure out how to do the code. You have really enlightened me on this.

Well...Not sure if that is right by doing this. But I really hope you programmers could at least reply and tell me what you think, so that I know what I did wrong or right so that I could learn. I think I will gain a lot more knowledge by having you guys giving me some comments.

for grade between 0 - 100 and prompting the user for input 12 grades.

This is what I do for the largest number:

int grade = 0;
int largest = 0;
int smallest = 100;  // According to SixOfEleven's theory 
							 // of the smallest. Thanks so much SixOfEleven.

 /////...................Forget about the validity checking requirement here.

for(int i = 1; i <= 12; i++)
{
	 Console.Write("Please enter your grade: ");
	 grade = int.Parse(Console.ReadLine());

	 // Finding the largest
	 if(grade > largest)
			largest = grade;

	 // Finding the smallest
	 if(grade < smallest)
			 smallest = grade;
}

// Getting the average
Console.Write("Average: {0}", (grade - (largest + smallest)) / 10); 



For the smallest grade, since smallest contains the max grade and when the user enters a number, for example, 1, grade will be compared against the largest, which in this case is 0. So grade is larger than largest, and the value is assigned to largest and the process will continue as long as grade is greater than largest and if grade is less than largest, the if(grade > largest) is bypassed.

Then back to where we left off, which is when the user enters 1, and having had it checked within the first if boolean expression, and the value 1 is passed to the second if boolean expression to be checked, since grade is 1, and 1 is less than 100, which is the value previously assigned to smallest. Then the value 1 is assigned to smallest if the condition is true. But in this case, it is. This will continue only if the value of grade is less than the value of the smallest.
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Find the smallest number

Posted 25 November 2009 - 04:21 PM

Check out the pseudo-code in the post above yours. That will give you a bit of help.
Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Find the smallest number

Posted 25 November 2009 - 05:10 PM

Without doubt, the pseudo-code is crystal clear. Thanks a lot for this help. I think, with your pseudo-code, the program could be done in no time.
Was This Post Helpful? 0
  • +
  • -

#11 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Find the smallest number

Posted 26 November 2009 - 04:04 PM

Well...If we don't know the range of the values we need to check, what will we do? Isn't it a good choice to use the Int32.MaxInt at the beginning of the program like Momerath said? It would be easy to set 100 to the smallest since we know what range of value we are comparing, and in this case 0 to 100. But what if we don't, it would be tough.
Was This Post Helpful? 0
  • +
  • -

#12 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Find the smallest number

Posted 26 November 2009 - 06:32 PM

View Postkenryuakuma, on 26 Nov, 2009 - 05:04 PM, said:

Well...If we don't know the range of the values we need to check, what will we do? Isn't it a good choice to use the Int32.MaxInt at the beginning of the program like Momerath said? It would be easy to set 100 to the smallest since we know what range of value we are comparing, and in this case 0 to 100. But what if we don't, it would be tough.


If you never know the maximum in the range it is always best to use the maximum value the the field can hold. All of the types: double, float, int, long, etc. all have an appropriate Max property to them. This would be far easier if you were to use an array though. What you would do then is set the largest and smallest variables to be the first index in the array. Then you would just loop through the array, starting at the second index instead of the first would be a very minor optimization, and compare the values at each index to the largest and smallest variables. Something like this. It assumes that you have an array called values.

int smallest;
int largest;

smallest = values[0];
largest = values[0];

for (int i = 1; i < values.Length; i++)
{
	if (values[i] < smallest)
		smallest = values[i];
	if (values[i] > largest)
		largest = values[i];
}



Much simpler and easier to implement.
Was This Post Helpful? 0
  • +
  • -

#13 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 385
  • Joined: 14-December 08

Re: Find the smallest number

Posted 26 November 2009 - 09:29 PM

Thanks! I would check out the array section about largest and smallest and no wonder why when I browsed through the internet, I saw a bunch of results after the search, all of which are related to array that helps find the largest and smallest.

This post has been edited by kenryuakuma: 26 November 2009 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1