Thread: Sorting Two Dimensional Arrays

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Sorting Two Dimensional Arrays

    I've made two C programs.
    The first program writes a binary table to 'sales.dat'.
    The second program is what I have a problem with.
    I want to sort those numbers in two ways by reading the binary file created by the first program:
    1. I want to display the top three number in each of the 12 rows.
    (I was thinking of using selection sort for that, but I can't quite get it.)
    2. I want to add every number in each column and display the top three sums and its product name.

    Please help.

    Here is the table I'm trying to base this program of:
    Code:
               Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    Note A 34  56 27 100 74 56 12 5 87 29 63 16
    Note B 9 11 14 55 30 4 47 150 136 26 18 12
    Note C 46 3 22 8 78 14 52 27 35 80 123 90
    Ballpoin
    t pen A 58 38 79 120 85 52 39 23 31 10 10 46
    Ballpoin
    t pen B 8 5 3 8 2 21 7 3 8 13 8 7
    Ballpoin
    t pen C 26 32 28 26 24 20 19 22 23 28 30 29
    Pencil 12 19 54 51 41 36 20 28 3 5 7 32
    Marker 22 14 53 21 25 23 22 180 31 6 213 58
    Pencil
    case 10 14 43 12 13 17 14 12 11 9 54 15
    Erase A 21 20 28 17 19 14 13 12 17 18 14 11
    Erase B 24 21 13 24 20 16 13 17 24 38 23 28
    The program that writes the file:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main(){
    
            FILE* fp;
            char filename[]="sales.dat";
            int a[11] [12] = {
                            {34, 56, 27, 100, 74, 56, 12, 5, 87, 29, 63, 16},
                            {9, 11, 14, 55, 30, 4, 47, 150, 136, 26, 18, 12},
                            {46, 3, 22, 8, 78, 14, 52, 27, 35, 80, 123, 90},
                            {58, 38, 79, 120, 85, 52, 39, 23, 31, 10, 10, 46},
                            {8, 5, 3, 8, 2, 21, 7, 3, 8, 13, 8, 7},
                            {26, 32, 28, 26, 24, 20, 19, 22, 23, 28, 30, 29},
                            {12, 19, 54, 51, 41, 36, 20, 28, 3, 5, 7, 32},
                            {22, 14, 53, 21, 25, 23, 22, 180, 31, 6, 213, 58},
                            {10, 14, 43, 12, 13, 17, 14, 12, 11, 9, 54, 15},
                            {21, 20, 28, 17, 19, 14, 13, 12, 17, 18, 14, 11},
                            {24, 21, 13, 24, 20, 16, 13, 17, 24, 38, 23, 28} };
    
            if(!(fp=fopen(filename, "wb"))){
                            printf("Can't open a file\n");
                            return -1;
                    }
    
            if(fwrite(a, sizeof(int), 132, fp)!= 132){
                    fclose(fp);
                    printf("Can't write\n");
                    return -1;
            }
            printf("Success to write!\n");
            fclose(fp);
    }
    The program I am having trouble with:
    Code:
    #include<stdio.h>
    
    int main(){
    
            FILE *fp;
            char filename[]="sales.dat";
            int a[11] [12];
            int k[12];
            int sum[12];
            char str[11] [50] = {"Note A", "Note B", "Note C", "Ballpoint pen A", "Ballpoint pen B", "Ballpoint pen C", 
                                    "Pencil", "Marker", "Pencil case", "Erase A", "Erase B"};
            int i, j, tmp, min;
    
            if(!(fp=fopen(filename, "rb"))){
                    printf("Can't open a file\n");
                    return -1;
            }
            if(fread(a, sizeof(int), 132, fp)!=132){
                    fclose(fp);
                    printf("Can't read\n");
                    return -1;
            }
            printf("Success to read!\n");
            fclose(fp);
    
            for(i=0;i<11;i++){
                    for(j=0;j<12;j++){
                            printf("%5d", a[i] [j]);
                    }
                    printf("\n");
            }
    
            for(i=0;i<12;i++){
                    min = i;
                    for(j=i+1;j<11;j++){
                            if(a[0] [min]>a[0] [j]){
                                    tmp = a[0] [min];
                                    a[0] [min] = a[0] [i];
                                    a[0] [i] = tmp;
                            }
                    }
            }
    
    
            for(i=0;i<12;i++){
                    for(j=0;j<11;j++){
                            sum[i]+=a[i] [j];
                            printf("%5d", a[i] [j]);
                    }
                    printf("\n");
            }
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Hufnagel!

    What is the connection between the data? The first table seems to have each record with it's record fields across one row.

    In the second program it's just data. What's the relationship between the two tables?

    Could you show what output you're trying to get, from a 3 row example of the data and records? Your description doesn't make it clear to me.

    Normally, if you want to show sorted output two or more different ways, you would either copy the original (to preserve the order it has), and then sort the duplicate table (or create a table of pointers, and sort "through" the pointers, or a separate index table).

    Using a duplicate table is easiest, an index table is and perhaps easier, but using pointers is more fun.

    We can help you with the sorting for sure, but I'm not clear what you want for your final output.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by hufnagel View Post
    The first program writes a binary table to 'sales.dat'.
    I want to sort those numbers in two ways by reading the binary file created by the first program:
    1. I want to display the top three number in each of the 12 rows.
    (I was thinking of using selection sort for that, but I can't quite get it.)
    2. I want to add every number in each column and display the top three sums and its product name.
    Two things to be aware of:
    1. When a 2d-array is declared as MyArray[y][x], we say that MyArray has y rows and x columns.
    2. C treats a 2d-array as if it is actually a 1d-array where each element is itself a row.

    Sorting MyArray so that each row is in increasing order is straightforward. Let's call this operation arrsortrows(MyArray). To sort by column, here is a simple method:

    1. Transpose MyArray into MyArray_T
    2. Perform arrsortrows(MyArray_T)
    3. Transpose MyArray_T into MyArray

    Performing these operations in C is straightforward but tedious. Better to use functions. You should be able to adapt this example to solve your problem:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // arrprint(y, x, a[y][x]): print array a in tabular format
    void arrprint(int y, int x, int a[y][x]);
    
    // arrcopy(y, x, a, b): copy elements of a into b
    void arrcopy(int y, int x, int a[y][x], int b[y][x]);
    
    // arrtrans(y, x, a, aT): transpose a into aT
    void arrtrans(int y, int x, int a[y][x], int aT[x][y]);
    
    // arrsort(n, a[n]): sort a numerically elementwise
    void arrsort(int n, int a[n]);
    
    // arrsortrows(y, x, a[y][x]): sort a row-by-row
    void arrsortrows(int y, int x, int a[y][x]);
    
    // arrsortcolumns(y, x, a[y][x]): sort a column-by-column
    void arrsortcolumns(int y, int x, int a[y][x]);
    
    int main(int argc, char *argv[])
    {
            int orig[11] [12] = {
                            {34, 56, 27, 100, 74, 56, 12, 5, 87, 29, 63, 16},
                            {9, 11, 14, 55, 30, 4, 47, 150, 136, 26, 18, 12},
                            {46, 3, 22, 8, 78, 14, 52, 27, 35, 80, 123, 90},
                            {58, 38, 79, 120, 85, 52, 39, 23, 31, 10, 10, 46},
                            {8, 5, 3, 8, 2, 21, 7, 3, 8, 13, 8, 7},
                            {26, 32, 28, 26, 24, 20, 19, 22, 23, 28, 30, 29},
                            {12, 19, 54, 51, 41, 36, 20, 28, 3, 5, 7, 32},
                            {22, 14, 53, 21, 25, 23, 22, 180, 31, 6, 213, 58},
                            {10, 14, 43, 12, 13, 17, 14, 12, 11, 9, 54, 15},
                            {21, 20, 28, 17, 19, 14, 13, 12, 17, 18, 14, 11},
                            {24, 21, 13, 24, 20, 16, 13, 17, 24, 38, 23, 28} };
    
        printf("Original array:\n");
        arrprint(11, 12, orig);
        
        printf("\nSorted by month (columnwise):\n");
        int bymonth[11][12];
        arrcopy(11, 12, orig, bymonth);
        arrsortcolumns(11, 12, bymonth);
        arrprint(11, 12, bymonth);
        
        printf("\nSorted by product (rowwise):\n");
        int byproduct[11][12];
        arrcopy(11, 12, orig, byproduct);
        arrsortrows(11, 12, byproduct);
        arrprint(11, 12, byproduct);
        
        return 0;
    }
    
    // numcmp: return negative number if a < b, nonnegative otherwise
    static inline int numcmp(const void*a, const void *b) {
        return *(int *)a - *(int *)b;
    }
    
    void arrprint(int y, int x, int a[y][x]) {
        for (int i=0; i < y; i++) {
            for (int j=0; j < x; j++)
                printf("%3d ", a[i][j]);
            printf("\n");
        }
    }
    
    void arrcopy(int y, int x, int a[y][x], int b[y][x]) {
        for (int i=0; i < y; i++)
            for (int j=0; j < x; j++)
                b[i][j] = a[i][j];
    }
    
    void arrtrans(int y, int x, int a[y][x], int aT[x][y]) {
        for (int i=0; i < y; i++)
            for (int j=0; j < x; j++)
                aT[j][i] = a[i][j];
    }
    
    void arrsort(int n, int a[n]) {
        qsort(a, n, sizeof(int), numcmp);
    }
    
    void arrsortrows(int y, int x, int a[y][x]) {
        for (int i=0; i < y; i++)
            arrsort(x, a[i]);
    }
    
    void arrsortcolumns(int y, int x, int a[y][x]) {
        int aT[x][y];
        arrtrans(y, x, a, aT);
        arrsortrows(x, y, aT);
        arrtrans(x, y, aT, a);
    }
    Last edited by c99tutorial; 11-10-2012 at 03:02 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    2. C treats a 2d-array as if it is actually a 1d-array where each element is itself a row
    Not quite. C 2-d arrays (and in fact all multidimensional arrays) are flat, one-dimensional arrays. 2-d arrays can have their elements accessed with the formula X * Y + Z where X is a row, Y is the size of the row dimension, and Z is the column. The compiler simply does the math for you if you use array-style access. And you have to do some setup if you want array-style access for multidimensional dynamic arrays.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    11
    Quote Originally Posted by Adak View Post
    Welcome to the forum, Hufnagel!

    What is the connection between the data? The first table seems to have each record with it's record fields across one row.

    In the second program it's just data. What's the relationship between the two tables?

    Could you show what output you're trying to get, from a 3 row example of the data and records? Your description doesn't make it clear to me.

    Normally, if you want to show sorted output two or more different ways, you would either copy the original (to preserve the order it has), and then sort the duplicate table (or create a table of pointers, and sort "through" the pointers, or a separate index table).

    Using a duplicate table is easiest, an index table is and perhaps easier, but using pointers is more fun.

    We can help you with the sorting for sure, but I'm not clear what you want for your final output.
    This is what I'm trying to display:
    PHP Code:
    The top 3 sales for Jan is
    Ballpoint pen A 58
    Note C 46
    Note A 34
    The top 3 sales 
    for Feb is
    [product name] [number sold]
    and 
    so on for every month... 
    The other one would be like this:
    PHP Code:
    The top 3 products sold over the year are:
    [
    product name] [total of numbers in column]
    and 
    so on for the latter 2... 
    I made a little bit of progress before looking at the replies:
    I was able to sort each row in an increasing order,
    not quite there with adding the columns though
    Code:
    #include<stdio.h>
    
    int main(){
    
            FILE *fp;
            char filename[]="sales.dat";
            int a[11] [12];
            char str[11] [50] = {"Note A", "Note B", "Note C", "Ballpoint pen A", "Ballpoint pen B", "Ballpoint pen C", 
                                    "Pencil", "Marker", "Pencil case", "Erase A", "Erase B"};
            int h, i, j, k, tmp, min, sum;
    
            if(!(fp=fopen(filename, "rb"))){
                    printf("Can't open a file\n");
                    return -1;
            }
            if(fread(a, sizeof(int), 132, fp)!=132){
                    fclose(fp);
                    printf("Can't read\n");
                    return -1;
            }
            printf("Success to read!\n");
            fclose(fp);
    
            for(i=0;i<11;i++){
                    for(j=0;j<12;j++){
                            printf("%5d", a[i] [j]);
                    }
                    printf("\n");
        }
        
    
            for(i=0;i<12;i++){
                sum+=a[0] [i];
            }
            printf("%d\n", sum);
        
        
        for(h=0;h<12;h++){
            for(i=0;i<10;i++){
                min = a[i] [h];
                k = i;
                for(j=i+1;j<11;j++){
                    if(a[j] [h] < min){
                        min = a[j] [h];
                        k = j;
                    }
                }
                tmp = a[i] [h];
                a[i] [h] = a[k] [h];
                a[k] [h] = tmp;
            }
        }
    
        
        for(i=0;i<11;i++){
            for(j=0;j<12;j++){
                            printf("%5d", a[i] [j]);
                    }
                    printf("\n");
        }
    
        for(i=0;i<11;i++){
            min = a[10] [i];
            k = i;
            for(j=i+1;j<12;j++){
                if(a[10] [j] < min){
                    min = a[10] [j];
                    k = j;
                }
            }
            tmp = a[10] [i];
            a[10] [i] = a[10] [k];
            a[10] [k] = tmp;
        }
    
        for(i=0;i<11;i++){
            for(j=0;j<12;j++){
                            printf("%5d", a[i] [j]);
                    }
                    printf("\n");
        }
    
        
    
    }
    I do understand your point in saving the original data table, and messing around with the copy instead of the original. I'm not sure in how to do that though.
    Also, how would I make it so the months and product names would be included and move with the modifications? Is that something that I should do with the first program?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Your array as defined has only integers (the number of sales). To keep the other data together you should use a struct rather than array of ints. For example:

    ProductEntry
    month : string
    product : string
    sales : int

    With a proper definition, we can simply change `int' to `ProductEntry' and the procedure works as before. Sort the array copy and then print the relevant result.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    // ProductEntry
    typedef struct {
        char month[4];
        char product[50];
        int sales;
    } ProductEntry;
    
    // Y: number of rows (products)
    // X: number of cols (months)
    #define Y 11
    #define X 12
    
    // Index->name mappings
    const char PRODSTR[Y][50] = {"Note A", "Note B", "Note C", "Ballpoint pen A", "Ballpoint pen B", "Ballpoint pen C",
                                    "Pencil", "Marker", "Pencil case", "Erase A", "Erase B"};
    const char MONTHSTR[X][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
    
    // Create an initialized ProductEntry data[][] by using an int numbers[][] as input
    void ProductEntry_create(int y, int x, ProductEntry data[y][x], int numbers[y][x]);
    
    // arrprint...(...) : array printing functions
    void arrprintheader(void);
    void arrprintall(int y, int x, ProductEntry a[y][x]);
    void arrprintmonth3(int y, int x, ProductEntry a[y][x], int month_index);
    void arrprintproduct3(int y, int x, ProductEntry a[y][x], int product_index);
    
    // arrcopy(y, x, a, b): copy elements of a into b
    // arrtrans(y, x, a, aT): transpose a into aT
    void arrcopy(int y, int x, ProductEntry a[y][x], ProductEntry b[y][x]);
    void arrtrans(int y, int x, ProductEntry a[y][x], ProductEntry aT[x][y]);
    
    // arrsort, arrsortrows, arrsortcolumns: array sorting
    void arrsort(int n, ProductEntry a[n]);
    void arrsortrows(int y, int x, ProductEntry a[y][x]);
    void arrsortcolumns(int y, int x, ProductEntry a[y][x]);
    
    int main(int argc, char *argv[])
    {
        int rawnumbers[Y] [X] = {
                            {34, 56, 27, 100, 74, 56, 12, 5, 87, 29, 63, 16},
                            {9, 11, 14, 55, 30, 4, 47, 150, 136, 26, 18, 12},
                            {46, 3, 22, 8, 78, 14, 52, 27, 35, 80, 123, 90},
                            {58, 38, 79, 120, 85, 52, 39, 23, 31, 10, 10, 46},
                            {8, 5, 3, 8, 2, 21, 7, 3, 8, 13, 8, 7},
                            {26, 32, 28, 26, 24, 20, 19, 22, 23, 28, 30, 29},
                            {12, 19, 54, 51, 41, 36, 20, 28, 3, 5, 7, 32},
                            {22, 14, 53, 21, 25, 23, 22, 180, 31, 6, 213, 58},
                            {10, 14, 43, 12, 13, 17, 14, 12, 11, 9, 54, 15},
                            {21, 20, 28, 17, 19, 14, 13, 12, 17, 18, 14, 11},
                            {24, 21, 13, 24, 20, 16, 13, 17, 24, 38, 23, 28} };
        
        // Initialize rawdata table
        ProductEntry rawdata[Y][X];
        ProductEntry_create(Y, X, rawdata, rawnumbers);
        
        printf("\nTop sales by month:\n");
        ProductEntry bymonth[Y][X];
        arrcopy(Y, X, rawdata, bymonth);
        arrsortcolumns(Y, X, bymonth);
        arrprintheader();
        for(int monthid=0; monthid <= X-1; monthid++)
            arrprintmonth3(Y, X, bymonth, monthid);
        
        printf("\nTop sales sorted by product:\n");
        ProductEntry byproduct[Y][X];
        arrcopy(Y, X, rawdata, byproduct);
        arrsortrows(Y, X, byproduct);
        arrprintheader();
        for (int prodid=0; prodid <= Y-1; prodid++) 
            arrprintproduct3(Y, X, byproduct, prodid);
        
        return 0;
    }
    
    void ProductEntry_create(int y, int x, ProductEntry data[y][x], int numbers[y][x]) {
        for (int i=0; i < y; i++)
            for (int j=0; j < x; j++) {
                strcpy(data[i][j].month, MONTHSTR[j]);
                strcpy(data[i][j].product, PRODSTR[i]);
                data[i][j].sales = numbers[i][j];
            }
    }
    
    void arrprintheader(void) {
        printf("%-5s %-20s %-5s\n", "---", "-------------", "-----");
        printf("%-5s %-20s %-5s\n", "Mon", "Product name", "Sales");
        printf("%-5s %-20s %-5s\n", "---", "-------------", "-----");
    }
    
    void arrprintall(int y, int x, ProductEntry a[y][x]) {
        arrprintheader();
        for (int i=0; i < y; i++) {
            for (int j=0; j < x; j++) {
                printf("%-5s %-20s %-5d\n", a[i][j].month, a[i][j].product, a[i][j].sales);
            }
        }
    }
    
    void arrprintmonth3(int y, int x, ProductEntry a[y][x], int mo) {
        for (int i=0; i < 3; i++) {
            printf("%-5s %-20s %-5d\n", a[i][mo].month, a[i][mo].product, a[i][mo].sales);
        }
    }
    
    void arrprintproduct3(int y, int x, ProductEntry a[y][x], int pr) {
        for (int i=0; i < 3; i++) {
            printf("%-5s %-20s %-5d\n", a[pr][i].month, a[pr][i].product, a[pr][i].sales);
        }
    }
    
    void arrcopy(int y, int x, ProductEntry a[y][x], ProductEntry b[y][x]) {
        for (int i=0; i < y; i++)
            for (int j=0; j < x; j++)
                b[i][j] = a[i][j];
    }
    
    void arrtrans(int y, int x, ProductEntry a[y][x], ProductEntry aT[x][y]) {
        for (int i=0; i < y; i++)
            for (int j=0; j < x; j++)
                aT[j][i] = a[i][j];
    }
    
    static inline int ProductEntry_sort_desc(const void*a, const void *b) {
        const ProductEntry *x = a;
        const ProductEntry *y = b;
        return y->sales - x->sales;
    }
    
    void arrsort(int n, ProductEntry a[n]) {
        qsort(a, n, sizeof(a[0]), ProductEntry_sort_desc);
    }
    
    void arrsortrows(int y, int x, ProductEntry a[y][x]) {
        for (int i=0; i < y; i++)
            arrsort(x, a[i]);
    }
    
    void arrsortcolumns(int y, int x, ProductEntry a[y][x]) {
        ProductEntry aT[x][y];
        arrtrans(y, x, a, aT);
        arrsortrows(x, y, aT);
        arrtrans(x, y, aT, a);
    }
    Last edited by c99tutorial; 11-10-2012 at 10:13 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a text file version of some of what you're doing, using structs.

    Code:
    /*Sorts, sums, and displays this data, by row and col 
    Item Name       Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    ==================================================================
    Notepad A,       34  56  27 100  74  56  12   5  87  29  63  16
    Notepad B,        9  11  14  55  30   4  47 150 136  26  18  12
    Notepad C,       46   3  22   8  78  14  52  27  35  80 123  90
    Ballpoint pen A, 58  38  79 120  85  52  39  23  31  10  10  46
    Ballpoint pen B,  8   5  38  22   1   7  3    8   1   3   8   7
    Ballpoint pen C, 26  32  28  26  24  20  19  22  23  28  30  29
    Pencil,          12  19  54  51  41  36  20  28   3   5   7  32
    Marker,          22  14  53  21  25  23  22  180 31   6 213  58
    Pencil case,     10  14  43  12  13  17  14  12  11   9  54  15
    Eraser A,        21  20  28  17  19  14  13  12  17  18  14  11
    Eraser B,        24  21  13  24  20  16  13  17  24  38  23  28
    */
    
    #include <stdio.h>
    
    #define SIZE 128
    
    typedef struct {
       char name[20];
       int moAmt[12];
    }Sales;
    
    void sortRows(Sales *sales, int n);
    void printIt(Sales *sales, int n);
    void sum(Sales *sales, int n, char *header,int *sumrow,int *sumcol);
    
    int main(void) {
       int i=0,j,n=0; 
       char ch;
       char header[79]={"Item Name      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec  Sum of Row\n"};
       int sumrow[11], sumcol[12];
    
       Sales sales[30];
       char line[SIZE];
       FILE *fp=fopen("salesData.txt","r");
       if(!fp){
          printf("Error, unable to open file\n");
          return 0;
       }
       while(fgets(line, SIZE, fp)) {
          //printf("%s\n",line);
          
          sscanf(line,"%[^','], %d %d %d %d %d %d %d %d %d %d %d %d*c",sales[i].name,
          &sales[i].moAmt[0],&sales[i].moAmt[1],&sales[i].moAmt[2],&sales[i].moAmt[3],
          &sales[i].moAmt[4],&sales[i].moAmt[5],&sales[i].moAmt[6],&sales[i].moAmt[7],
          &sales[i].moAmt[8],&sales[i].moAmt[9],&sales[i].moAmt[10],&sales[i].moAmt[11]),
          &ch;
          
          printf("%20s, ",sales[i].name);
          for(j=0;j<12;j++) {
             printf("%3d ",sales[i].moAmt[j]);
          }
          printf("\n");
          ++i;
       }
       fclose(fp);
       printf("i: %d\n",i);
       n=i;
       printf("\n\nBefore insertionSort\n\n");
       for(i=0;i<n;i++) {
          printf("%20s, ",sales[i].name);
          for(j=0;j<n;j++) {
             printf("%3d ",sales[i].moAmt[j]);
          }
          printf("\n");  
       }
       sortRows(sales,i); //sort by first Month sales ascending
       printIt(sales,i);
       printf("\n\n");
       
       sum(sales,i,header,sumrow,sumcol );
    
    /*   
       sortCols(sales, i);
       sumRows(sales, i);
       sumCols(sales, i);
    */
    
       printf("\n");
       return 0;
    }
    void sum(Sales *sales, int n,char *header,int *sumrow, int *sumcol) {
       int r,c;
       
       printf("     %s\n",header);
       printf("===============================================================================\n");
       for(r=0;r<n;r++) {
          printf("%19s ",sales[r].name);
          sumrow[r]=0;
          for(c=0;c<12;c++) {
             printf("%3d ",sales[r].moAmt[c]);
             sumrow[r]+=sales[r].moAmt[c];
          }
          printf("   %4d\n",sumrow[r]);
       }
       printf("===============================================================================\n");
       printf("                    ");
       for(c=0;c<12;c++) {
          sumcol[c]=0;
          for(r=0;r<n;r++) {
             sumcol[c]+=sales[r].moAmt[c]; 
          }
          printf("%3d ",sumcol[c]);
       }
    }
    
    
    void sortRows(Sales *sales, int n) {
       int i, j=0; 
       Sales val; 
       printf("Start: \n");
       for(i=1;i<n;i++) {  
          val = sales[i]; //sales[i].moAmt[0];
          j = i-1;
          while(sales[j].moAmt[0] > val.moAmt[0]) { 
             sales[j+1] = sales[j];
             --j;
             if(j<0) break;
          }   
          sales[j+1] = val;
       }
    } 
    
    /* sum() has the updated print out format. They'll be moved to this
    function later.
    */
    void printIt(Sales *sales, int n) {
       int i,j;
       printf("\n");
       for(i=0;i<n;i++) {
          printf("%19s, ",sales[i].name);
          for(j=0;j<12;j++) {
             printf("%3d ",sales[i].moAmt[j]);
          }
          printf("\n");
       }
       printf("\n");
    }
    After sum() it looks like this:
    Sorting Two Dimensional Arrays-tableprogram-png

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting two dimensional arrays
    By ejjjjj in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 11:38 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  4. help with sorting d-dimensional points
    By acosgaya in forum C++ Programming
    Replies: 6
    Last Post: 06-26-2006, 12:00 PM
  5. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM