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.
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.
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.
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]);
}
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..:
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...
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:
usingnamespace 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++.
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:
#include <vector>
usingnamespace 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.
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.