Random Array of 100 numbers ordered from lowest to highest

Pages: 12
I want to make a C++ program that can take an array of 100 random numbers, print out a 10 by 10 block of them, then print out another 10 by 10 block of those same random numbers but order them from lowest to highest.

I've used a bubble sort method, and two functions, the "int main" function was used as one of the functions.




You used a bubble sort? So the array is already sorted from lowest to highest? In that case, all you need to do is output it in 10x10 blocks.

To print out 10x10 block:
1
2
3
4
5
6
7
8
int arr[100] {0};
for(int i = 0; i < 100; i++)
{
     if(i % 10 == 0) 
     std::cout << '\n';

     std::cout << arr[i];
}


Do it once while it's unsorted and then again when it is sorted. Are you having issues sorting the array form lowest to highest values?
[code]if(i % 10 == 0) trips on zero and there isn't any spacing there.

there are 20 ways to do it, the direct approach:
string s = " ";
for(int z = 0; z < 100; )
cout << arr[z++]<<s<< … repeat 10 times total << endl;

you can also do double for loops to print, or the above idea and
(i % 10 == 0 && i)


and so on.
Last edited on
Seems like slight overkill to me. Mine "trips" on 0, but that can easily be fixed by adding an extra condition (along with a hundred other ways): if ((i != 0) && i % 10 == 0). Though, personally, I think it's convenient having it output a new line at the start when you've probably been outputting things previously. Spacing can be easily fixed with: std::cout << arr[i] << ' ';

It was just some basic code. The OP tweaking is expected.
I used two for loops a int main () function and a in random() that I had passed a gloabal array a[10][10];
the for loops looked like for (i=0; i<=9; i++)
for (j=0; j<=9; j++)
Would be easier if you just uploaded your code? And don't forget to use code tags if you do:
[.code][./code] - Remove the periods and put your code between them.

