# Getting the smallest value array

Page 1 of 1

## 12 Replies - 1340 Views - Last Post: 08 October 2008 - 12:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=66768&amp;s=0cbb2f85c2499d90956752b45e95b567&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 srbjed

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

# Getting the smallest value array

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: ");

num = new int[size];

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

do
{
duplicate = false;
Console.Write("Input a number[" + i + "]: ");
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...

Is This A Good Question/Topic? 0

## Replies To: Getting the smallest value array

### #2 modi123_1

• Suitor #2

Reputation: 10674
• Posts: 41,553
• 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];

```

### #3 srbjed

Reputation: 2
• 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

### #4 srbjed

Reputation: 2
• 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: ");

num = new int[size];
smallest = num[0];

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

do
{
duplicate = false;
Console.Write("Input a number[" + i + "]: ");

//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 );

```

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...

### #5 red_4900

• Code T(h)inkers

Reputation: 21
• 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.

### #6 eclipsed4utoo

Reputation: 1532
• Posts: 5,969
• 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.

### #7 srbjed

Reputation: 2
• 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 + "]: ");
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....

### #8 srbjed

Reputation: 2
• 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 + "]: ");
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....

### #9 modi123_1

• Suitor #2

Reputation: 10674
• Posts: 41,553
• 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: ");

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

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

### #10 eclipsed4utoo

Reputation: 1532
• Posts: 5,969
• Joined: 21-March 08

## Re: Getting the smallest value array

Posted 08 October 2008 - 11:26 AM

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: ");

int[] num = new int[size];

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

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);

}

```

```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

### #11 srbjed

Reputation: 2
• 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.....

### #12 modi123_1

• Suitor #2

Reputation: 10674
• Posts: 41,553
• Joined: 12-June 08

## Re: Getting the smallest value array

Posted 08 October 2008 - 12:21 PM

srbjed, 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!

### #13 srbjed

Reputation: 2
• 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: ");

int[] num = new int[size];

for (int i = 0; i < size; i++)
{
do
{
duplicate = false;
Console.Write("Enter number[" + i + "]: ");
//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);

```

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: ");

int[] num = new int[size];

for (int i = 0; i < size; i++)
{
do
{
duplicate = false;
Console.Write("Enter number[" + i + "]: ");
//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);