Sort C# array while adding numbers [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have this array and I want that every time the user inputs a number, the program immediately makes the sort. How can I do that? For example, if I input 6 and then 3, immediately takes the 3 to the first position. And then if I put 2, immediately take it to the first position and sort the others (2,3,6). Then if I put 1, takes it to the first position, sorting the others(1,2,3,6) and so on
int[] a = new int[5] ;
for (int i = 0; i < a.Length; i++)
{
a[i] = Convert.ToInt32(Console.ReadLine());
}

As #juharr suggest you can use System.Collections.Generic.SortedSet:
static void Main(string[] args)
{
var list = new SortedSet<int>();
int count = 5;
for ( int i = 0; i < count; i++ )
list.Add(Convert.ToInt32(Console.ReadLine()));
foreach (int item in list)
Console.WriteLine(item);
Console.ReadKey();
}

add the numbers to a list and call list.sort()
List<int> list = new List<int> { 6, 3 };
list.Sort();

Related

Sort float numbers List from small to large [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to sort float numbers List from small to large. I tried to use the array.Sort () method but it doesn't work.
Below is my code.
List<float> array = new List<float>();
array.Add(x);
listBox1.Items.Add(x);
Just Sort():
List<float> array = new List<float>();
array.Add(x);
array.Sort();
When array (quite a strange name for List<float>) is sorted, we can insert x somewhere in the middle of listBox1.Items. Asuming that listBox1.Items contains ordered float items only, we can put
// Here's we have WinForms ListBox
int at = listBox1.Items.Count;
for (int i = 0; i < listBox1.Items.Count; ++i) {
if (x < (float) (listBox1.Items[i])) {
at = i;
break;
}
}
listBox1.Items.Insert(at, x);

How do I create a program that grabs a random number from a pool of numbers and adds it to a total in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I understand how to create a random number generator in C # that is not what I'm trying to do. What I'm trying to do is to create a program that will pull numbers from an existing list of numbers output those numbers and add it to a total.
var numbers = new List<int>();
numbers.Add(50);
numbers.Add(100);
numbers.Add(150);
For example the numbers within this list. When the person running the program inputs R it will output one of these 3 numbers, Let's say it outputs 50 the total output would be 50, then the user can run it again, and let's say it outputs 150 at that time. the total would go up to 200.
If you simply want random numbers from a list you should see #Adrian Efford answer. If you want non repeating numbers from a list you can shuffle the list;
public static void Shuffle<T>(this IList<T> list, Random rnd)
{
for(var i=list.Count; i > 0; i--)
list.Swap(0, rnd.Next(0, i));
}
public static void Swap<T>(this IList<T> list, int i, int j)
{
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
And use like
Shuffle(MyList, rnd);
MyList.Take(10).Sum();
You could further extend the solution by adding a parameter to the shuffle method that stops shuffling after N items.
Here a solution in case you just need to add them for one run of the APP:
If you need to store the values for other runs in the future let me know & I will edit my answer. (In this case you will need a database or a external text file to store the data)
List<int> PossibleNumbers = new List<int>()
{
10,
50,
23,
45,
100
};
Random rnd = new Random();
List<int> randomNumbers = new List<int>();
public int GetRandomFromList()
{
return PossibleNumbers[rnd.Next(0, PossibleNumbers.Count)];
}
public int NewSum()
{
//Get number
var random = GetRandomFromList();
randomNumbers.Add(random);
//Add numbers
int sum = 0;
foreach (var number in randomNumbers)
{
sum += number;
}
return sum;
}

create array on the fly with random length containing elements initialized with fixed value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this code :
for(int i=0;i<???;i++)
{
...
}
tb.SetWidths(new int[] { 50, 50, 50,.... });
The problem is:
The amout of array elemens must be equal to "i"
I also want to set the value to all these elements to 50
How can i do that?
Tb is an object from itextsharp, i'm using it to draw tables on pdf files
I guess something like that would work for you? (https://stackoverflow.com/a/34379619/986160)
if count is random you can do:
Random rand = new Random();
int count = rand.Next(1, 101); // creates a number between 1 and 100
( 50 is the fixed value for all 'count' elements)
int[] array = Enumerable
.Repeat(50, count)
.ToArray();
then you can do:
tb.SetWidths(array);
It seems like you've put little to no effort into this exercise.
By simply Googling "c# define array of size" I found the following code:
int i = random number;
int[] myArray = new int [i];
Next, in order to populate the array with a certain integer, you simply loop through it:
for(int x = 0; x < myArray.Length; x++){
myArray[x] = 50;
}

How to call my int array class to ad my dropdownlist [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public static int[] ddDuration()
{
int[] ddarray = new int[] { 1, 2, 3, 4, 5 };
return ddarray;
}
How to make in c# to call my int array to who any page in my DropDownList
//and I am try to show in my DropDownList
//like that some thing
Duration dm = new Duration();
//dm.ddDuration;
//dropdduration.item.AddRang(dm.ddDuration);
If I got this right, you're trying to add an array of integers as items to a DropDownList.
var collection = ddDuration();
int len = collection.length;
for(int i=0; i<len; i++){
DropDownList1.Items.Insert(i, new ListItem(collection[i].toString(), ""));
}
Insert's first parameter is the position (index) in the Items collection and the second parameter takes a ListItem; of which constructor can be build upon its content (string in this case).

Making an array ints and replace the order of number I write [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I try to do, is for example if I write five numbers 1,2,3,4,5 after this it should print 5,4,3,2,1
int[] numbers = new int[5];
for (int i = 0; i < numbers.Length ; i++)
{
Console.Write("");
numbers[i] = int.Parse(Console.ReadLine());
}
for (int i = 0; i < numbers.Length; i++)
{
}
If you need just to print them in a reverse order, but you want to keep them stored as you inserted them, change the second loop like this:
for (int i=numbers.Length-1; i >= 0; i--){
Console.WriteLine(numbers[i]);
}
You can print the array with a reverse loop as in the previous answer, or you can do that in a single line:
Console.WriteLine(string.Join(",", numbers.Reverse()));

Categories

Resources