0

Question: Write a program named SortWords that includes a method that accepts any number of words and sorts them in alphabetical order. Demonstrate that the program works correctly when the method is called with one, two, five, or ten words.

What I have thus far:

private void button1_Click(object sender, EventArgs e)
{
    String[] outWords = new string[20];       
    outWords[0] = textbox1.Text;
    outWords[1] = textBox2.Text;
    outWords[2] = textBox3.Text;
    outWords[3] = textBox4.Text;
    outWords[4] = textBox5.Text;
    outWords[5] = textBox6.Text;
    outWords[6] = textBox7.Text;
    outWords[7] = textBox8.Text;
    outWords[8] = textBox9.Text;
    outWords[9] = textBox10.Text;

    sortAndPrint(outWords[11]);
}

private void sortAndPrint(params string[] newWords)
{
    Array.Sort(newWords);

    label1.Text = newWords[0];
    label2.Text = newWords[1];
    label3.Text = newWords[2];
    label4.Text = newWords[3];
    label5.Text = newWords[4];
    label6.Text = newWords[5];
    label7.Text = newWords[6];
    label8.Text = newWords[7];
    label9.Text = newWords[8];
    label10.Text = newWords[9];
}

My issues here is that I either don't get anything in my label boxes or I get errors thrown from can't convert a string into string[] or System.IndexOutOfRangeException. I'm sure im doing something completely wrong here.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • You state `sortAndPrint(outWords[11]);`. What is `outWords[11]`? It appears to never be assigned. Perhaps you should pass either the whole array `outWords`, or as the assignment states, pass one, two, five, and ten words to the method. – Rufus L Mar 15 '18 at 02:09
  • your method accepts any number of words, but you are hard-coding indexes that may or may not exist in the `newWords` params array. You should not assume this, and instead use a loop to output the values. Make sure you understand the difference between `params string[]` and just plain old `string[]`. – Rufus L Mar 15 '18 at 02:11
  • Check the answer below. There are many changes in you code. You can easily identify. – Gaurang Dave Mar 15 '18 at 02:12

3 Answers3

1

Try this :

private void button1_Click(object sender, EventArgs e)
{
    string[] outWords = new string[10];

    outWords[0] = textbox1.Text;
    outWords[1] = textBox2.Text;
    outWords[2] = textBox3.Text;
    outWords[3] = textBox4.Text;
    outWords[4] = textBox5.Text;
    outWords[5] = textBox6.Text;
    outWords[6] = textBox7.Text;
    outWords[7] = textBox8.Text;
    outWords[8] = textBox9.Text;
    outWords[9] = textBox10.Text;

    sortAndPrint(outWords);

}

private void sortAndPrint(string[] newWords)
{
    Array.Sort(newWords);

    label1.Text = newWords[0];
    label2.Text = newWords[1];
    label3.Text = newWords[2];
    label4.Text = newWords[3];
    label5.Text = newWords[4];
    label6.Text = newWords[5];
    label7.Text = newWords[6];
    label8.Text = newWords[7];
    label9.Text = newWords[8];
    label10.Text = newWords[9];
}

Summary

Pass whole array sortAndPrint(outWords); not single element.

Take array length only what you need. string[] outWords = new string[10];

Please check this question for the use of params. You need to pass values when you user param but if you need to pass variable, you have to remove params.

Example of params

private void button1_Click(object sender, EventArgs e)
{
    sortAndPrint("one","two","three","four","five");
}

private void sortAndPrint(params string[] newWords)
{
    Array.Sort(newWords);

    label1.Text = newWords[0];
    label2.Text = newWords[1];
    label3.Text = newWords[2];
    label4.Text = newWords[3];
    label5.Text = newWords[4];
}
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34
  • *"Demonstrate that the program works correctly when the method is called with one, two, five, or ten words"* I don't think passing in an entire array was the actual objective here... (hence the signature is `params string[]`, not `string[]`) – Rufus L Mar 15 '18 at 02:13
  • @RufusL added one more point in summary. Thanks for noticing that. :) – Gaurang Dave Mar 15 '18 at 02:18
0
sortAndPrint(outWords[11]);

will pass a single string (as an array) to sortAndPrint, so when you call

label2.Text = newWords[1];

you get an out of bounds exception. Try just

sortAndPrint(outWords);

to pass the entire array. Also note that empty slots in the array wil get sorted before other strings, so you need to come up with a way to get rid of the blank/null string.

Or, if the intent is to demonstrate how to use params, you could do something like:

sortAndPrint(textbox1.Text, 
             textbox2.Text,  
             textbox3.Text,  
             textbox4.Text,  
             textbox5.Text);

But you need to check the bounds of the array in sortAndPrint, rather than just assuming the array has a size of at least 10.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

If you read the instructions carefully, you have this requirement:

...includes a method that accepts any number of words...

You accomplish this by using the params keyword along with a string[].

...and sorts them in alphabetical order.

This part you doing with Array.Sort(newWords);

Demonstrate that the program works correctly when the method is called with one, two, five, or ten words

This part you're not doing - you're assuming in your code that the input array will have 10 items, when instead you should check to see how many items it has before outputting the results.

Since the array size cannot be determined by the method, then it cannot make any assumption that there will be enough labels on the form to populate with the results. Given this, we could use a MessageBox to show the results instead, and we can use String.Join to join all the items in the array with an Environment.NewLine character in order to show the sorted words in different lines:

private void SortAndPrint(params string[] newWords)
{
    Array.Sort(newWords);
    MessageBox.Show(string.Join(Environment.NewLine, newWords));
}

Now, we can demonstrate the use of this function by passing in different numbers of arguments to it.

First, just so we're on the same page, I have this code in the Form_Load method that adds 10 textboxes to the form, all with the tag "input":

private void Form1_Load(object sender, EventArgs e)
{
    var stdHeight = 20;
    var stdWidth = 100;
    var stdPad = 10;
    var count = 10;

    for (int i = 0; i < count; i++)
    {
        var textBox = new TextBox
        {
            Name = "textBox" + (i + 1),
            Left = stdPad,
            Width = stdWidth,
            Height = stdHeight,
            Top = (stdHeight + stdPad) * i + stdPad,
            Tag = "input"
        };
        Controls.Add(textBox);
    }
}

Now, in our button click event, we can search for all controls on the form who have the Tag == "input" and whose .Text property is not empty, and we can pass these Text values to our method both as a single array OR as individual items:

private void button1_Click(object sender, EventArgs e)
{
    // Select all textbox Text fields that have some value
    var words = Controls.Cast<Control>()
        .Where(t => t.Tag == "input" && !string.IsNullOrWhiteSpace(t.Text))
        .Select(t => t.Text)
        .ToArray();

    // Pass all the words in a single array to our method
    SortAndPrint(words);

    // We can also demonstrate this by passing individual words
    // Pass one word if we can
    if (words.Length > 0)
    {
        SortAndPrint(words[0]);
    }
    // Pass two words if we can
    if (words.Length > 1)
    {
        SortAndPrint(words[0], words[1]);
    }
    // Pass five words if we can
    if (words.Length > 4)
    {
        SortAndPrint(words[0], words[1], words[2], words[3], words[4]);
    }
    // Pass ten words if we can
    if (words.Length > 9)
    {
        SortAndPrint(words[0], words[1], words[2], words[3], words[4], 
                        words[5], words[6], words[7], words[8], words[9]);
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43