I am working with sorting methods and random. Button1 Creates the parameter randomize numbers, size, and the maximum number limit to work from. Then according to what linear method is chosen, Button2 Sorts those numbers and then uses a stopwatch to time how long it took. I am currently implementing away to show : sort method, size, time_tosort and number of operations in a text file. So I have the first part done, when program loads I have prompted the user to create a file for where the results will be stored.
What ways can I go about appending the results into the textFile and averaging the results ? Also would I need to add a button to close the write function after user finishes sorting?
Example of desired format: sort method, size, time_tosort and number of operations
Linear 10000 .9 100,000,000
Linear 10000 .8 110,000,000
Linear 10000 .75 150,000,000
Linear 10000 .50 70,000,000
Linear 10000 .7375 107,500,000 ---- AVG
CODE
namespace sortMachine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Save()
{
var saveReport = new SaveFileDialog();
saveReport.Filter = "Text Files | *.txt";
var result = saveReport.ShowDialog();
if (result == DialogResult.Cancel || string.IsNullOrWhiteSpace(saveReport.FileName))
return;
using (var writer = new StreamWriter(saveReport.FileName))
{
writer.Write(textBox1.Text);
writer.Close();
}
}
private List<string> messages = new List<string>() { "Linear", "Bubble", "Index", "Other" };
private int clickCount = 0;
Stopwatch sw = new Stopwatch();
private void button1_Click(object sender, EventArgs e)
{
try
{
}
else if (textBox7.Text == "Bubble")
{
}
else if (textBox7.Text == "Index")
{
}
else if (textBox7.Text == "Other")
{
}
else if (textBox7.Text == "")
{
MessageBox.Show("Please input a sorting method");
}
}
private void Form1_Load(object sender, EventArgs e)
{
Save();
}
private void button3_Click(object sender, EventArgs e)
{
textBox7.Text = messages[clickCount];
clickCount++;
if (clickCount == messages.Count)
clickCount = 0;
}
}
}
I didn’t look through all the code, but you have
twothree parts to your question:How can I sort/average the results? Once you have a
Listof your results, you can call.Average(),.OrderBy(), and so on on them. These are part of theSystem.Linqnamespace. (Full list of useful functions here.)How can I output it to a text file? Take a look at the
File.IOnamespace. Here’s a guide.How can I get the data for the result? Your best bet is to create a new class:
Create a
List<SortData>and put all your results in there, each as anew SortData(). Then you can do something along these lines:You’ll need to output to the file instead of the console, but the idea is the same.