How Can I Generate and Sort 500 Numbers in an Array?

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Array Numbers
Click For Summary
SUMMARY

This discussion focuses on generating and sorting an array of 500 random numbers using C++. The user initially generates random numbers with the `rand()` function and seeks to sort them. The recommended sorting algorithm is the "bubble sort," which iteratively compares and swaps adjacent elements until the array is sorted. The user clarifies that the goal is to print an array with the same number of elements, leading to a solution involving initializing an array with zeros.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with random number generation using the `rand()` function
  • Knowledge of sorting algorithms, specifically bubble sort
  • Basic array manipulation in C++
NEXT STEPS
  • Implement and optimize the bubble sort algorithm in C++
  • Explore alternative sorting algorithms such as quicksort or mergesort
  • Learn about the `std::sort` function in C++ for efficient sorting
  • Investigate random number generation techniques in C++ for different distributions
USEFUL FOR

C++ developers, computer science students, and anyone interested in learning about array manipulation and sorting algorithms in programming.

needOfHelpCMath
Messages
70
Reaction score
0
I want to generate 500 numbers in an array but in order? For example I can generate 500 random numbers with this code but how will i be able to put it in order?

HTML:
//code example

count <<  "Generate 500 random numbers:" << endl;
	const int nnn = 500;
	int numberOfArray3[nnn];
		for (i = 0; i < nnn; ++i) {
			numberOfArray3[i] = rand() % 100;
			count << numberOfArray3[i] << " ";
		}
 
Technology news on Phys.org
What do you mean by "in order"? Are you saying that you have generated 500 numbers using a random number generator and then want to order them from smallest to largest? There are a number of algorithms for ordering a set of numbers. Probably the simplest is the "bubble sort":
https://en.wikipedia.org/wiki/Bubble_sort. The "bubble sort" goes through the list repeatedly, comparing each pair of adjacent numbers, if the larger number comes first, the two numbers are swapped, then goes on to the next number. The repetition repeats until it goes through the entire list without any swaps.

For example if the list is 2, 4, 5, 3 then "bubble sort" looks at the first pair, (2, 4). They are in the correct order so it goes on to (4, 5). They are also in the correct order so it goes on to (5, 3). They are not in the correct order so it swaps them to (3, 5) so the list is now 2, 4, 3, 5. Now, repeat. The first pair, (2, 4), is in the correct order so it goes on to the next pair, (4, 3). They are not in the correct order so they are swapped to (3, 4). The list is now 2, 3, 4, 5. The last pair, (4, 5) is in the correct order so it goes back to the beginning and goes through that list, finding no swaps so terminates.

One nice thing about "bubble sort" is that it can be done "in place". If your original list is A then your sorted list will also be in A. You do not have to define a new array to hold the numbers.
 
HallsofIvy said:
What do you mean by "in order"? Are you saying that you have generated 500 numbers using a random number generator and then want to order them from smallest to largest? There are a number of algorithms for ordering a set of numbers. Probably the simplest is the "bubble sort":
https://en.wikipedia.org/wiki/Bubble_sort. The "bubble sort" goes through the list repeatedly, comparing each pair of adjacent numbers, if the larger number comes first, the two numbers are swapped, then goes on to the next number. The repetition repeats until it goes through the entire list without any swaps.

For example if the list is 2, 4, 5, 3 then "bubble sort" looks at the first pair, (2, 4). They are in the correct order so it goes on to (4, 5). They are also in the correct order so it goes on to (5, 3). They are not in the correct order so it swaps them to (3, 5) so the list is now 2, 4, 3, 5. Now, repeat. The first pair, (2, 4), is in the correct order so it goes on to the next pair, (4, 3). They are not in the correct order so they are swapped to (3, 4). The list is now 2, 3, 4, 5. The last pair, (4, 5) is in the correct order so it goes back to the beginning and goes through that list, finding no swaps so terminates.

One nice thing about "bubble sort" is that it can be done "in place". If your original list is A then your sorted list will also be in A. You do not have to define a new array to hold the numbers.


Thanks I figure it out my professor wasnt clear on what he wanted... He wanted to print out array that will have the same number.

HTML:
int tArray(int arr[], int N)
{
	count << endl << endl;
	count << "Generate 5000 numbers: " << endl;
	
	for(int i = 0; i  < N; ++i) 
	{
		arr[i] = 0;
		count << arr[i] << " ";	
	}
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
20
Views
2K
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
22
Views
5K
  • · Replies 66 ·
3
Replies
66
Views
6K