5 Replies - 8588 Views - Last Post: 01 March 2009 - 01:25 PM Rate Topic: -----

#1 MarcTonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-February 09

User input into Array.sort

Post icon  Posted 25 February 2009 - 11:53 AM

Hey everyone this is probably a stupid question and I am over thinking this and brain dead... I did all the hard ones ones for class and this should have been the easy one :P

I am making a simple program that takes the user's input and puts it into an array, then sorts the integers. I have been looking at some ways to do this but I cant get it to stick.

Sorry about the sloppy code but this is what I have

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{

	public class ReverseArraySort
	{

		public static void Main()
		{

			string inValue;

			Console.WriteLine("How many integers will you enter? \n");
			string amount = Console.ReadLine();
			int arrayAmount = Convert.ToInt32(amount);
			int[] number = new int[arrayAmount];

			for (int i = 0; i < number.Length; i++)
			{
				Console.WriteLine("\nEnter number {0}: ", i + 1);
				inValue = Console.ReadLine();
				number[i] = Convert.ToInt32(inValue);
			}


			//How do I get the numbers enetered from above to get put into the array?
			//I feel stupid this is so easy I know it....


			//This is part of one of the ways I saw to do it.

			int[] ints = new int
			Console.WriteLine("Array elements: ");
			DisplayArray(ints);
			Array.Sort(ints);
			DisplayArray(ints);
		}

		//This is another way i saw to do this.

		public static void DisplayArray(Array array)
		{
			foreach (object o in array)
			{
				Console.Write("{0} ", o);
			}
			Console.WriteLine();
			Console.ReadLine();
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: User input into Array.sort

#2 MarcTonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-February 09

Re: User input into Array.sort

Posted 25 February 2009 - 12:47 PM

Well I shortened it up and made it make sense. I just need to make the value sort the numbers. I will post up when I finish it, but any help or suggestions would be awesome!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{


	public class ReverseArraySort
	{

		public static void Main()
		{

			Console.WriteLine("How many integers will you enter? \n");
			string amount = Console.ReadLine();
			int arrayAmount = Convert.ToInt32(amount);
			int[] array = new int[arrayAmount];

			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Convert.ToInt32(Console.ReadLine());
			}

			Console.WriteLine("The Values you've entered are");
			Console.WriteLine("{0}{1,8}", "index", "value");
			for (int counter = 0; counter < array.Length; counter++)
				Console.WriteLine("{0,5}{1,8}", counter, array[counter]);
				Console.ReadLine();

	   }
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: User input into Array.sort

Posted 25 February 2009 - 12:54 PM

Here's an example of performing a Bubble Sort on an array that may help


/// <summary>
/// method to perform a bubble sort
/// </summary>
public static void BubbleSortArray()
{
	//variable too hold our array
	int[] arrayNum = new int[100];
	//variable to hold the boolean from TryParse
	bool isNum = false;
	//variable to hold the return value if TryParse is successful
	int sizeNum=0;

	//prompt the user to enter the array size
	Console.WriteLine("Number of elements in the array ?");
	//read the value in
	string sizeStr = Console.ReadLine();
	//use TryParse to check if the value is numeric
	isNum = Int32.TryParse(sizeStr, out sizeNum);

	//now check isNum
	if (isNum)
	{
		//they entered a valid numeric value, so now prompt for the array values
		Console.WriteLine("-----------------------");
		Console.WriteLine("Enter array values (numeric only).");
		Console.WriteLine("-----------------------");

		//now we need a loop for the size of the array to grab each value
		//they enter. We then need to check for numeric values
		for (int i = 0; i < sizeNum; i++)
		{
			//temp variable to hold each value the user enters
			int temp = 0;
			//read in each value on each iteration
			string arrayValue = Console.ReadLine();
			//now make sure they entered a numeric value
			isNum=Int32.TryParse(arrayValue,out temp);
		   
			//now check the value of isNum
			if (isNum)
			{
				 arrayNum[i] = temp;
			}
			else
			{
				Console.WriteLine("You entered a non-numeric value!");
				break;
			}

		} 
		int limit = sizeNum - 1;
		//now we need to start 2 more loops, each holding the
		//index of the previous loop
		for (int j = 0; j < sizeNum - 1; j++)
		{
			for (int k = 0; k < limit - j; k++)
			{
				//check if the index of the inner loop of the array
				//matches the inner loops index of the array + 1
				if (arrayNum[k] > arrayNum[k + 1])
				{
					//now we will do our sorting. we will set
					//the index of the inner loop to our finalNum
					//variable, then set the index of the array to the
					//index plus 1
					int finalNum = arrayNum[k];
					arrayNum[k] = arrayNum[k + 1];
					arrayNum[k + 1] = finalNum;
				}						
			}
		}
		//now we will print out the final sorted array
		Console.WriteLine("----------------------------------");
		Console.WriteLine("Bubble Sort results:");
		//loop through the sorted array and print out the values
		for (int l = 0; l < sizeNum; l++)
		{
			Console.WriteLine(arrayNum[l]);
		}
	} 
	else
	{
		Console.WriteLine("You must enter a numeric value!");
	}
}




And an example of a Bucket Sort on an integer array

private void BucketSort(int[] values)
{
	//make sure we have values to wort
	if (values == null || values.Length == 0) return;

	//variables to hold the min and max values of the
	//array, set them both to the first value in our array
	//so they're initialized
	int max = values[0];
	int min = values[0];

	//here we will loop through the array, starting at the first index (1)
	for (int i = 1; i < values.Length; i++)
	{
		//inside the loop we will find the max value in the array
		if (values[i] > max) max = values[i];
		//inside the loop we will find the min value in the array
		if (values[i] < min) min = values[i];
	}

	//create a bucket for holding the values in the array. Each
	//value will be stored in it's values's index
	//EXAMPLE: 15 will be at index 15 minus the min value
	List<int>[] holder = new List<int>[max - min + 1];

	//now inistialize our holder variable by creating a
	//new List<int>() for each value in our holder
	for (int i = 0; i < holder.Length; i++)
	{
		holder[i] = new List<int>();
	}

	//start moving values to our holder object
	for (int i = 0; i < values.Length; i++)
	{
		holder[values[i] - min].Add(values[i]);
	}
	
	//variable to hold the index of the
	//original array
	int k = 0;

	//now we move the items from pour temporary holder back to the array
	//in the new sorted order
	for (int i = 0; i < holder.Length; i++)
	{
		if (holder[i].Count > 0)
		{
			for (int j = 0; j < holder[i].Count; j++)
			{
				values[k] = holder[i][j];
				k++;
			}
		}
	}
}



Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#4 MarcTonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-February 09

Re: User input into Array.sort

Posted 25 February 2009 - 01:40 PM

Thank you! I am going to try the Bubble and Bucket get it running both ways. Show myself I can do it more than one way.

Once I get back from grocery store ill post up what I did again, if anyone would like to see what I am up.
Was This Post Helpful? 0
  • +
  • -

#5 StCroixSkipper  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 121
  • Joined: 23-December 08

Re: User input into Array.sort

Posted 26 February 2009 - 02:59 PM

View PostMarcTonk, on 25 Feb, 2009 - 12:40 PM, said:

Thank you! I am going to try the Bubble and Bucket get it running both ways. Show myself I can do it more than one way.

Once I get back from grocery store ill post up what I did again, if anyone would like to see what I am up.


The easiest way to solve your problem is with System.Collections.Generic List
List<string> list = new List<string>(); // this list grows as needed
list.Add("Tyrannosaurus");
list.Add("Amargasaurus");
list.Add("Mamenchisaurus");
list.Add("Deinonychus");
list.Add("Compsognathus");

list.Sort();
list.Reverse();



Was This Post Helpful? 0
  • +
  • -

#6 MarcTonk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 04-February 09

Re: User input into Array.sort

Posted 01 March 2009 - 01:25 PM

Here is what I did

using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace OrderedList
{
	class Program
	{
		static void Main(string[] args)
		{

			ArrayList myAL = new ArrayList();
			Console.WriteLine("How many numbers do you want to use?\n");
			String s = Console.ReadLine();
			int x = Convert.ToInt32(s);

			for (int i = 0; i < x; i++)
			{
				Console.WriteLine("\nEnter number {0}:", i + 1);
				String s1 = Console.ReadLine();
				double d = Convert.ToDouble(s1);
				myAL.Add(d);
			}

			myAL.Sort();
			Console.Clear();
			Console.WriteLine("The numbers in order:\n");
			foreach (object i in myAL)
			{
			Console.WriteLine(i);
			}

			Console.WriteLine("\nPress ENTER to terminate...");
			Console.ReadLine();
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1