C# Help

I hate doing this here but I am stuck.
Need help with program.

This is the part I am stuck on:
The Program should sort the numbers from the input file and place the sorted number is another file that has the prefix “sorted_” and the extension .txt that should be written in the same directory as the input file is in. Your code should only use file manipulation methods to sort the numbers. You can declare an many temporary files as you need in the process. No other method is allowed (NO Array usage,  NO API). Do not declare a variable for each generated number.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
homeworks? we are not doing them for you!
jyoung127Author Commented:
I need help not asking for whole thing. Where is my code wrong?
Fernando SotoRetiredCommented:
Hi jyoung127;

In the code you posted what problem you are trying to fix? What is not working the way you expected?

I had a quick look at the code you posted and it seems to have some methods that seem to do the same thing. For example all the tempX methods. The only difference between them is that they use different input/output files. You could refactor them to one method and pass in the name of the file to use for each run of the method. For example as the following.

temp("temp.txt", "temp1.txt");            
temp2("temp1.txt", "temp2.txt");
temp3("temp2.txt", "temp3.txt");
temp4("temp3.txt", "temp4.txt");
temp5("temp4.txt", "temp5.txt");
temp6("temp5.txt", "temp6.txt");

Of course you will need to change the signature of the method.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

jyoung127Author Commented:
The program reads random numbers then puts them into a file. Then it reads that file and pulls out the smallest number. It is suppose to put all the other numbers into a temp file(Sum Method).
Then Each temp method is suppose to read a temp file and pull the smallest number into the sorted file.

As it goes through the process I should get a sorted text file.

When the temp methods run the files that are generated don't have the right numbers in them. Its like my code is dropping numbers and I can't figure out where it is doing it at.

Maybe the main root of my problem is I am trying to pull the smallest number out incorrectly.

Thanks For you help
Fernando SotoRetiredCommented:
Hi jyoung127;

The code in the Form_Load seems fine and should have no issues. But will need to be modified if you will use the code outlined below. Replace these lines


With this one line


Your Form_Load and the method below is all that will be needed.
private void Temp()
    // Initialize the variables used in the method
    // This variable is used the smallest found in a 
    // single pass of randomNumbers.txt Initialed to 
    // Integer max value so that the first number read
    // from the file will become the smallest
    int smallest = int.MaxValue;
    // Holds the number read from the randomNumbers.txt
    // file converted to an integer
    int number = 0;
    // A temp variable used to help swap smallest with number
    int temp = 0;
    // A running total of the values in the list.
    int sum = 0;
    // Used to know when we are done the task.
    bool notempty = true;

    // File streams used in this method
    StreamReader inputFile;
    StreamWriter sorted;
    StreamWriter outputFile;

    // Open the streams
    inputFile = File.OpenText("randomNumbers.txt");
    outputFile = File.AppendText("TempFile.txt");
    sorted = File.AppendText("sorted_.txt");

    // Continue doing this untill randomNumber.txt is empty
    while( notempty )
        // Iterate through all numbers selecting the smallest
        // in each iteration and storing it in the sorted file.
        while( !inputFile.EndOfStream )
            // Read a line from the randomNumbers file
            // and convert to an integer  

            // Test to see which number is the smallest
            if (number < smallest)
                // Test to see if smallest is set to MaxValue
                if (smallest != int.MaxValue)
                    // Copy smallest to temp variable
                    // Copy number to smallest
                    // Then write temp to the outputFile
                    // Copy number to smallest;

        // Write the value in smallest to the sorted file
        // Add the value of smallest to the running total of sum
        // Set smallest to int.MaxValue for next iteration

        // Close the inputFile and outputFile

        // Copy the file on disk TempFile.txt to randomNumbers.txt 
        // Make sure to use the one with three parameters last parameter
        // is a boolean, set to tru to allow overwrite.
        // Delete file on disk TempFile.txt
        // Open the file randomNumbers.txt.
        // Then test to see if the file has a length equal to zero
        // If it does have a value of zero then set notempty to false
        // Open file TempFile.txt

    // Display the Sum of all the numbers that was in randomNumbers.txt

    // Close all files

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jyoung127Author Commented:
Thank you for you help.
Fernando SotoRetiredCommented:
If you have a question about the outline when you code it post here and I will help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.