how to put in element of array in order

hello ..
help me to solve this :

Write a C++ program that has an array of integers. The maximum size of the array is 100. The program should ask the user to enter the number of elements to be stored in the array, then the program should ask the user to enter that many numbers to be stored in the array as they are entered.

((The program should call a function (putInOrder) to rearrange the numbers stored in the array such that they are stored in the array in increasing order)).

The function putInOrder takes as input the address of the array and the number of elements in the array.




how cuold i write a code to sort the numbers of array in order "starting from smaller number to the begest number??
To answer the question would defeat the purpose of the assignment. But let me point out a big something: What algorithm do you need to use? Bubble sort? Quicksort? Merge sort? First decide on the algorithm, then attempt to code this. Then ask questions if you get stuck.
i wrot the program like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

void putInOrder (int,int& ); // to rearrange the number 


int main ()
{
	int arrynum;
	int array1[100];



	cout <<"enter the number of elements to be stored in the array: ";
	cin >> arrynum;

	if (arrynum>100)
	{cout <<"Erorr!:the array can't hold more than 100 element"<<endl
		  <<"the program will stop runing. . . \n";
	return 0;}

	

	for  (int n=0 ; n<arrynum ; n++)
	{
		cout <<"Enter the "<<arrynum<<" numbers that you chose:";
			cin >> array1[n];

	}



	return 0;
}



but i still have to write the function void putInOrder (int,int& ); to reorder the number in the array starting from smallest one to largest one
Last edited on
Bazzy, I don't think the guy can just use the sort algorithm provided by STL. In my first programming class I was taught several algorithms and I had to write code for them all.
what sort do you have to use? The easiest for a beginner is probably bubble sort but that said bubble sort as a god awful sort.
for this type of assignment it might be easier to actually add the element in the correct place instead of sorting the list at the end.
List: { ?, ?, ?, ?, ?, ... }

Entered: 5
List: { 5, ?, ?, ?, ?, ... }

Entered: 1
List: { 1, 5, ?, ?, ?, ... }

Entered: 2
List: { 1, 2, 5, ?, ?, ... }

Entered: 8
List: { 1, 2, 5, 8, ?, ... }

...
hey thanks for bing with me ,,,

but they need me to sort it after store it in the array

because i am a computer sciense student
and this assignment from my first cource in c++!!
Did they say what algorithm you should use to sort the array? There are many different ways, and most of them have names; like: Bubble, Insertion, Selection, Shell, Merge, Quick, ...
id reccomend a bubble sort for just 100 integers. it works like so:

say i have 5 numbers in the wrong order
5, 3, 8, 2, 1

i start at index 0 each time and compare it to every number like this

5 swaps places with 3 because it is greater, so i now switch them, array is now (3, 5, 8, 2, 1)

5 is less then 8, so i dont swap them and continue sorting now with 8.

8 is greater then 2, swap them, array is now (3, 5, 2, 8, 1)

8 is greater then 1, swap them, array is now (3, 5, 2, 1, 8)

now i start over at index 1 again.

3 is less than 5 so continue with 5.

5 is greater than 2 so swap them. array is now (3, 2, 5, 1, 8)

5 is greater than 1 so swap them. array is now (3, 2, 1, 5, 8)

5 is less than 8 so do nothing.

hopefully you see how it would just keep on going. at the end of each iteration you check to see if the numbers are sorted or not.

hope this helps :)
Last edited on
Topic archived. No new replies allowed.