Getting the smallest value array

can someone check my code and see why it cant seem to capture the smal

Page 1 of 1

12 Replies - 1210 Views - Last Post: 08 October 2008 - 12:24 PM Rate Topic: -----

#1 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Getting the smallest value array

Post icon  Posted 08 October 2008 - 08:35 AM

			//variable declaration//
			int[] num;
			int size,largest = 0,temp=0;
			bool duplicate;
			



			//input size//
			Console.Write("Input Size: ");

			size = int.Parse(Console.ReadLine());
			num = new int[size];
			
			

			for (int i = 0; i < size; i++)
			{
				
				do
				{
					duplicate = false;
					Console.Write("Input a number[" + i + "]: ");
					num[i] = int.Parse(Console.ReadLine());
					int smallest = num[0];
					
					//duplicate//
					for (int n = 0; n < i; n++)
					{
						if (num[i] == num[n])
						{
							duplicate = true;
							Console.WriteLine("It Is A Duplicate");
						}
					}
					//largest and smallest//
					if (num[i] > largest)
					{
						largest = num[i];

					}   
					else if (num[i] < smallest)
						{
							smallest = num[i];
						}
						

					
				} while (duplicate == true);

				
			}
			Console.WriteLine("Largest is: " + largest);
			Console.WriteLine("Smallest is: " + temp);




can someone look at this code...
and see what the problem is why i cant seem to get the smallest value...
the getting the duplicates and getting the largest is already running...
thanks in advance...

Is This A Good Question/Topic? 0
  • +

Replies To: Getting the smallest value array

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: Getting the smallest value array

Posted 08 October 2008 - 09:09 AM

Yeah.. pull the declaration of the smallest out of the for loops

					int smallest = num[0];


Was This Post Helpful? 0
  • +
  • -

#3 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Re: Getting the smallest value array

Posted 08 October 2008 - 09:22 AM

					int smallest = num[0];



if i remove it the loop the resulting value for the smallest will be zero...
my problem with my code for the smallest value is like this:
Input Number[0]:1
Input Number[1]:2
Input Number[2]:3
Input Number[3]:4
Input Number[4]:5

Output:

Smallest is: 1

BUT!!!

if the input is like this:
Input Number[0]:2
Input Number[1]:1
Input Number[2]:3
Input Number[3]:4
Input Number[4]:5

Output will be:
Smallest is: 5
Was This Post Helpful? 0
  • +
  • -

#4 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Re: Getting the smallest value array

Posted 08 October 2008 - 09:58 AM

			//variable declaration//
			int[] num;
			int size,largest = 0,temp=0,smallest;
			bool duplicate;
			
			



			//input size//
			Console.Write("Input Size: ");

			size = int.Parse(Console.ReadLine());
			num = new int[size];
			smallest = num[0];
			
			
			

			for (int i = 0; i < size; i++)
			{
				
				do
				{
					duplicate = false;
					Console.Write("Input a number[" + i + "]: ");
					num[i] = int.Parse(Console.ReadLine());
					

					
					//duplicate//
					for (int n = 0; n < i; n++)
					{
						if (num[i] == num[n])
						{
							duplicate = true;
							Console.WriteLine("It Is A Duplicate");
						}
					}
					//largest and smallest//
					if (num[i] > largest)
					{
						largest = num[i];

					}   
					else if (num[i] < smallest)
						{
							smallest = num[i];
							
						}

					temp = smallest;
					
				} while (duplicate == true);

				
			}
			Console.WriteLine("Largest is: " + largest);
			Console.WriteLine("Smallest is: "+temp );
			
			
			













			Console.ReadKey();



I removed the int smallest = n[0];
but somehow the output for the smallest value turns out to be zero no matter what the input is...
can someone know whats the problem with my code for smallest value...
if you can improvise my code that would be really nice...
my code traps the duplicate get the largest and the size of the array depends on the size input... thanks...
Was This Post Helpful? 0
  • +
  • -

#5 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,120
  • Joined: 22-February 08

Re: Getting the smallest value array

Posted 08 October 2008 - 10:17 AM

