Sorting Arrays

I have to make an array with 20 double digit numbers in it randomly then bubble sort them into a different array. I keep getting errors when I am trying to run it and I am getting negative numbers in my arrays instead of regular numbers.

I assume it's something simple and I am making a dumb mistake.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<cmath>
#include<ctime>

using namespace std;

//constant
const int MAX = 20;

//prototype



int main()
{
	srand(time(NULL));
	//variables
	int original[MAX];
	int sorted[MAX];
	int quantity = 0, sum = 0, x = 0;
	double avg;
	int i, j;
	for (j = 0; j < 20; j++)
	{
		i = rand()%99+10;
			original[i]=i;
	}

	//input


	//calculations
	
	for (i = 0; i <= 19; i++)
	{
		for (j = i + 1; j <= 20; j++)
		{
			int temp;
			if (original[i] > original[j])
			{
				temp = original[i];
				original[i] = original[j];
				original[j] = sorted[j];
			}
		}
	}
	for (i = 0; i <= 19; i++)
	{
		cout << sorted[i];
	}

	//output




	return 0;
}
Last edited on
For a random number from Min to Max, inclusive:

 
rand() % (Max - Min + 1) + Min;

You are also using the wrong array index to load the array. Don't use the random number!

1
2
3
4
const int ArrSize = 20, Min = 10, Max = 99;

    for (int i = 0; i < ArrSize; i++)
        original[i]= rand % (Max - Min + 1) + Min;

Then you should copy it to the "sorted" array and sort that. "original" should not be mentioned in the bubble sort. And remember to use ArrSize and ArrSize-1 instead of 20 and 19.
Hello Shawske,


I keep getting errors when I am trying to run it


The answer is 42.
https://www.youtube.com/watch?v=aboZctrHfK8
https://www.dictionary.com/e/slang/42/

If you are going to keep your errors a secret no one can help.

You sort the original array, but you print out the uninitialized sorted array.

Try changing the for loop that should be under the "input" section to:
1
2
3
4
5
6
7
for (j = 0; j < 20; j++)
	{
		i = rand()%99+10;
		original[i] = i;
                sorted[i] = i;  // <--- Saves on copying later.
	}


Then under the "calculations" heading, which should be "sort" , because you are not calculating anything. Sort the "sorted" array not the original.

But first you could start with:
1
2
3
int original[MAX]{};
int sorted[MAX]{};
double avg{};

The empty {}s will initialize the arrays to all (0) zeros. and the double to "0.0". It is always a good idea to initialize your variables.

A few blank lines in your code make it much easier to read and follow.
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	srand(time(NULL));

	//variables
	int original[MAX];
	int sorted[MAX];
	int quantity = 0, sum = 0, x = 0;
	double avg;
	//int i, j;  // <--- Should be defined in the for loops unless you really need them later.

	for (int j = 0; j < 20; j++)


This should work, but at this moment I have not tested it.

Andy
Topic archived. No new replies allowed.