I'm having a hard time understanding how to sort loops and the different types.
I've read several articles, reviewed several programs, and still cannot grasp how to sort arrays let alone do anything else with them, I.E. add rows/columns, find the mode, ect...

I know the different types of sorts, selection, bubble, and from what I've seen, it seems bubble sorting is the easiest, but I still don't understand how its done. I'm not looking for someone to 'do' a program for me, but rather just help me grasp it through examples.

here is a basic program that asks a user to input rows and columns, and displays it accordingly with random numbers between 1-9.
How do you start the sorting code? Which is more effective or universal for sorting?
and lastly how would you go about getting the program identifying numbers in the array and having them multiply, add, find median, mode, least occurring ect...

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

using namespace std;

int main()
{
int put, puts;

cout << "how manny columns?"<<endl;
cin >> puts;
cout << "how many rows?"<<endl;
cin >> put;

int ROW = put;
int COL = puts;
int array[ROW][COL];

srand(time(NULL));
for(int r = 0; r < ROW; r++) //row
{
for(int c = 0; c < COL; c++)//column
{
array[r][c] = rand()%10;//setting numbers in array to random
cout<<array[r][c]<< " ";//print array
}
cout<<endl;
}

return 0;
}``````

## All 6 Replies

I have searched, I need specific examples in C++ I understand what they do, but its how everything works when sorting them. I've read articles as I've stated before...

I haven't found that website, the sorting part was useful, but I'm still not getting how does the coding assort the array.

this is from what I understand with the example given on the website, I did this quickly because I have work in 30 so bare with me please. I used his program as an example:

``````#include <cstdlib>
#include <ctime>
#include <iostream>
#include <windows.h>
using namespace std;

int main() {

srand(time(NULL));
int x;
cout << "how many"<<endl;
cin >> x;
int big = x;
srand(time(NULL));
int iaArray[rand()%10];
int iLength = big;

// Insertion sort
for (int iInsert = 1; iInsert < iLength; ++iInsert) {
while (iInsert > 0 && iaArray[iInsert - 1] > iaArray[iInsert]) {
int iSwap        = iaArray[iInsert];
iaArray[iInsert]    = iaArray[iInsert - 1];
iaArray[iInsert - 1]    = iSwap;
--iInsert;
}
}

for (int iIndex = 0; iIndex < iLength; ++iIndex) {
cout << iaArray[iIndex] << "  ";
}
cout << endl;

return 0;
}``````

I had to switch the random initializer with his example 1d array to the top when defining the array:

``````srand(time(NULL));
int x;
cout << "how many"<<endl;
cin >> x;
int big = x;
srand(time(NULL));
int iaArray[rand()%10];
int iLength = big;``````

instead of leaving at the bottom like my previous 2d array because its defining the array being random to late and would not be sorted through the sorting code...

``````srand(time(NULL));
for(int r = 0; r < ROW; r++)
{
for(int c = 0; c < COL; c++)
{
array[r][c] = rand()%10;
cout<<array[r][c]<< " ";
}
cout<<endl;``````

My first question is when the program runs, the random numbers are huge! not between 1 and 10 like I wanted, why is that?

My next question is since my program is a 2d array, how would I ajust the soring part?

First question: `int iaArray[rand()%10];` doesn't initialize the array elements so what you see is junk.
Second question: How do you want to sort it? I can't imagine how do you define a sorted matrix.

Be a part of the DaniWeb community

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