I think it's because the value of 'i' will never be incremented in your code. You have for loops in there, but it is never utilized in searching for the largest and smallest value. So, instead of searching the largest and smallest value from the array OUTSIDE of the for loops, like you did here :
for (int n = 0; n < i; n++)
{
	if (num[i] == num[n])
	{
		duplicate = true;
		Console.WriteLine("It Is A Duplicate");
	}
}
//largest and smallest//
if (num[i] > largest)
	largest = num[i];
else if (num[i] < smallest)
	smallest = num[i];



Put it INSIDE the for loops, so the value of 'i' can be incremented properly. Like this :
for (int n = 0; n < i; n++)
{
	if (num[i] == num[n])
	{
		duplicate = true;
		Console.WriteLine("It Is A Duplicate");
	}
	//largest and smallest//
	if (num[i] > largest)
		largest = num[i];
	else if (num[i] < smallest)
		smallest = num[i];
}


Hope that helps you. :)
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Getting the smallest value array

Posted 08 October 2008 - 10:27 AM

try not having the "else". technically, your largest number could also be your smallest number.

they should be compared separately.
Was This Post Helpful? 0
  • +
  • -

#7 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Re: Getting the smallest value array

Posted 08 October 2008 - 10:56 AM

					//duplicate//
					for (int n = 0; n < i; n++)
					{
						if (num[i] == num[n])
						{
							duplicate = true;
							Console.WriteLine("It Is A Duplicate");
						}
					}


