-1

A program I am trying to create must ask for a user input to the size of a matrix, and then read in data from a file to put into said matrix. The only issue is that the information can only be input with pointers and can only use pointer arithmetic. The complete specifications are as follows:

  1. Read in dimensions of a matrix
  2. Prompt for a filename and read in the data from that file into the matrix
  3. Utilize an array of structures, where a single structure holds the mean, median, and standard deviation for a column in the matrix.
  4. All of your code should access the memory locations via pointers and pointer arithmetic.

I was wondering if there is any way to call for the data within the matrix without using array notation (e.g. myArray[0][1])? I have always known to use that notation and have no understanding of how else to seperate the given data without that notation. Any help would be greatly appreciated.

Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Jsosa6
  • 31
  • You can always use pointer notation instead of array notation. For instance, myArray[1] is equivalent to *(myArray +1), myArray[0][1] is equivalent to *(*(myArray + 0) + 1) – Mohiuddin Sohel Sep 09 '15 at 04:12
  • Arrays aren't pointers, but they Cheat and go with one dimensional array. access will be `*(myArray+(row*numColumns+column))`. Simplifies the book-keeping. and makes reading in the matrix stupidly easy. – user4581301 Sep 09 '15 at 05:07
  • Oh and by the way, once you strip off the homework assignment parts and get down to the pointers and arrays issue, this is pretty much a duplicate of http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c – user4581301 Sep 09 '15 at 05:10

1 Answers1

0

You should try to allocate the matrix dynamically. Here's a sample code on how to do that.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int row, col;
    // Get the matrix dimension ...

    int **matrix = (int**) malloc(sizeof(int*) * row);
    for (int i = 0; i < row; i++) {
        matrix[i] = (int*) malloc(sizeof(int) * col);
    }

    // Do something with the matrix ...
    // You could still access the matrix with array notation

    for (int i = 0; i < row; i++) {
        free(matrix[i]);
    }
    free(matrix);

    return 0;
}