We help IT Professionals succeed at work.

C# Help

jyoung127
jyoung127 asked
on
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.
Form1.txt
Comment
Watch Question

Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
homeworks? we are not doing them for you!

Author

Commented:
I need help not asking for whole thing. Where is my code wrong?
Fernando SotoRetired
Distinguished Expert 2017

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

Author

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
Retired
Distinguished Expert 2017
Commented:
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

Sum();
temp();            
temp2(");
temp3();
temp4();
temp5();
temp6();

With this one line

Temp();

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
                }
                else
                    // Copy number to smallest;
            }
            else
                outputFile.WriteLine(number);
        }

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

Author

Commented:
Thank you for you help.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
If you have a question about the outline when you code it post here and I will help.