this loop is for trapping the duplicate....

			for (int i = 0; i < size; i++)
			{
				
				do
				{
					duplicate = false;
					Console.Write("Input a number[" + i + "]: ");
					num[i] = int.Parse(Console.ReadLine());
					smallest = num[0];
//largest and smallest//
					if (num[i] > largest)
					{
						largest = num[i];

					}   
					else if (num[i] < smallest)
						{
							smallest = num[i];
							
							
						}
					temp = smallest;
					
				} while (duplicate == true);



while this one is for the array and for the smallest and largest....
Was This Post Helpful? 0
  • +
  • -

#8 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Re: Getting the smallest value array

Posted 08 October 2008 - 11:10 AM

			for (int i = 0; i < size; i++)
			{

				
					Console.Write("Input a number[" + i + "]: ");
					num[i] = int.Parse(Console.ReadLine());
					int smallest = num[0];

					//largest and smallest//
					if (num[i] > largest)
					{
						largest = num[i];

					}   
					if(num[i]<smallest)
						{
							smallest = num[i];
							
							
						}
					temp = smallest;
	
			}
			Console.WriteLine("Largest is: " + largest);
			Console.WriteLine("Smallest is: "+temp );



i removed first the trapping for duplicate loop for the snallest and largest loop code will be clea... above is the code for the smallest and largest....
can someone improvise this so that it can determine the smallest number...
largest is already running for this code...
thanks....
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: Getting the smallest value array

Posted 08 October 2008 - 11:26 AM

what the guy said below.. but here's more condensed code.
       
            int size, largest = 0;
            int input = 0;
            int smallest = 0;
            //input size//
            Console.Write("Input Size: ");
            size = int.Parse(Console.ReadLine());

            for (int i = 0; i < size; i++)
            {
                Console.Write("Input a number[" + i + "]: ");
                input = int.Parse(Console.ReadLine());

                if (i == 0) { smallest = input; }

                //largest and smallest//
                if (input > largest)
                {
                    largest = input;
                }
                if (input < smallest)
                {
                    smallest = input;
                }             

            }


            Console.WriteLine("Largest is: " + largest);
            Console.WriteLine("Smallest is: " + smallest);

This post has been edited by modi123_1: 08 October 2008 - 11:35 AM

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Getting the smallest value array

Posted 08 October 2008 - 11:26 AM

EDITED: made a small correction.
this code works.....I have removed the duplicate code since you have stated that it already works.

        static void Main(string[] args)
        {
            int size = 0;
            int largest = 0;
            int smallest = 0;

            Console.Write("Enter size: ");
            size = int.Parse(Console.ReadLine());

            int[] num = new int[size];

            for(int i = 0; i < size; i++)
            {
                Console.Write("Enter number[" + i + "]: ");
                num[i] = int.Parse(Console.ReadLine());

                for (int j = 0; j <= i; j++)
                {
                    if (i != 0)
                    {
                        if (num[i] <smallest)
                        {
                            smallest = num[i];
                        }
                    }
                    else
                    {
                        smallest = num[i];
                    }
                }

                for (int h = 0; h <= i; h++)
                {
                    if (i != 0)
                    {
                        if (num[i] > largest)
                        {
                            largest = num[i];
                        }
                    }
                    else
                    {
                        largest = num[i];
                    }
                }
            }

            Console.WriteLine("Largest is: " + largest);
            Console.WriteLine("Smallest is: " + smallest);

            Console.ReadLine();
        }



your problem was the line

int smallest = num[0];



you were always overwriting the smallest number with the first number.

also fixed where if the user put in negative numbers, your largest number would show correctly instead of 0.

This post has been edited by eclipsed4utoo: 08 October 2008 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#11 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Re: Getting the smallest value array

Posted 08 October 2008 - 11:55 AM

i tried your code sir but that code still has the same output as my code...
hmmm.....
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9366
  • View blog
  • Posts: 35,187
  • Joined: 12-June 08

Re: Getting the smallest value array

Posted 08 October 2008 - 12:21 PM

View Postsrbjed, on 8 Oct, 2008 - 01:55 PM, said:

i tried your code sir but that code still has the same output as my code...
hmmm.....



which person's code? mine works!
Was This Post Helpful? 0
  • +
  • -

#13 srbjed  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 07-October 08

Re: Getting the smallest value array

Posted 08 October 2008 - 12:24 PM

		static void Main(string[] args)
		{
			int size;
			int largest = 0;
			int smallest = 0;
			bool duplicate;

			Console.Write("Enter size: ");
			size = int.Parse(Console.ReadLine());

			int[] num = new int[size];

			for (int i = 0; i < size; i++)
			{
				do
				{
					duplicate = false;
					Console.Write("Enter number[" + i + "]: ");
					num[i] = int.Parse(Console.ReadLine());
					//duplicate//
					for (int n = 0; n < i; n++)
					{
						if (num[i] == num[n])
						{
							duplicate = true;
							Console.WriteLine("It Is A Duplicate");
						}
					}
					//smallest//
					for (int j = 0; j <= i; j++)
					{
						if (i != 0)
						{
							if (num[i] < smallest)
							{
								smallest = num[i];
							}
						}
						else
						{
							smallest = num[i];
						}
					}
					//largest//

					for (int h = 0; h <= i; h++)
					{
						if (i != 0)
						{
							if (num[i] > largest)
							{
								largest = num[i];
							}
						}
						else
						{
							largest = num[i];
						}
					}
				} while (duplicate == true);
			}

			Console.WriteLine("Largest is: " + largest);
			Console.WriteLine("Smallest is: " + smallest);
			Console.ReadKey();  



This is the code for trapping duplicate with finding the smallest and largest value...
Thanks for the help eclipse!!!

		static void Main(string[] args)
		{
			int size;
			int largest = 0;
			int smallest = 0;
			bool duplicate;

			Console.Write("Enter size: ");
			size = int.Parse(Console.ReadLine());

			int[] num = new int[size];

			for (int i = 0; i < size; i++)
			{
				do
				{
					duplicate = false;
					Console.Write("Enter number[" + i + "]: ");
					num[i] = int.Parse(Console.ReadLine());
					//duplicate//
					for (int n = 0; n < i; n++)
					{
						if (num[i] == num[n])
						{
							duplicate = true;
							Console.WriteLine("It Is A Duplicate");
						}
					}
					//smallest//
					for (int j = 0; j <= i; j++)
					{
						if (i != 0)
						{
							if (num[i] < smallest)
							{
								smallest = num[i];
							}
						}
						else
						{
							smallest = num[i];
						}
					}
					//largest//

					for (int h = 0; h <= i; h++)
					{
						if (i != 0)
						{
							if (num[i] > largest)
							{
								largest = num[i];
							}
						}
						else
						{
							largest = num[i];
						}
					}
				} while (duplicate == true);
			}

			Console.WriteLine("Largest is: " + largest);
			Console.WriteLine("Smallest is: " + smallest);
			Console.ReadKey();  



This is the code for trapping duplicate with finding the smallest and largest value...
Thanks for the help eclipse and modi.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1