How to put values of an array in numerical order

May 20, 2014 at 11:27pm
hey im trying to make a statistics program and i have an array holding all the users numbers. The array has 10 elements and i was wondering how i could put these elements in order from smallest to largest. Please help me out.
May 21, 2014 at 12:31am
1
2
3
4
5
#include <algorithm>
//...
int myarray[10];
//...
std::sort(myarray, myarray + 10);
May 21, 2014 at 8:25am
Thanks so much man.
May 21, 2014 at 9:27am
You're welcome :).
May 21, 2014 at 12:09pm
you can do it without using the sort function !

say if you want that !
:-)
thanks
May 21, 2014 at 12:45pm
Nah this great I saw something like this but it wasnt very clear. I have already got this up and running about half way through coding this project.
May 21, 2014 at 8:32pm
cool
May 21, 2014 at 9:03pm
You should understand sorting techniques too because they are the foundation of algorithms
in computer science, dont just blindly use the function.
May 21, 2014 at 10:03pm
I am currently reading a book on c++ fundamentals I might not be up to that stage but I wanted to write this program to practice my arrays.
May 21, 2014 at 10:17pm
Also, it might be better to use vectors instead of normal arrays since vectors are a lot less challenging to use.

1
2
3
4
5
6
#include <algorithm>
#include <vector>
//..
std::vector<int> vec(10)
//...
std::sort(vec.begin(), vec.end());


Vectors also come with lots of included functions which make life a little easier.
Last edited on May 21, 2014 at 10:19pm
May 21, 2014 at 11:29pm
is there a way to check if an array has the same number more than once to get the mode of a data set.
May 21, 2014 at 11:43pm
You'll have to do that with a nested for loop:

1
2
3
4
5
6
7
8
9
10
for (int i = 0; i < vect.size(); i++) // arrays are generally the same things as vectors, so it should work similarly with them
	{
		for (int j = 0; j < vect.size(); j++)
		{
			if (i != j && vect[j] == vect[i])
			{
				//Do your stuff. Assign i or j to a variable to store the index for later use or perform some other action right here.
			}
		}
	}
Last edited on May 21, 2014 at 11:45pm
May 22, 2014 at 3:15am
what if i want to count no of each numbers in an array ?
May 22, 2014 at 8:24am
We'll, take the address.
1
2
3
4
for(auto a = myarray; a != myarray + 10; a++)
{
// the value of "myarray" in each intance is *a 
}
May 22, 2014 at 2:06pm
sagarkar10,

If you want to count how many times a number appears in an array you can simply use the std::count(); function from <algorithm>

For further reading:
http://www.cplusplus.com/reference/algorithm/count/
Last edited on May 22, 2014 at 2:06pm
May 22, 2014 at 11:59pm
Gaius can you explain the if statement

if (i != j && vect[j] == vect[i])
i got it working in my program but i dont fully understand it. I know it loops through the vector with i and j but wont i != j be false because they are looping through it at the same time and also the other part. vect[j] == vect[i]. if you loop through zero then wont the not equal be false and then j and i will equal zero. Please help.
May 23, 2014 at 12:35am
if (i != j && vect[j] == vect[i])

Let's go through this step by step.

i = 0, j = 0


i != j = false
Because i is equal to j.
We do this check, because we don't want to compare a value to itself.

We move on. In nested for-loops, the nested loop (j) iterates all the way to the end, before the parent loop continues.
It looks kind of like this:

i = 0, j = 0
i = 0, j = 1
i = 0, j = 2

i = 1, j = 0
i = 1, j = 1
i = 1, j = 2

etc.

So the next iteration is going to be:

i = 0, j = 1


i != j = true
Because i isn't equal to j.
vect[j] == vect[i]
Here we test if the value in vect at j is the same as the value in vect at i. We aren't assigning anything to anything (just for clarification).

Let's pretend that vect[j] and vect[i] are the same, in which case we would get...

if(i != j && vect[j] == vect[i])
True, because i isn't equal to j and the value of vect at j is the same as the value of vect at i.

Hope this helped.

May 23, 2014 at 12:57am
If you use map instead of vector you can sort your lists automatically while you insert you data.
May 23, 2014 at 8:41am
Oh so the values of i and j are just the indexes and not the actual user input. So you are comparing index values instead of the actual numbers that vect[1] or vect[2] are holding. And if the indexes are not the same it checks if it is the same value as each other there for counting each index and comparing not to the same one but another index.
Last edited on May 23, 2014 at 8:47am
Topic archived. No new replies allowed.