0

i'm beginner at c# and programming in total.. so i'm working on this code in school right now, where i have to generate 21 random numbers between 1-21 (you can have duplicates of numbers). i have made the code and it's working sorta... it generates the numbers in listbox1 but it's not the same numbers i get sorted in listbox3.

private void button1_Click(object sender, EventArgs e)
        {
            int[] a = new int[21];
            Random tal = new Random();


            for (int x = 1; x < a.Length; x++)
            {
                a[x] = tal.Next(1, 21);
            }
            for (int x = 1; x < a.Length; x++)
            {
                listBox1.Items.Add(a[x]);
            }
            foreach (int i in a)
            {
                Array.Sort(a);
                listBox3.Items.Add(a[i]);
            }
            int min = a[1];
            for (int x = 1; x < a.Length; x++)
                if (a[x] < min)
                    min = a[x];
            listBox2.Items.Add(min);
            int max = a[20];
            for (int x = 1; x > a.Length; x++)
                if (a[x] < max)
                    max = a[x];
            listBox2.Items.Add(max);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int[] a = new int[21];
            Random tal = new Random();
            a.Clone();

            foreach (int i in a)
            {
                Array.Sort(a);
                listBox3.Items.Add(a[i]);
            }
        }
    }
}
1
  • Break the problem down into much smaller parts and work on each piece separately. Commented Feb 1, 2019 at 21:12

2 Answers 2

1

1.Array begin with index 0, so change all your loops to start from 0:

 for (int x = 0; x < a.Length; x++)

instead of

for (int x = 1; x < a.Length; x++)

and also int min = a[0]; not int min = a[1];

2.Sort the array outside the for loop, there is no need to do it over and over:

Array.Sort(a);
foreach (int i in a)
{
    listBox3.Items.Add(a[i]);
}

the items must be the same (but different order).

and also you are cloning a (a.Clone();) and actually not assigning it to anything, so its an extra code.

Sign up to request clarification or add additional context in comments.

6 Comments

the (a.clone();) wasn't finished, cause i was trying to search online for it, but if i want the button_click3 to be used for sorting the numbers generate by clicking button_click1, should i copy the array ? and if so, how do i do that? because when i use array.copy(a); it gives me error in button_click3
well the array a is defined in button1_click() scope and invalid outside it. you can either declare a in a bigger scope (inside your form calss) just after your form class begins, or use the listbox1 items (int button3) for sorting: int a = listbox1.Items.Select(x=> int.Parse(x)); and then use the new a array.
thx alot! so that means if i move the int[] a= new int[21]; up to form class, i can use it again down at button3_click right?
the code in my comment was wrong it should be: int[] a = listbox1.Items.Select(x=> int.Parse(x)).ToArray();
Hello again @Ashkan so i now got the whole thing to work with the same numbers and new button ect. the only problem i have is that i can only get sorting to work descending, when i have to make it ascending. a.orderbydescinding(x=>x=).ToArray(); is working, but what should i use for ascending order?
|
0

Variables have scope.

What this means for you right now, is that the a in button1_Click is not the same as the a in button3_Click (not to mention they are assigned to different arrays).

If you need them to be shared, a should be declared at the class level (ie, not in a method). Then both methods can use the same variable (just don't re-assign it!).

Also:

  • a.Clone(); doesn't do anything unless you assign the result
  • Calling Sort in your loop is totally overkill
  • Random tal = new Random() isn't even used in button3_click

2 Comments

so i took the sort out of the loop now, but how can i get the numbers from listbox1 to be the same in listbox3 just sorted? cause it's just different numbers it sorts in listbox3
but when i moved the sort out of the loop i get a number like "19" as lowest even though the number after is "2"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.