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] << " ";	
	}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top