Are those for loops to output or for sorting?
Using namespace std;
Int a[10][10];
Int random(int a [][10], int n){
Int I, j, min, temp, z, swap, k, l;
For (i=0; i<=9; i++)
{
For (j=0; j<=9; j++)
{
If (a[i][[j]>a[i][i+j])
{
swap=a[i][j];
a[i][j]=[i][j+1];
a[i][j+1]=swap;
}
}
}
Return(a[10][10]);
}
Int main ()
{
Int I, j, k;
Int n=sizeof(a)/sizeof(a[0]);
Int n=sizeof(a)/sizeof(a[0]);
srand(time(NULL));

for (i=0; i<=9: i++)
{
for {j=0; j<=9; J++)
{
a[i][j]=rand()%(101-3)+1;
random(a,n);
cout <<setw(6)<<a[i][j]<<setw(6);
}
Cout <<endl;
}
Cout<endl;
Cout<<endl;
For (i=0; i<=9; i++)
{
For (j=0;j<=9; j++) {
a[i][j]=1+rand()%(100-1)+1;
cout<<setw(6) <<a[i][j]<<setw(6);
}
Cout<<endl;
}
System(“pause”);
}

some errors capital letters ect; sorry
I believe the for loops are for output, not sure if they are involved with sorting, but the output produces a number of zeros before the actual numbers start, so if the box was 10 by 10, half of the box is zeros then sorted numbers.

I used a bubble sort method for the random numbers, in the code above you can see it here :

If (a[i][[j]>a[i][i+j])
{
swap=a[i][j];
a[i][j]=[i][j+1];
a[i][j+1]=swap;
}

not having issues with it sorting from lowest to highest zapshe (472).
my issue is with the zeros before hand. is there a way to tweak the limites of the for loop to decrease the zeros, or is the bubble sort incorrect?
Did you type this into the text box yourself rather than copy and paste? Are you using a good text editor/IDE to write your code? I highly recommend you use Visual Studio if you're not already.

I did my best to put your code snippets together and fix them up..:

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
using namespace std;
int a[10][10];
int random(int a[][10], int n) 
{
	int i, j, min, temp, z, swap, k, l;
	for(int i = 0; i <= 9; i++)
	{
		for(int j = 0; j <= 9; j++)
		{
			if(a[i][j] > a[i][i + j])
			{
				swap = a[i][j];
			//	a[i][j] = [i][j + 1]; //???
				a[i][j + 1] = swap;
			}
		}
	}
	return(a[10][10]);
}

int main()
{
	int i, j, k;
	int n = sizeof(a) / sizeof(a[0]);
	srand(time(NULL));

	for (i = 0; i <= 9; i++)
	{
		for (j = 0; j <= 9; j++)
		{
			a[i][j] = rand() % (101 - 3) + 1;
			random(a, n);
			cout << setw(6) << a[i][j] << setw(6);
		}
	}

	cout << endl;
	cout << endl;
	cout << endl;
	for (i = 0; i <= 9; i++)
	{
		for (j = 0; j <= 9; j++) 
		{
			a[i][j] = 1 + rand() % (100 - 1) + 1;
			cout << setw(6) << a[i][j] << setw(6);
		}
		cout << endl;
	}
	system("pause");
}


Your question is whether or not you can remove the zeroes at the beginning of the array?

^ https://www.geeksforgeeks.org/remove-leading-zeros-from-an-array/

If you're allowed, a vector would be easier to manage in this way.
Last edited on
I appreaciate it, I am typing it into word, transferring it from a picture because im at work and wife cannot use visual studio but it is in visual studio...

yes and that's what I was trying to do... correct
and I believe this is a duplicate

int n = sizeof(a) / sizeof(a[0]);
EDIT: Fixed the duplicate. If this is for school they may not allow vectors, but if you change out the arrays for vectors removing leading 0s is a lot easier.

This code is a bit round-about but it'll get the job done:

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
using namespace std;

void remove(int arr[], int &count)
{
	for (int i = 0; i < 100; i++) //Output to see the array before changing it
	{
		std::cout << arr[i] << ' ';
	}

	for (int i = 0; i < 100; i++) //Run through the array
	{
		if (arr[i] == 0) //If 0, do nothing
		{
		}
		else //Otherwise save the value in the array starting from the beginning onward
		{
			arr[count] = arr[i];
			count++;
		}
	}
	std::cout << "\n\n";
	for (int i = 0; i < count; i++) //Output array with no leading 0s
	{
		std::cout << arr[i] << ' ';
	}
}


int main()
{
	int arr[100]{0};
	int count = 0;

	for (int i = 20; i < 100; i++) //Just to fill the array with stuff - First 20 values are 0s
		arr[i] = (i+12);

	remove(arr, count);
}


The issue with this code is that it wont resize the array, you'll have to use the value "count" as the new parameter for when going through your altered array. So if you want to use this, you'll have the variable count rather than the array's actual size as the parameter when sifting through the array.

If you weren't using Visual Studio, you could use VLAs and make a new array with the new size on the fly, but it's sadly not standard C++.
Last edited on
this looks brilliant!

I will study this and see how it works out. It is for school yes and im sure vectors would be aloud although we did not cover it, but how else are you suppose to get it done.

I will check this out later when I have some downtime. I thank you a bunch
No problem, I added some comments to make it more helpful.

With vectors, you can use a multitude of built in functions to not only more easily get rid of the leading 0s, but also resize the vector so you don't have "extra" values at the end. More than that, a vector always knows it's own size so no need for the little trick where you divide the sizes. With a vector, it would look 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
36
#include <vector>
using namespace std;

void remove(vector<int> arr)
{
	for (int i = 0; i < 100; i++) //Output vector before changing
	{
		std::cout << arr[i] << ' ';
	}
	while(arr[0] == 0)
	{
		arr.erase(arr.begin()); //Delete the first element. arr[1] becomes the new value for arr[0] and so on
	}
	std::cout << "\n\n";
	for (int i = 0; i < arr.size(); i++) //output changed vector
	{
		std::cout << arr[i] << ' ';
	}
}


int main()
{
	vector<int> arr;
	
	int count = 0;

	for (int i = 20; i < 100; i++) //Naive way to fill Vector with 100 values - first 20 are 0s
	{
		arr.resize(i + 1); //Make the Vector Bigger To Hold Another Value
		arr[i] = (i + 12);
	}

	remove(arr);

}


Notice how you can always just call the .size() function to get the size of the vector. Also, be weary using .resize(), it allocates memory and so looping it the I've done (which was to simply fill the vector) will be time-consuming.
Last edited on
take an array of 100 random numbers

but
my issue is with the zeros before hand.

makes no sense. Random numbers are random. If they can be zeros, then they can be zeros. Therefore, zeros are not special and not an issue.

What "issue" do you actually have?



Functions are great for dividing a task into simple steps. The main could be very "clear":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <algorithm>

void getdata( int data[], size_t elems );
void print( int data[], size_t elems, size_t columns );

int main()
{
  constexpr size_t N {100};
  int arr[N] {};

  getdata( arr, N );
  print( arr, N, 10 );
  std::sort( arr, arr+N ); // you could call your bubble-sort here
  print( arr, N, 10 );
}

That is logically very different from your original, where you have merged getdata, print, and sort in:
1
2
3
4
5
6
7
8
9
for ( row = 0; row < 10; +row )
{
  for ( col = 0; col < 10; ++col )
  {
    // set value for block[row][col]
    // sort entire block
    // show "sorted" value of block[row][col]
  }
}

That makes no sense.
You should not sort before you have shown the original random data.

You cannot sort all values before you have all values.
Calling the sort inside loops means that you sort 100 times.
What you do is that you sort some random values and some zeros and end up showing those zeros for the first half of the block.
Here is simple working example using standard:

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
62
63
64
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>

typedef unsigned short USHORT;

//
// Generates random numbers for given range
//
inline USHORT Generate(USHORT start, USHORT end)
{
	static std::random_device rand_dev;
	static std::uniform_int_distribution<USHORT> dist(start, end);
	static std::mt19937 eng(rand_dev());

	return dist(eng);
}

//
// Print vector contents into 10 x 10 table
//
void Print(std::vector<USHORT>& vec)
{
	const size_t size = vec.size();
	const size_t chunks = size / 10;

	for (size_t i = 0; i < size; i++)
	{
		if (i % chunks)
		{
			std::cout << vec.at(i) << "\t";
		}
		else
		{
			std::cout << std::endl;
		}
	}
}

int main()
{
	std::vector<USHORT> numbers;

	// fill vector with 100 random numbers
	for (size_t i = 0; i < 100; i++)
	{
		numbers.push_back(Generate(1, 100));
	}

	// Print original (unsorted) contents
	std::cout <<std::endl << "ORIGINAL CONTENTS";
	Print(numbers);

	// sort numbers
	std::sort(numbers.begin(), numbers.end());

	// print sorted numbers
	std::cout << std::endl << "SORTED CONTENTS";
	Print(numbers);

	std::cin.get();
	return 0;
}
Last edited on
Pages: 12