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

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
  • Tags Tags
    Array Numbers
AI Thread Summary
The discussion centers on generating and sorting an array of 500 numbers in C++. Initially, the user seeks clarification on how to order randomly generated numbers. The suggested solution involves using the "bubble sort" algorithm, which repeatedly compares and swaps adjacent numbers until the list is sorted. The algorithm operates in place, meaning the sorted numbers can be stored in the same array without needing a new one. Ultimately, the user resolves their confusion, realizing their professor wanted them to print an array filled with zeros instead of sorting random numbers.
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

cout <<  "Generate 500 random numbers:" << endl;
	const int nnn = 500;
	int numberOfArray3[nnn];
		for (i = 0; i < nnn; ++i) {
			numberOfArray3[i] = rand() % 100;
			cout << 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)
{
	cout << endl << endl;
	cout << "Generate 5000 numbers: " << endl;
	
	for(int i = 0; i  < N; ++i) 
	{
		arr[i] = 0;
		cout << arr[i] << " ";	
	}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top