views:

78

answers:

6

For some reason, I can't understand the logic of a program that:

  1. Takes in a list of numbers
  2. Goes through them (via a for each loop) to find the least number
  3. displays the least number via writeline

If leastNumber must be initialized to 0, then won't the leastNumber ALWAYS be 0? (I've provided a basic txt file containing a list of various integers, that's what TwoNumbers.txt is)


string[] lines = File.ReadAllLines ( "TwoNumbers.txt" );

 int leastNumber = 999;
 int previousNumber = 0;

 foreach (string line in lines)
 {
   int currentNumber = int.Parse ( line );
   currentNumber = leastNumber;
   if (currentNumber < previousNumber)
   {
     leastNumber = currentNumber;
   }
 }

 Console.WriteLine ("The least number is: " + leastNumber);
 Console.ReadLine ();

+2  A: 

Try this:

string[] lines = File.ReadAllLines("TwoNumbers.txt");

int leastNumber = int.MaxValue;

foreach (string line in lines)
{
    int currentNumber = int.Parse(line);

    if (currentNumber < leastNumber)
    {
        leastNumber = currentNumber;
    }
}

Console.WriteLine("The least number is: " + leastNumber);
Console.ReadLine();
Pieter
Thank you Pieter. My tired old brain saw that problem a few minutes before I checked back.
DonG
+2  A: 

In your program, you're overriding the current number:

int currentNumber = int.Parse ( line );
currentNumber = leastNumber;

The second line will cause the current number to be 999 in the first iteration, which will then cause leastNumber to get set to 0 after your check.

After that point, currentNumber will always get set to 0, and remain that way.

You don't really need previous number at all. Just do:

string[] lines = File.ReadAllLines ( "TwoNumbers.txt" );

int leastNumber = int.MaxValue;

foreach (string line in lines)
 {
   int currentNumber = int.Parse ( line );
   if (currentNumber < leastNumber)
   {
     leastNumber = currentNumber;
   }
 }

 Console.WriteLine ("The least number is: " + leastNumber);
 Console.ReadLine ();
Reed Copsey
Thanks, Reed. This is exactly the solution my tired brain showed me just now.
DonG
A: 

Your problem is in the comparison.

You're comparing currentNumber < previousNumber but previousNumber is always equal to 0 because you haven't assigned anything to it.

Gio
A: 

There are NEGATIVE integers, which are less than 0. Is anything stopping negative integers from being in your text file?

Steve Danner
Excellent Point. No, there are only positive integers in my list. Thanks!
DonG
A: 

Bah. I see what I did.

string[] lines = File.ReadAllLines ( "TwoNumbers.txt" );

        int leastNumber = 0;
        int previousNumber = 0;

        foreach (string line in lines)
            {
            int currentNumber = int.Parse ( line );
            **currentNumber = leastNumber;**

            if (leastNumber == 0 || currentNumber < leastNumber)
                {
                leastNumber = currentNumber;
                }
            previousNumber = currentNumber;
            }
        Console.WriteLine ("The least number is: " + leastNumber);
        Console.ReadLine ();

There's no need for the assignment currentNumber = leastNumber; Thanks everyone. I'm going to go over the 6 comments so far and extract nuggets of knowledgey goodness. I'm not thinking so good today. Noob signing off.

DonG
A: 

If you assume that the list of strings ints are only because of your testing method (using a text file to hold your test data) and distill this down to the problem at hand which seems to be finding the lowest integer value in a set, you could use LINQ to do that for you:

int[] listOfPositiveInts = new int[]{2,3,4,5,1,67};
var minInt = listOfPositiveInts.Min();
Console.WriteLine(minInt);

If your business problem is to convert a text file full of strings representing numbers, you should beef up your solution a bit more.

If you want to understand the algorithm, then you should try and think in pseudo code

create variable to track current lowest number 
initialize tracking Variable to the first number in my array
loop through the array of numbers to inspect
if the number I'm inspecting is lower than my current lowest number
    replace my current lowest number with the number I'm inspecting
end loop
Display lowest value found

Which looks like this when using a foreach loop

// test data
int[] arrayOfNumbers= new int[]{2,3,4,5,1,67};

// search algorithm
int lowestValueFound = arrayOfNumbers[0];
foreach(int i in arrayOfNumbers)
{
     if (i < lowestValueFound)
         lowestValueFound = i;
}
Console.WriteLine("Lowest value found is {0}", lowestValueFound);

The string conversion in there may be just noise if you are trying to understand the algorithm. If you are just trying to get it done, try using the array of ints and using LINQ instead.

Dave White