merging and sorting 2d arrays

Aug 28, 2014 at 6:05pm
Hi I'm a beginner and I want to make a program that merges and sorts 2d arrays. I have a program, but when I run it the results are messed up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>

using namespace std;

int main()
{
    int arr [2] [2];
    int ar [2] [2];
    int num;
    int temp [3] [1];
    cout << "enter the numbers in your first array:"<< endl;
    for (int i=0; i<2; i++)
{
        for (int t=0; t<2; t++)
    {
        cout << i << "," << t << ":";
        cin >> arr [i][t];
    }
}
    cout << "enter the numbers in your second array:"<< endl;
    for (int i=0; i<2; i++)
{
        for (int t=0; t<2; t++)
    {
        cout << i << "," << t << ":";
        cin >> ar [i][t];
    }
}

         num=arr[0][1];
    if (arr[0][0]<num){temp[0][0]=num;temp[0][1]=arr[0][0];};
        num=arr[0][1];
    if (arr[0][0]>num){temp[0][0]=arr[0][0];temp[0][1]=num;};
    num=arr[1][0];
    if (arr[1][1]<num){temp[1][0]=num;temp[1][1]=arr[1][1];};
    num=arr[1][0];
    if (arr[1][1]>num){temp[1][0]=arr[1][1];temp[1][1]=num;};
        num=ar[0][1];
    if (ar[0][0]<num){temp[2][0]=num;temp[2][1]=ar[0][0];};
        num=ar[0][1];
    if (ar[0][0]>num){temp[2][0]=ar[0][0];temp[2][1]=num;};
    num=ar[1][0];
    if (ar[1][1]<num){temp[3][0]=num;temp[3][1]=ar[1][1];};
    num=ar[1][0];
    if (ar[1][1]>num){temp[3][0]=ar[1][1];temp[3][1]=num;};

    for (int o=0; o<4; o++)
       {
            for (int d=0; d<2; d++)
             {
                  cout<<temp[o][d]<<",";
              }
        }
     return 0;
}
Last edited on Aug 28, 2014 at 6:43pm
Aug 28, 2014 at 6:21pm
What do you mean by "the results are messed up"? Do they look like a swastika? Are they just 2 decimal places off from the meaning of life? Are they your family's social security numbers but backwards? You need to be specific.
Aug 28, 2014 at 6:37pm
It doubles or triples some numbers when it shows couts
Last edited on Aug 28, 2014 at 6:42pm
Aug 28, 2014 at 7:25pm
Here is a simple program to do what you want.
When you say merges, there are several ways to do that so i just choose 1 for this example.
Also, a 2D array can be sort in many ways. consider a 2d array array[i][j] .
You could sort by major index i or by sub index j .
I choose to sort all elements in the order they are in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <utility>
#include <cstdlib>
using namespace std;

void print(int a[][3],int m);

int main()
{
    int array1[3][3];
    int array2[3][3];

    int arraySum[6][3];

    int k = 0;  //put into array;
    int l = 10;

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            array1[i][j] = ++k;     //fill from 1 to 10
            array2[i][j] = ++l;     //fill from 11 - 19
        }
    }
    /*merge arrays*/
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            (i < 3) ? arraySum[i][j] = array2[i][j] : arraySum[i][j] =  array1[i-3][j];
            //fill arraySum with array2 and append with array1.
            //just so that arraySum does not have any order
        }
    }

    cout<<"Arrays before sorting"<<endl<<endl;
    cout<<"Arary 1: "<<endl;
     print(array1,3);
    cout<<endl<<endl;
    cout<<"Arra2: "<<endl;
    print(array2,3);
    cout<<endl<<endl;
    cout<<"arraySum: "<<endl;
    print(arraySum,6);

        /*sort array with selection sort*/
       /* bubble sort*/
    for(int i = 0; i < 6; i++)
    {
        for(int j = 0; j < 3; j++)
        {
           for(int k = i+1; k < 6; k++)
           {
               for(int m = 0; m < 3; m++)
               {
                   if(arraySum[i][j] > arraySum[k][m])
                   {
                       //swap
                       int temp = arraySum[i][j];
                       arraySum[i][j] = arraySum[k][m];
                       arraySum[k][m] = temp;
                   }
               }
           }
        }
    }
    cout<<"\n\nArray sum after sorting"<<endl<<endl;
    print(arraySum,6);
    cin.ignore();
    return 0;
}

void print(int a[][3],int m)
{
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            cout<<a[i][j]<<"  ";
        }
    }
}
Last edited on Aug 28, 2014 at 7:26pm
Aug 29, 2014 at 1:37am
I don't want it hardcoded
Last edited on Aug 29, 2014 at 1:37am
Topic archived. No new replies allowed.