## NvIs

I'm trying to learn how to sort arrays. I tried to learn select sort, but I couldn't code it right...
so I tried the easiest, bubble sorting and kind of got the hang of it! But with a few problems and questions.

I'm trying to print an array with random numbers that are assorted. But the numbers even though they are random get stuck at one number after randomly selecting two random numbers. like so:
1239
9999
9999
9999

Theres something wrong or something with the way its sorted thats messing with the way the random numbers are output.
Any help would be appreciated!

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>

using namespace std;

const int ROW = 9;
const int COL = 5;
int array[ROW][COL];

void sortit()// what does the sorting
{
int* p = (int*)array;
int r,c;
for(r = 0; r < ((ROW * COL)-1);r++)
{
for(c = r+1; c < (ROW * COL); c++)
{
if(p[r] > p[c])
{
int hold = p[r];
p[c] = p[c];
p[c] = hold;
}
}
}
}

void reg()//unsorted array
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{

array[i][j]= rand()%10;
cout << array[i][j] << "";
}
cout<<endl;
}cout <<"\n\n\n";
}

int main()
{

srand(time(NULL));
// unsorted array
reg();
// array
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{

array[i][j]= rand()%10;// random numbers
sortit();//Sorts array
cout << array[i][j] << "";// prints array
}
cout<<endl;
}

return 0;
}

## drkybelk

your line 24 is wrong. can you spot it? ;-)

## NvIs

hahahahaha I found it, but then arises another problem.

Its still different random numbers though.
There's four 1's in the first array yet the arranged array displays 5 or six.
Wheres the problem if I need the numbers to be the same?

## drkybelk

I'm not quite sure about your main functionand also why you have 9 x 5 matrix.
The 9 x 5 thing we can ignore for the mo, but in your main function you call the sortit function every time when you enter a new random value. Apart from the complexity it's not a huge problem, but I don't think you intended that.
1) initialise the (2-dim) array with random values.
2) then print the unsorted array
3) do the sorting
4) print again to see whether your sort worked.

The way you do it you will be overwriting already sorted values and also reprinting values who were at a different place before the sort. Follow the above guide and your sort will probably work.

## NvIs

Ah ok I changed some things around or actually just deleted on little thing and cleaned up the program a bit.
What was wrong was I was randomizing the arrays twice which were giving me different numbers when I sorted them. Thank you :)

So I have a question... if I wanted to add up all the rows and columns would I go about doing the same way?

## drkybelk

Don't quite understand what you mean by the "same way", but here what you would need to do:
if you want to accumulate *ALL* values the just initialise a variable with 0 and loop through rows and columns and do sum += array[j]
if you want to have a separate sum for each colum and each row then you need to create 0-initialised arrays, one size 5 and another one size 9 and then again loop through all values of array, but this time add each value to the resultrow and resultcolumn

for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{
resultrow[i] += array[i][j];
resultcolumn[j] += array[i][j];
}
}

## NvIs

Thank you for all the help but I still can't seem to get it to add.
My goal is to try to understand how it works to add to eventually learn how to find the average, mean, and mode...

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
///////////////////////////////////////////////////////////////////////////////////////////////////////
int rows,cols;
cout << "how many rows?";
cin >> rows;
cout << "how many columns";
cin >> cols;
const int ROW = rows;
const int COL = cols;
int array[ROW][COL];

int sum=0;
////////////////////////////////////////////////////////////////////////////////////////////////////////// print unsorted random array
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{

array[i][j]= rand()%10;
cout << array[i][j] << "";
}
cout<<endl;
}cout <<"\n\n\n";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
{

for(int i = 0; i < ROW; i++)// arrayarrayarrayarrayarrayarrayarrayarrayarrayarrayarrayarrayarray
{
for(int j = 0; j < COL; j++)
{
{/////////////////////////////////////////////////////////////////////////////////////////////sort array
int* p = (int*)array;
int r,c;
for(r = 0; r < ((ROW * COL)-1);r++)
{
for(c = r+1; c < (ROW * COL); c++)
{
if(p[r] > p[c])
{
int hold = p[r];
p[r] = p[c];
p[c] = hold;
}
}
}
}// end Sorts array
cout << array[i][j] << "";// prints array
}
cout<<endl;
}
}
for(int i = 0; i < ROW; i++)//add array
{
for(int j = 0; j < COL; j++)
{
sum+= array[i][j];

cout <<sum <<"";
}
}

return 0;
}

## drkybelk

What errors do you get? Does the program compile?

## NvIs

It compiles just fine, but It doesn't add up the rows and columns like I'd like it to.

## drkybelk

your code adds up everything into one sum. You should get a monotonous increasing list of values (ROW*COL - many). trouble is you concatenate them all into one big number your cout << sum <<""; should be cout << sum << endl;

## NvIs

Ok well I adjusted the program to the best of my knowledge, but its still not adding the numbers correctly :(
It will add up one column correctly, but the rest of the numbers are off.

any further help would be appreciated...

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
///////////////////////////////////////////////////////////////////////////////////////////////////////
int rows,cols;
cout << "how many rows?";
cin >> rows;
cout << "how many columns";
cin >> cols;
const int ROW = rows;
const int COL = cols;
int array[ROW][COL];
int sumr[]= {0};
int sumc[]={0};
////////////////////////////////////////////////////////////////////////////////////////////////////////// print unsorted random array
{
for(int i = 0; i < ROW; i++)
{
for(int j = 0; j < COL; j++)
{

array[i][j]= rand()%10;
cout << array[i][j] << "";
}
cout<<endl;
}cout <<"\n\n\n";
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
{

for(int i = 0; i < ROW; i++)// arrayarrayarrayarrayarrayarrayarrayarrayarrayarrayarrayarrayarray
{
for(int j = 0; j < COL; j++)
{
{/////////////////////////////////////////////////////////////////////////////////////////////sort array
int* p = (int*)array;
int r,c;
for(r = 0; r < ((ROW * COL)-1);r++)
{
for(c = r+1; c < (ROW * COL); c++)
{
if(p[r] > p[c])
{
int hold = p[r];
p[r] = p[c];
p[c] = hold;
}
}
}
}// end Sorts array
cout << array[i][j] << "";// prints array
}
cout<<endl;
}
}
for(int i = 0; i < ROW; i++)//add array
{
for(int j = 0; j < COL; j++)
{
sumc[j]+= array[i][j];
sumr[i]+= array[i][j];

}
}
cout <<"the addition of the columns in the array is: "<<endl; // add column
for(int n = 0; n != COL; ++n){
{
cout << sumc[n]<< " ";

}

}
cout <<"\n\nthe addition of the rows in the array are: "<<endl;// add rows
for (int n = 0; n !=ROW; ++n)
{
cout << sumr[n]<< endl;
}

return 0;
}

## drkybelk

int array[ROW][COL];
[B]    int sumr[]= {0};
int sumc[]={0};[/B]

you need to dimension sumr and sumc correctly!

int sumr[ROW]= {0};
int sumc[COL]={0};

## NvIs

Ok I see that know. I got rid of the two ints and just used one simple float sum=0. It doesn't add up all the rows or columns, but instead adds up all the the numbers in the array, then I divided it by how many numbers in the array and I get the average, which was my original intention.

Now I just need help finding the mode. I'm assuming I would need to count the numbers after the array is sorted then get the print out which one came up the most...

## drkybelk

what is the "mode"? median? mean?

## NvIs

like the number that occurs most often is the mode, and the mean is average, and the median is the middle number...
I got the average/mean, but I'm having trouble going about finding the mode and median.
Would I imply some sort of count after looping through the array?