Why is my array mostly zeros?

I want to flip my input vertically, but for some reason, my output is mostly zeros.
Note: 17 7 in input are column and row respectively.

Here is my input:
1
2
3
4
5
6
7
8
9
10
P2
17 7
255
37  44  37  37  44  37  44  44  44  37  44  44  48  48  48  44  37
34  33  34  37  33  34  37  48  44  44  44  48  44  44  44  44  53
44  44  37  44  53  53  49  44  41  37  41  41  33  33  29  26  26
29  29  33  37  37  44  48  53  53  57  53  57  61  64  69  69  81
81  77  81  81  73  77  72  48  37  33  33  33  37  37  48  44  33
22  22  25  22  22  14  14  14  14  22  22  22  22  22  22  22  22
22  22  26  22  22  22  22  18  22  18  22  18  18  18  22  26  22


My code:
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char **argv)
{
    if(argc != 3)
    {
        printf("Incorrect number of arguments.\n");
        exit(1);
    }
    //Open files
    FILE *fin;
    fin = fopen(argv[1], "r");
    if(fin ==NULL)
    {
        printf("File: %s does not exist\n", argv[1]);
        exit(1);
    }
    FILE *fout;
    fout = fopen(argv[2], "w");
    if(fout ==NULL)
    {
        printf("File: %s does not exist\n", argv[2]);
        exit(1);
    }

    int i=0;
    char p2[3];                     //P2
    int row_quantity;               //Number of rows
    int column_quantity;            //Number of columns
    int current_row;                //Current row
    int current_column;             //Current column
    int pixel;                      //255

    //Read in non pixels
    fscanf(fin, "%s", p2);
    fscanf(fin, "%d", &column_quantity);
    fscanf(fin, "%d", &row_quantity);
    fscanf(fin, "%d", &pixel);

    //Print out non-pixels
    fprintf(fout, "%s\n", p2);
    fprintf(fout, "%d %d\n", column_quantity, row_quantity);
    fprintf(fout, "%d\n", pixel);
    //Allocate memory
    int **array = (int **)malloc(sizeof(int*) *(row_quantity));

    //Read in pixels
    for(current_row = 0; current_row < row_quantity; current_row++)
    {
        for(current_column =0; current_column < column_quantity; current_column++)
        {
            array[current_column] = (int *)malloc(sizeof(int) *(column_quantity));
            fscanf(fin, "%d", &array[current_row][current_column]);
        }
    }
    //Print with vertical flip
    for(current_row = row_quantity-1; current_row >= 0; current_row--)
    {
        for(current_column = 0; current_column < column_quantity; current_column++)
        {
            fprintf(fout, "%d ", array[current_row][current_column]);
        }
        fprintf(fout, "\n");
    }
    return 0;
}


And my output:
1
2
3
4
5
6
7
8
9
10
P2
17 7
255
0 0 0 0 0 0 22 18 22 18 22 18 18 18 22 26 22
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

You aren't allocating the memory correctly.

int** is a pointer to an array of pointers. This implies you want to perform 1 allocation for the array of pointers,
and then 1 allocation for each column == a total of #columns + 1 allocations.

You are allocating once on line 47 and then #rows * #columns times on line 54.
Aha! Thank you very much kind sir. I must admit I am horrid at memory allocation. It is much appreciated.

I just removed line 54 and placed array[current_row] = (int*)malloc(sizeof(int) *(column_quantity +1)); in the first for loop and that fixed it.
Topic archived. No new replies allowed.