Im still 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 (3×5, 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 my code:
#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(fin,"%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[i]);
printf("The Average values for the five columns are:[%f]", array[j]);
return 0;
}
In text file: 4.33 5.33 1.11 99.00 100.00 1.0 33.3 12.5 1.1 -1000.00 22.1 11.9 2.4 8.3 8.9
The program should output:
The average values for the three rows are: 41.95 -190.42 10.32
The average values for the five columns are: 9.14 16.84 5.33 36.13 -297.7
Having Trouble getting it to do this correctly, any help would be appreciated. I don’t want the answer I want to learn from this but just need some hints. Thank you.
Updated Code:
#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLUMNS 5
float array[ROWS][COLUMNS];
int main(int argc, char* argv[]){
FILE* fin;
int i;
float x;
float** array;
fin = fopen("textfile94", "r");
array=(float**) malloc(ROWS*sizeof(float*));
for(i=0;i<ROWS;i++)
array[ROWS]=(float*)malloc(COLUMNS*sizeof(float));
for(j=0;j<COLUMNS;j++){
fscanf(fin,"%f",&x);
x = array[ROWS][COLUMNS];
}
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;
}
Ok, I’ll see what I can add.
Defines are not written like that, and by convention should be all upper case
He wants you to dynamically allocate the array via malloc, you are now statically allocating a 2-dimensional array of floats and then you try to force arrays of ints into it. You should look up how to do multidimensional arrays with malloc.
Basically what you want is
Now array is a pointer to pointer to float, then assign array rows number of pointers to float.
After that you can for each row assign array[row] with
Now you have your array[ROWS][COLUMNS] structure
One approach to reading in the data in pythonesque pseudo code would be
Tell me if I am being too vague, trying to give hints without giving the code.