473,800 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sort numbers from lowest to highest in C#

1 New Member
Hi

I need to write a programme that asks users to enter 10 numbers and once the numbers have been entered i then need to sort them from lowest to highest in C#

i.e 45, 3, 64, 6, 24, 75
3, 6, 24, 45, 64, 75


I am unsure on how to do it and what to use?
So far i have used strings to store the numbers entered,

if anyone could help ill be greatful!!

thanks
Mar 28 '07 #1
6 29331
Savage
1,764 Recognized Expert Top Contributor
Hi

I need to write a programme that asks users to enter 10 numbers and once the numbers have been entered i then need to sort them from lowest to highest in C#

i.e 45, 3, 64, 6, 24, 75
3, 6, 24, 45, 64, 75


I am unsure on how to do it and what to use?
So far i have used strings to store the numbers entered,

if anyone could help ill be greatful!!

thanks

Hi,

Why are you using strings?


You could only use array of integers.

To sort them try using two for loops with different counters so that secound loop is alocated inside first.

After this you need to see wich element is smaller(if you want to sort from highest to lowest than test wich is larger)

Then you have two choices:

1. Use a temporary array wich wich will store smaller number,then assign to smaller number value of larger and to ex larger value of temp.aray;

2.Simple do it without temp array.

It should be like:

Expand|Select|Wrap|Line Numbers
  1. if(a[j]<a[i])
  2. {
  3.       a[i]=a[i]+a[j];
  4.       a[j]=a[i]-a[j];
  5.       a[i]=a[i]-a[j];
  6. }
  7.  
  8. /*else do nothing.*/
Note: j is starting from j=i+1 in secound for loop.

Savage
Mar 28 '07 #2
sicarie
4,677 Recognized Expert Moderator Specialist
Hi

I need to write a programme that asks users to enter 10 numbers and once the numbers have been entered i then need to sort them from lowest to highest in C#

i.e 45, 3, 64, 6, 24, 75
3, 6, 24, 45, 64, 75


I am unsure on how to do it and what to use?
So far i have used strings to store the numbers entered,

if anyone could help ill be greatful!!

thanks
1) why not store them as numbers, as then you will be able to make numeric comparisons (greater than, less than, etc...)? I would think all you need is a numeric array and a tmp var.
2) As this is C#, I am moving this to the .NET forum.
Mar 28 '07 #3
SammyB
807 Recognized Expert Contributor
Hi
I need to write a programme that asks users to enter 10 numbers and once the numbers have been entered i then need to sort them from lowest to highest in C#
i.e 45, 3, 64, 6, 24, 75
3, 6, 24, 45, 64, 75

I am unsure on how to do it and what to use?
So far i have used strings to store the numbers entered,
if anyone could help ill be greatful!!
thanks
Assuming you are using C#.NET, then the Array class has a Sort method. So, if you are allowed, the sort is only one line:
Expand|Select|Wrap|Line Numbers
  1. int[] MyNumbers = new int[] {45,3,64,6,24,75,3,6,24,45};
  2. Array.Sort(MyNumbers);
Note -- this only answers your sort question, you still have to get the numbers from the user and print out the sorted array.
Mar 28 '07 #4
vanc
211 Recognized Expert New Member
You can try dynamic array ArrayList to Add item and Sort them, and you also don't have to worry about predefined length of array.
Cheers.
Mar 28 '07 #5
tastro
2 New Member
Hi, Savage could you please explain with words what this function does? Because it works correct for me, but i just can't figure out how it works... I have been staring at it now for more then 30 minutes.
Dec 4 '13 #6
tastro
2 New Member
I think that i get it now. Thanks!
Dec 4 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1668
by: GIMME | last post by:
My question is ... How do I sort an Array on numeric, not character values ? In the example below, after sorting the contents are 1,10,2,3 . How do I get the contents to be 1,2,3,10 ? Thanks, em
11
1949
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them to the database. How can accomplish this task? Thanks!
17
7722
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
2
1386
by: bigpaco | last post by:
How do I sort five numbers? and what library should I include?
5
3793
by: ryuchi311 | last post by:
In C++ using arrays. I need to create a C Program that will ask for five integers input from the user, then store these in an array. Then I need to find the lowest and highest integers inside that array and add them together. The output will be the result in this format: LOWEST+HIGHEST=SUM #include<stdio.h> void main (){ int r, low, hig, ans, index; for(index=0, index<4, index++){ printf("Enter number");
1
3645
by: pratimapaudel | last post by:
Can anyone help me to do this problem? Question goes like below: Use C or Borne shell. Take two integer command line arguments. The script should display the numbers between the two integers (including the two numbers) in ascending order if the first number is smaller than the second, and in descending order if the first number is greater than the second. Do proper exception handling in code. Thanks,
7
7401
by: Alberto Fortuny | last post by:
Hey guys, any idea as to how to find the lowest value and the highest value in this 2D array? I made the functions to find the lowest and highest, but all im getting on execution is a 1 on both, no matter what number i input. Here's my code #include <cstdlib> #include <iostream> using namespace std; const int MONKEYS = 3; const int DAYS = 7; double average = 0;
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.