1
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>

using namespace std;
const int AS = 6;
void FillingRandomly(int [AS][AS]);
void printing(int[AS][AS]);
void forsorting(int[][AS], int);
int c;

int main()

{    
    int funny = 0;
    int timpa = 0;
    int counter = 0;
    int Array[AS][AS];
    srand(time(0));

    FillingRandomly(Array);

    cout << "The unsorted array is" << endl << endl;

    printing(Array);

    cout << "The sorted array is" << endl << endl;

    forsorting(Array, funny);
    printing(Array);

    system("PAUSE");

    return 0;

}

void FillingRandomly(int Array[AS][AS])
{    
    for (int i = 0; i<AS; i++)
    {
        for (int j = 0; j<AS; j++)
            Array[i][j] = rand()%87 +12;
    }
}

void printing(int Array[AS][AS])
{    
    int counter = 0;
    for (int i = 0; i<AS; i++)
    {
        for (int j = 0; j<AS; j++)
        {
            cout << setw(5) << Array[i][j];
            counter++;
            if (counter%AS == 0)
                cout << endl << endl;
        }
    }
}

void forsorting(int Array[AS][AS], int funny)
{

    int w=0;
    int dice = 0;
    int Brray[AS*AS];
    int super = 0;
    int space=0;

    //Transofrming Array[][] into Brray[]
    for (int i = 0; i < AS; i++)
    {
        for (int k = 0; k < AS; k++)
        {
            Brray[space] = Array[k][i];
            space++;
        }
    }


    //Bubble sorting in Brray[]

    for (int passer = 0; passer < AS-1; passer++)
    {
        for (int timpa = 0; timpa < AS-1; timpa++)
        {
            if (Brray[timpa]>Brray[timpa + 1])
            {
                super = Brray[timpa];
                Brray[timpa] = Brray[timpa + 1];
                Brray[timpa + 1] = super;
            }
        }
    }

    //Transforming Brray[] into sorted Array[][]

    for (int j=0;j<AS;j++)
        for (int i=0;i<AS;i++)
        {
            Brray[w]=Array[i][j];
        }

    w++;

}

Ok so here's my code. All I need done is the sorting part, Ive written the bubble sorting technique, and i double checked my course and it was the same logic. So what I would like to know is why is my array not sorted when I print it out on the screen.

Thank you for your help

Numeri
  • 1,027
  • 4
  • 14
  • 32
Kauto
  • 17
  • 1
  • 4
  • If you want people to read your code, please take care to lay it out properly. Your indentation is broken, and there is too much whitespaces between lines. – GreenAsJade Jan 25 '14 at 23:30
  • Look at the last part, transforming Brray into sorted Array. Maybe you should assign to Array in stead of Brray in the double for? – Noctua Jan 25 '14 at 23:38
  • Why is this a 2D array instead of a 1D array? – Zac Howland Jan 25 '14 at 23:39

3 Answers3

3

This

Brray[w]=Array[i][j];

is the wrong way around.

You never actually write into Array.

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98
2

One of the issues you have is that you assumed a total size of AS when implementing the bubble sort algorithm, instead of AS*AS. In addition, your implementation does not even check for when the array is already sorted, leading to unnecessary comparison operaations. Try this:

//Bubble sorting in Brray[]
bool sorted;
int len = AS*AS-1;
do
{
    sorted = true;
    for (int timpa = 0; timpa < len; timpa++)
    {
        if (Brray[timpa]>Brray[timpa + 1])
        {
            super = Brray[timpa];
            Brray[timpa] = Brray[timpa + 1];
            Brray[timpa + 1] = super;
            sorted = false;
        }
    }
    len--;
} while (!sorted);

Furthermore, you have switched the order of the assignment operations, when moving the contents back to the 2D array:

int w = 0;
for (int j=0;j<AS;j++) {
    for (int i=0;i<AS;i++)
    {
      Array[i][j] = Brray[w];
    }
    w++;
}

I would also advise you to split the program's logic into more generic and manageable pieces: two functions to reinterpret a 2D array into a 1D array (and vice-versa), and another for the actual sorting. Both could accept arrays of variable sizes, making it applicable to more problems in the same program. This also applies to the function that fills the array with random numbers.

Another useful piece of advice in the case of C++ is to declare variables about where you need them, rather than stacking them up at the beginning of the function. See the w variable in the code above.

E_net4
  • 27,810
  • 13
  • 101
  • 139
-1

There are so many things wrong with this code...

Firstly, your sorting is wrong. This will work

for (int i = 1; i <= AS*AS; i++)
    for (int j = 0; j < (AS*AS - 1); j++)
        if (Brray[j + 1] > Brray[j])  {
            int temp = Brray[j];
            Brray[j] = Brray[j + 1];
            Brray[j + 1] = temp;
        }

Secondly, as others have mentioned, you're not actually updating your 2D array.

for (int j = 0; j < AS; j++)
    for (int i = 0; i < AS; i++)
        Array[j][i] = Brray[w++];

Thirdly, you could have just used std::sort, it's faster than bubble sort, safer, more reliable etc:

std::sort(std::begin(Brray), std::end(Brray));

Fourthly, there are much better alternatives than raw arrays. Use std::vector for example. One of the benefits is that you can have access to .size().

Fifthly, don't use system("pause");. Use std::cin.get(); instead.

Sixthly, rand() is not an accurate way to get uniformly distributed random numbers. Here's a better alternative:

#include <random>

// both min and max inclusive
int rnd(int min, int max) {

    std::random_device rand_dev;                        // create a random device
    std::mt19937 mt(rand_dev());                        // create a mersenne_twister
    std::uniform_int_distribution<int> dist(min, max);  // uniformly distribute between min and max

    return dist(mt);

}

Seventhly, using namespace std; is bad. I will let you do the research to find out why.

There are a few more improvements, but I think there's plenty of work for you already.

Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • What's wrong with `system("pause")` on a Windows system? It gives a message, `cin.get()` doesn't. Moreover - `cin.get()` can work improperly if `cin` has an error set. There's also nothing wrong in `using namespace std` in this context. Yes, I did my research. – Paweł Stawarz Jan 26 '14 at 00:08
  • @PawełStawarz http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong – Oleksiy Jan 26 '14 at 00:10
  • @PawełStawarz, It's slow, its not portable, it's bad style: making a System call should only be done when really necessary. `cin.get()` is better in every way. – Oleksiy Jan 26 '14 at 00:12
  • that link doesn't answer my question. The user clearly doesn't want to distribute his code (he wouldn't use any of these in that case), and I really doubt the speed really matters when calling the command once. There's no bad style in it - that's even said in the link you posted yourself. – Paweł Stawarz Jan 26 '14 at 00:16
  • @PawełStawarz I don't see the point in arguing, but maybe you'll remember this when your professor/lead programmer complains about an unnecessary call to a System, adding ridiculous overhead, causing potential problems and ambiguities, pausing the program's execution and allocating unnecessary resources. All that can be avoided with a `cin.get()`. His teacher might want to run this code on a different OS. It won't work in that case, will it? – Oleksiy Jan 26 '14 at 00:21
  • and I don't see a point in telling someone about bad practice that both doesn't help and is dependant on the users preferences. In normal circumstances you shouldn't use neither `system` nor `cin.get()`. You shouldn't pause the execution **at all**. Also: you still didn't bother to answer about the `using` directive. The author used it in a `*.cpp` file, that's not included anywhere. Nothing wrong with that, it's actually **good** practice when using that directive. – Paweł Stawarz Jan 26 '14 at 00:28