Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,667 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 3,665 people online right now. Registration is fast and FREE... Join Now!




Getting the smallest value array

2 Pages V  1 2 >  
Reply to this topicStart new topic

Getting the smallest value array, can someone check my code and see why it cant seem to capture the smal

srbjed
post 8 Oct, 2008 - 07:35 AM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

CODE

            //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...
User is offlineProfile CardPM

Go to the top of the page


modi123_1
post 8 Oct, 2008 - 08:09 AM
Post #2


D.I.C Addict

Group Icon
Joined: 12 Jun, 2008
Posts: 508



Thanked 10 times

Dream Kudos: 100
My Contributions


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

CODE

                    int smallest = num[0];
User is offlineProfile CardPM

Go to the top of the page

srbjed
post 8 Oct, 2008 - 08:22 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10


CODE

                    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
User is offlineProfile CardPM

Go to the top of the page

srbjed
post 8 Oct, 2008 - 08:58 AM
Post #4


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

CODE

            //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...
User is offlineProfile CardPM

Go to the top of the page

red_4900
post 8 Oct, 2008 - 09:17 AM
Post #5


D.I.C Addict

****
Joined: 22 Feb, 2008
Posts: 788



Thanked 10 times
My Contributions


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 :
csharp
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 :
csharp
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. smile.gif
User is online!Profile CardPM

Go to the top of the page

eclipsed4utoo
post 8 Oct, 2008 - 09:27 AM
Post #6


D.I.C Regular

***
Joined: 21 Mar, 2008
Posts: 307



Thanked 17 times
My Contributions


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

they should be compared separately.
User is online!Profile CardPM

Go to the top of the page

srbjed
post 8 Oct, 2008 - 09:56 AM
Post #7


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

CODE

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

CODE

            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....
User is offlineProfile CardPM

Go to the top of the page

srbjed
post 8 Oct, 2008 - 10:10 AM
Post #8


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

CODE

            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....
User is offlineProfile CardPM

Go to the top of the page

modi123_1
post 8 Oct, 2008 - 10:26 AM
Post #9


D.I.C Addict

Group Icon
Joined: 12 Jun, 2008
Posts: 508



Thanked 10 times

Dream Kudos: 100
My Contributions


what the guy said below.. but here's more condensed code.
cpp
       
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: 8 Oct, 2008 - 10:35 AM
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 8 Oct, 2008 - 10:26 AM
Post #10


D.I.C Regular

***
Joined: 21 Mar, 2008
Posts: 307



Thanked 17 times
My Contributions


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

csharp

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

csharp

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: 8 Oct, 2008 - 10:41 AM
User is online!Profile CardPM

Go to the top of the page

srbjed
post 8 Oct, 2008 - 10:55 AM
Post #11


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 10

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

Go to the top of the page

modi123_1
post 8 Oct, 2008 - 11:21 AM
Post #12


D.I.C Addict

Group Icon
Joined: 12 Jun, 2008
Posts: 508



Thanked 10 times

Dream Kudos: 100
My Contributions


QUOTE(srbjed @ 8 Oct, 2008 - 01:55 PM) *

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



which person's code? mine works!
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 07:00AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners