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;
}

Recommended Answers

All 16 Replies

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

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?

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.
Your steps should be:
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.

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?

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];
    }
}

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;
}

What errors do you get? Does the program compile?

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

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;

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;
}
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};

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...
Not exactly sure how to go about this, but any help would be appreciated :)

what is the "mode"? median? mean?

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?

You already sorted your array. The median is the element in the middle of your array.
The size of your array is ROW*COL with both ROW and COL > 0, then the median is the element at index [ROW*COL/2]. You have to think what you want to do if ROW*COL is odd or even, but that's it.
Since your array is sorted, finding the mode is easy: iterate through the array after initialising the modewith the first (smallest) element. You need two counters

int workCount = 0;
int maxCount = 0;

in your loop you use the workCount to count the number of times the smallest value is in your array. That's easy coz they are all at the start of your array. Stop counting when you find the first element that is not equal to it's predecessor. the new maxCount is your current workCount. Then reset the workCount to 0 and count the second smallest values in your array. They are also all consecutive coz you got a sorted array. at the end compare the workCount with the maxCount. if the workCount>maxCount then your new mode is the second element and your new maxCount is the current workCount, other wise leave them as they are. Then count 3rd smallest element and so on...
I don't write the code for you so you can practise ;-)

Thats wonderful! great input! I definitely don't expect you to write code, but just exactly what you said was perfect! Thanks I'm gonna implement those things as soon as I have time :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.