I have to read in a huge array of numbers from a file as if it were stdin.
The first two numbers in the file are the array dimensions. Then the numbers in the array are below.
Like this:
244 456
33 56 43 7 97 336 56
323 789 64 569 4
...
and so on.
I am able to read in the numbers and print them out just fine.
I am also able to print out the array horizontally and vertically with this code:
Horizontal:
Code:
array[x][y];
for (i=0; i<y; i++){
for(j=x-1; j>=0; j--){
printf("%d\n", array[j][i]);
}
j=x-1;
}
Vertical:
Code:
for (i=y-1; i>=0; i--){
for(j=0; j<x; j++){
printf("%d\n", array[j][i]);
}
j=0;
}
However, this is for school and they want me to do it another way.
For the horizontal part, they want us to malloc() a row's worth of pixels.
Im guessing is this how you do it?
Code:
int **row;
row = (int **) malloc(sizeof(int *)*x);
Then I probably read in each number of that row, and immediately print it out backwards in the same loop right? I dont know how to even read in something from malloc so .. can someone please help?
For the vertical part, I need to allocate an array of row (int *)'s. Then initialize each one of those (int *)'s by calling malloc() to allocate an array of column ints. Read each row of pixels into one of these arrays, and then print them out backwards.
Is this done like this?
Code:
int **col;
col = (int **) malloc(sizeof(int *)*x);
for (i = 0; i < x; i++) {
col[i] = (int *) malloc(sizeof(int)*y);
}
If not can someone correct me please.. Im really lost when it comes to malloc.