0

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;
}
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Cka91405
  • 165
  • 1
  • 8
  • 17
  • 1
    You are not required need to allocate each integer separately. You can allocate them at once, for example: `int* array; array = malloc(x * sizeof(int));`, then use `array[i]` to access each position. – jweyrich Nov 07 '11 at 19:32
  • How come I don't need the int* before malloc? Aren't I using that as a pointer? This is what my teacher had on the problem also: 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.... – Cka91405 Nov 07 '11 at 19:37
  • Please read the [c-Faq link](http://c-faq.com/~scs/cclass/int/sx9b.html) I provided you before in my comments, it explains this in much detail. – Alok Save Nov 07 '11 at 19:46
  • You down voted me for that wow uncalled for Seriously – Cka91405 Nov 13 '11 at 20:01

1 Answers1

1

Since you already know the dimensions of the array(3x5) you do not need to allocate it dynamically using malloc().
What you have is correct(but you should make it local instead of global):

float array[3][5];

This already allocates enough memory to hold all the array elements.

Also your requirement states:

I want you to create a global constant variable to hold those dimensions, and use those in your code.

Which means you need to declare constants to indicate array elements, something like:

const int rows = 3;
const int columns = 5;

float array[rows][columns];

EDIT:

From your comments, I believe you are using c89(or earlier version).In c99 variable length Arrays(look up VLA on google if you are not aware of this) are allowed,Which means above would compile.But in c98 VLA are not allowed. C89 and earlier versions require using compile-time constant expressions for the array dimension.So you will need to use compile-time constant expressions (which const-qualified variables are not in C). So you will need to use:

#define ROWS 3
#define COLUMNS 5

float array[ROWS][COLUMNS];
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • I understand that part i thought the same thing but my teacher wants it to be global and allocated dynamically and I'm not sure if i did it correctly. – Cka91405 Nov 07 '11 at 19:16
  • @Cka91405: Does it work correctly for you? the allocation part is correct. – Alok Save Nov 07 '11 at 19:19
  • Oh okay I'm starting to understand what you mean by declaring the constants I wasn't sure if that was correct. but I will try that. – Cka91405 Nov 07 '11 at 19:22
  • @Cka91405: I think You need to get your requirements clarified.[C-Faq](http://c-faq.com/~scs/cclass/int/sx9b.html) will be a good read too. – Alok Save Nov 07 '11 at 19:25
  • Okay, will do. Thank you very much for your help, also when i ran the code it said "error: variably modified ‘array’ at file scope" from the float array[rows][columns];.. I'm not to sure what that means. – Cka91405 Nov 07 '11 at 19:33
  • @Cka91405: Check the **EDIT** in the answer. – Alok Save Nov 07 '11 at 19:43
  • Oh okay ive een that in my book, Im not sure if its going to run correctly, with file that is being read in, because its suppose to average out the values for the top row and then print them out then average the values for the columns and print them out. Which does it look like my code is set up correctly to perform that task? – Cka91405 Nov 07 '11 at 19:48
  • @Cka91405: You have bundled too many Questions in here.First, clarify the requirement of what exactly needs to be done,Without a clear problem statement you can do nothing.Next The above is enough to get you started, write the code,test it,if it fails try to see what fails,why is it not working,debug a bit,If you are still stuck ask a separate Q here,with clear concise summary of what you want to achieve and what you have till that point of time.It is Homework so you should try and learn from it as much as you,and unless you try you won't.So pls try but ask Q's only when you are blocked. – Alok Save Nov 07 '11 at 19:52
  • @Cka91405: No! Not this one. Editing it now, renders all the answers useless to anyone who sees this in future.You should start a new Q. – Alok Save Nov 07 '11 at 19:55
  • Oh Okay! SORRY! Ill put a new question. – Cka91405 Nov 07 '11 at 20:19