Im stil a beginner in C programming and I need a little help writing a code for my C programming class . The prompt is: Input for this program is a two-dimensional array of floating point data located in a file named textfile94. The input array will contain 3 rows of data with each row containing 5 columns of data.
- I want you to use the two-subscript method of dynamic memory allocation.
- Use malloc to create an array that holds pointers.
- Each element of that array points at another array, which is the row of data.
- Use malloc in a loop to create your rows.
- Then you can use two subscript operators [r][c] to get at your data to do the summing and averaging that the program calls for.
This program calls for hard-coded height and width of the 2D array, known ahead of time (3x5, actually). Instead of writing in the literal numbers in your code, I want you to create a global constant variable to hold those dimensions, and use those in your code.
Here is what I have so far (I'm not sure if its correct):
#include <stdio.h>
#include <stdlib.h>
#define int rows = 3;
#define int columns = 5;
float array[rows][columns];
int main(int argc, char* argv[]){
FILE* fin;
float x;
int i,j;
int* array;
fin = fopen("textfile94", "r");
fscanf("%f", &x);
array = (int*) malloc(rows*sizeof(int*));
for(i=0;i<rows;i++){
for(j=0;j<columns;j++)
array[i]=(int*) malloc(columns* sizeof(int));
}
}
printf("The Average values for the three rows are:%f",array[rows]);
printf("The Average values for the five columns are:%f", array[columns]);
return 0;
}