-2

I've been trying to create a dynamic 2d matrix where the user gives me the size of the matrix along with the input. For example: 3 1 2 3 4 5 6 7 8 9, where the first int 3 is the size of the matrix (3*3) and the rest of the numbers are the numbers I'd like to input in my matrix. Any ideas how to do it?

Nadia
  • 3
  • 3
  • sure, you can use command line arguments or accept user input after the program starts. But if you want "2d" won't the user have to input a width and height? I think some systems will limit the number of command line arguments you can have though (somebody here will know), so you're probably better off reading in user input after the program starts and parsing that yourself. – yano Nov 29 '17 at 19:29
  • read about `malloc` – Aditi Rawat Nov 29 '17 at 19:32
  • my bad. fixed the question. I meant getting 3 that will give me n*n matrix where the rest of the numbers are the input. – Nadia Nov 29 '17 at 19:32
  • @AditiRawat yes, I know about malloc. I don't know how to extract the first number that supposed to be the size of my array. – Nadia Nov 29 '17 at 19:35
  • 1
    Read in the first number using `scanf`; create a 2D-array of ints accoring to the dimensions; write two nested loops and use scanf to read in the values... – Stephan Lechner Nov 29 '17 at 19:36
  • Look at Variable Length Arrays, and read up on string parsing. – Christian Gibbons Nov 29 '17 at 19:36
  • 1
    *"Any ideas?"* Show us what you have tried, and ask why it does not work. Voting to close. – Weather Vane Nov 29 '17 at 19:44
  • oh ok, I didn't know it's wrong to consult if you are stuck. I tried few things but they are bad. If it's against the rules I will delete the thread. – Nadia Nov 29 '17 at 19:50
  • @Nadia: don't mind if the things you tried are bad; show it. You will get comments on your thoughts, and it helps the community to estimate your maturity level when answering the question. – Stephan Lechner Nov 29 '17 at 20:08
  • "I've been trying to create a dynamic 2d matrix" --> posting that code helps to bring clarity to the post. – chux - Reinstate Monica Nov 29 '17 at 20:58

2 Answers2

1

If "dynamic" just means that the size of the 2d-matrix is defined at runtime, then you might use a variable length array. These are created in function scope and are "real" 2D-arrays in the sense that they are n x n continuously stored integers that can be accessed like array[x][y]. See the following code illustrating this approach:

int main() {

    int n;
    if (scanf("%d", &n) != 1 || n <= 0) {
        printf("invalid input.");
        return 1;
    }

    int array[n][n]; // variable length array
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (scanf("%d", &array[i][j]) != 1) {
                printf("invalid input.");
                return 1;
            }
        }
    }

    // matrix is available in main...
    return 0;
}

If "dynamic" means, however, allocated dynamically using malloc, then you have two chances, both not leading to a "real" 2D-array:

First, (dynamically) create an array of pointers, each entry pointing to a (dynamically) allocated array of ints. Advantage: you can access it like arr[x][y]; Drawback: rows are not continguously stored but (probably) spread in memory.

Second, (dynamically) create an array of ints of size n * n; Advantage: values are stored in a continguous block of memory; drawback: you cannot access it like arr[x][y] but rather have to calculate the cell's position on your own, i.e. arr[y * n + x];

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Your line `int array[n][n]; //variable length array` is giving error in the Visual Studio 2017 community edition. **Expression did not evaluate to a constant**. That might because that array declaration expect constant and you are providing a variable. – Siraj Alam Nov 29 '17 at 20:25
  • https://stackoverflow.com/a/19920531/5132337 This explains better of creating of dynamic 2d array – Siraj Alam Nov 29 '17 at 20:27
  • @Siraj The error message "Expression did not evaluate to a constant" is because the compiler used does not support C99 or the optional feature in C11. (was compiler the C++ or C compiler used?) – chux - Reinstate Monica Nov 29 '17 at 21:02
  • The project is chosen to be `Compiled as C` – Siraj Alam Nov 29 '17 at 21:07
  • I think the problem must be in C compiler. – Siraj Alam Nov 29 '17 at 21:11
0

This is what you are looking for. The code is explained in the comments in the code.

Read here, how to make dynamic 2d array in c.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n;

    scanf("%d", &n);

    /* It will allocate an array of pointers with n elements*/
    int **arr = (int**)malloc(n*sizeof(int*));

    /* each element in the above array would contain another array that makes the upper array as the 2D array */
    for (int i = 0; i < n; i++)
        arr[i] = (int*)malloc(n * sizeof(int));

    /*It is taking the input to put inside the array */
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            scanf("%d", &arr[i][j]);

    /* This is printing the content inside the array */
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            printf("%d\t", arr[i][j]);

    return 0;

}
Siraj Alam
  • 9,217
  • 9
  • 53
  • 65