2 Replies - 4576 Views - Last Post: 03 December 2008 - 09:27 PM Rate Topic: -----

#1 zharren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-November 08

sorting array of numbers with no repeats

Posted 02 December 2008 - 07:37 PM

Hi, I don't quite understand where to put the selection sort algorithm in my code. I have an array of 3 integers, each one will be randomly chosen. Then I have to compare them and make sure there are no duplicates. Once that is finished, I need to output the numbers in ascending order. I've already got the first part done, and i have the algorithm. Please let me know where the sorting should be.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace @new
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		Random rand = new Random();
		int[] Inum = new int[3];

	
// button to check for duplicate numbers and output them in textboxes

	private void button1_Click(object sender, EventArgs e)
		
		{
			
			Inum[0] = (rand.Next(1, 49));	
			textBox1.Text = Convert.ToString(Inum[0]);
			
			do
			{
				Inum[1] = (rand.Next(1, 49));
			}
			while (Inum[1] == Inum[0]);
			textBox2.Text =Convert.ToString(Inum[1]);
			
			do
			{
				Inum[2] = (rand.Next(1, 49));
			}
			while ((Inum[2] == Inum[0])|(Inum[2] == Inum[1]));
			textBox3.Text =Convert.ToString(Inum[2]);
		}
			
	
// button to sort original array(not sure about this)
   private void button2_Click(object sender, EventArgs e)
		{		
			int min;
			int temp;
			int count = 0;
			int [] nr = new int [3];
			int a = Convert.ToInt32(textBox1.Text);
			int b = Convert.ToInt32(textBox2.Text);
			int c = Convert.ToInt32(textBox3.Text);
			nr[0] = a;
			nr[1] = b;
			nr[2] = c;

			for (int i = 0; i < count - 1; i++)
			{
				
				min = i;
				for (int j = i + 1; j < count; j++)
				{
					if (nr[j] < nr[min])
						min = j;
				}
				temp = nr[i];
				nr[i] = nr[min];
				nr[min] = temp;
			}
		  
				  for (int i = 0; i<count; i++)
				  {
					 textBox1.Text = textBox1.Text + Convert.ToString(nr[i]);
					 textBox2.Text = textBox2.Text+Convert.ToString(nr[i]);
					 textBox3.Text = textBox3.Text+Convert.ToString(nr[i]);
				  }

			
			  }
			}		 
		}

	   
	


Is This A Good Question/Topic? 0
  • +

Replies To: sorting array of numbers with no repeats

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: sorting array of numbers with no repeats

Posted 02 December 2008 - 11:15 PM

View Postzharren, on 2 Dec, 2008 - 06:37 PM, said:

Hi, I don't quite understand where to put the selection sort algorithm in my code. I have an array of 3 integers, each one will be randomly chosen. Then I have to compare them and make sure there are no duplicates. Once that is finished, I need to output the numbers in ascending order. I've already got the first part done, and i have the algorithm. Please let me know where the sorting should be.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace @new
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		Random rand = new Random();
		int[] Inum = new int[3];

	
// button to check for duplicate numbers and output them in textboxes

	private void button1_Click(object sender, EventArgs e)
		
		{
			
			Inum[0] = (rand.Next(1, 49));	
			textBox1.Text = Convert.ToString(Inum[0]);
			
			do
			{
				Inum[1] = (rand.Next(1, 49));
			}
			while (Inum[1] == Inum[0]);
			textBox2.Text =Convert.ToString(Inum[1]);
			
			do
			{
				Inum[2] = (rand.Next(1, 49));
			}
			while ((Inum[2] == Inum[0])|(Inum[2] == Inum[1]));
			textBox3.Text =Convert.ToString(Inum[2]);
		}
			
	
// button to sort original array(not sure about this)
   private void button2_Click(object sender, EventArgs e)
		{		
			int min;
			int temp;
			int count = 0;
			int [] nr = new int [3];
			int a = Convert.ToInt32(textBox1.Text);
			int b = Convert.ToInt32(textBox2.Text);
			int c = Convert.ToInt32(textBox3.Text);
			nr[0] = a;
			nr[1] = b;
			nr[2] = c;

			for (int i = 0; i < count - 1; i++)
			{
				
				min = i;
				for (int j = i + 1; j < count; j++)
				{
					if (nr[j] < nr[min])
						min = j;
				}
				temp = nr[i];
				nr[i] = nr[min];
				nr[min] = temp;
			}
		  
				  for (int i = 0; i<count; i++)
				  {
					 textBox1.Text = textBox1.Text + Convert.ToString(nr[i]);
					 textBox2.Text = textBox2.Text+Convert.ToString(nr[i]);
					 textBox3.Text = textBox3.Text+Convert.ToString(nr[i]);
				  }

			
			  }
			}		 
		}

	   
	

You basically have it! :)

A couple of problems though. while ((Inum[2] == Inum[0])|(Inum[2] == Inum[1])); has a "|" instead of "||", an easily made mistake. :)

Another problem is
...
int count = 0;
...
for (int i = 0; i < count - 1; i++)


Count is set to zero, not the length of the nr[] array. Better to use for (int j = i + 1; j < nr.Length; j++), which sizes the loop to the array size.

Finally, in the output loop you add the sorted numbers to the original textbox contents 0 times. :)
for (int i = 0; i<count; i++)
{
   textBox1.Text = textBox1.Text + Convert.ToString(nr[i]);
   textBox2.Text = textBox2.Text + Convert.ToString(nr[i]);
   textBox3.Text = textBox3.Text + Convert.ToString(nr[i]);
}


Here's some code that maybe closer to what you want. I changed the output loop to just output the sorted numbers. The number strings could be appended to the original contents if that is what you are trying to achieve: tba[i].Text += " " + nr[i].ToString();
        private void button2_Click(object sender, EventArgs e)
        {
            int min;
            int temp;

            //int count = 0;  unused
            int[] nr = new int[3];
            
            // create an array of textboxes for the output loop
            TextBox[] tba = new TextBox[3];

            tba[0] = textBox1;
            tba[1] = textBox2;
            tba[2] = textBox3;

            //int a = Convert.ToInt32(textBox1.Text);
            //int b = Convert.ToInt32(textBox2.Text);
            //int c = Convert.ToInt32(textBox3.Text);

            nr[0] = Convert.ToInt32(textBox1.Text);
            nr[1] = Convert.ToInt32(textBox2.Text);
            nr[2] = Convert.ToInt32(textBox3.Text);

            for (int i = 0; i < nr.Length - 1; i++)
            {

                min = i;

                for (int j = i + 1; j < nr.Length; j++)
                {
                    if (nr[j] < nr[min])
                        min = j;
                }
                
                temp = nr[i];
                nr[i] = nr[min];
                nr[min] = temp;
            }

            for (int i = 0; i < nr.Length; i++)
            {
                //textBox1.Text = textBox1.Text + Convert.ToString(nr[i]);
                //textBox2.Text = textBox2.Text + Convert.ToString(nr[i]);
                //textBox3.Text = textBox3.Text + Convert.ToString(nr[i]);
                tba[i].Text = nr[i].ToString();
            }
        }


Was This Post Helpful? 1
  • +
  • -

#3 zharren  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-November 08

Re: sorting array of numbers with no repeats

Posted 03 December 2008 - 09:27 PM

Thank you very much! =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1