Write Me a C++ Integer Sorting Program

arranging of numbers and sorting it

Page 1 of 1

13 Replies - 2935 Views - Last Post: 02 January 2010 - 06:55 PM Rate Topic: ***-- 2 Votes

#1 penshoppegirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-January 10

Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 01:04 AM

C++ program
how can make a program that enables me sort all the numbers that i input and arrange it in a ascending order..maximum of 20 numbers
example:
2
5
16
9
25
1
2
5
16
3
2

the output should look like this:

input----count
1 -----------1
2 -----------3
3 -----------1
5 -----------2
9 -----------1
16----------2
25----------1

if you can help me..please do...thank you!

This post has been edited by penshoppegirl: 02 January 2010 - 01:36 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Write Me a C++ Integer Sorting Program

#2 orca600  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 30-December 09

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 01:19 AM

Declare 20 integer variables.
Ask the user to input their numbers.
Read each number into a variable as the user inputs them.

Thats the first stage and the easiest. After that i would imagine you would need to use an 'if' statement where the condition is a comparison between variables in order to pair like values. Sorry but if i said anymore it would take me some time and would leave no work for you.
Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 04:30 AM

View Postorca600, on 2 Jan, 2010 - 08:19 AM, said:

Declare 20 integer variables.

while the OP didn't show any effort in researching or solving their problem, encouraging bad programming doesn't help anyone.


As for the OP's problem - I'd suggest reading up on vectors if you're not already familiar with them; a vector is able to store multiple values in a sequence.

This post has been edited by Bench: 02 January 2010 - 04:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 06:41 AM

*solution removed*

This post has been edited by KYA: 02 January 2010 - 09:42 AM

Was This Post Helpful? 1
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 07:04 AM

View Postanonymouscodder, on 2 Jan, 2010 - 01:41 PM, said:

A very simple solution.
*sigh* anonymouscodder will you please READ The rules of this forum here http://www.dreaminco...wtopic13280.htm

Do not post solutions to other people's homework - you aren't helping anyone in doing this, in fact, all you're doing is encouraging more of the same thinking that they can be spoonfed with pre-packaged solutions; cheating their way through courses instead of actually learning the concepts and tools for themselves.

This post has been edited by Bench: 02 January 2010 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 07:50 AM

And by the way:
I would like to emphazise the idea from you Bench, that beginners should start working with vectors. They are really much more comfortable than arrays in C++.

To use a vector:
#include <vector>
int main() {
vector<int> vectorname(vectorsize);
}

Was This Post Helpful? 0
  • +
  • -

#7 flyingcloud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-January 10

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 08:03 AM

i think using vector will be more easy
Was This Post Helpful? 0
  • +
  • -

#8 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 09:24 AM

View PostBench, on 2 Jan, 2010 - 06:04 AM, said:

*sigh* anonymouscodder will you please READ The rules of this forum here http://www.dreaminco...wtopic13280.htm

Do not post solutions to other people's homework - you aren't helping anyone in doing this, in fact, all you're doing is encouraging more of the same thinking that they can be spoonfed with pre-packaged solutions; cheating their way through courses instead of actually learning the concepts and tools for themselves.


I'm sorry, I'm new here. I won't do it again.
Was This Post Helpful? 0
  • +
  • -

#9 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 614
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 01:32 PM

My favourite way of sorting numbers is by using a binary tree, because It's fast and efficient.

Check it out.

This post has been edited by Aphex19: 02 January 2010 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#10 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 04:39 PM

Friend, go to this site for tutorials.
And yes, vectors are awesome. But remember that they use slightly more memory than an array.

This post has been edited by Delta_Echo: 02 January 2010 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#11 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 05:10 PM

View PostDelta_Echo, on 2 Jan, 2010 - 11:39 PM, said:

Friend, go to this site for tutorials.
And yes, vectors are awesome. But remember that they use slightly more memory than an array.

Vectors are implemented as arrays (This much is required by the standard), so by definition they use the same amount of memory as an array. Unless you're talking about the fact that they also include a variable which tracks its size (and perhaps another couple of tidbits of information and/or padding - but we're probably talking about a few bytes here). Anyone who is creating a dynamically allocated array would also be required to use a variable to track its size; There is also overhead from new, but again, this overhead is also associated with a dynamically allocated array.

This post has been edited by Bench: 02 January 2010 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#12 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 05:41 PM

From my understanding, vectors have a nice chunk of memory allocated for future expansion.
Was This Post Helpful? 0
  • +
  • -

#13 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 06:41 PM

Yes, although if you're doing anything involving raw arrays, you probably would too; Anyone who has ever tried writing their own resizable container will find out through profiling that resizing an array is expensive, so a well-thought implementation involving arrays usually has little choice but to counter-balance that cost by allocating more memory than is needed for expanding that array by a single extra element.


If you have a reasonable idea of how much memory you think you're going to need, then you can force a vector to reserve() some memory - Even if its empty for a time its not 'wasted' because you know eventually most/all of that memory is going to be used anyway - you may as well reserve it early on.
Was This Post Helpful? 0
  • +
  • -

#14 Delta_Echo  Icon User is offline

  • D.I.C Addict

Reputation: 5
  • View blog
  • Posts: 722
  • Joined: 24-October 07

Re: Write Me a C++ Integer Sorting Program

Posted 02 January 2010 - 06:55 PM

Exactly, I was refering to the extra memory in actual use. A static array of 5 elements vs. say a vector of 5 elements. I just meant that vectors, in general use; use slightly more memory.

This post has been edited by Delta_Echo: 02 January 2010 - 06:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1