- #1
Clint_Johnson
- 9
- 0
Homework Statement
Need to write a 1 dimensional array in 2D so I can do operations on a 9 cell average. Here's what I've got so far
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int dataArray[2];
int imax, jmax;
FILE *fp;
char buffer[4];
int dimensions[2];
int i, r, c;
fp = fopen("input.bin", "rb");
if(fp == NULL)
{
printf("Error: File cannot be opened.\n");
return 1;
}
else
{
fread(buffer, sizeof(buffer), 1, fp);
fread(dimensions, sizeof(dimensions), 1, fp);
fread(buffer, sizeof(buffer), 1, fp);
printf("dimensions ( row , col ) ( %d , %d)\n", dimensions[0],dimensions[1]);
int elements = dimensions[0] * dimensions[1];
float myArray[elements];
fread(buffer, sizeof(buffer), 1, fp);
fread(myArray, sizeof(myArray), 1, fp);
fread(buffer, sizeof(buffer), 1, fp);
imax = dimensions[0];
jmax = dimensions[1];
printf(" %d, %d\n", dimensions[0],dimensions[1] );
r = i / dimensions[1];
c = i % dimensions[1];
dataArray[r][c] = myArray[i++];
if
fclose(fp);
return 0;
}
}
I know I need to do something simple with i, i++ to make the array into 2D because this doesn't compile.
Please help, I'm very ignorant when it comes to C
Thanks