Home > OS >  Generating matrix in C language
Generating matrix in C language

Time:05-11

I can't solve simple task to generate matrix with given size. I'm getting too much errors whitch do not understand like: C2087, C2131 or C2447 (Microsoft Visual Studio) Please help me if you can

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
int max = 100;



void random(int & size, int M[][])
{   for (int i = 0; i < size; i  )
    {   for (int j = 0; j < size; j  )
        {
            M[i][j] = rand() % 9;
        }
    }
}

void display(int size, int M[][])
{   for (int i = 0; i < size; i  )
    {   for (int j = 0; j < size; j  )
        {
            printf("%d", &M[i][j]);
        }
    }
}

int main()
{
    printf("give me matrix size\n");
    int size;
    scanf_s("%d", &size);
    //int* M = new int[size][size];
    int M[max][max];

    random(size, M);
    display(size, M);

    return 0;
}

CodePudding user response:

There is an issue with the way that you are handling arrays in c. Arrays are not actually passed around as arrays, but as pointers.

Consider this example

void print_arr (int array[], int length);
void print_arr (int * array, int length);
void print_arr (int array[10], int length);
void print_arr (int array[11], int length);

These four functions, as far as c is concerned, are the same. When you pass an array as a function argument, you are actually just passing a pointer. It works okay because you can access indexes of pointers. When you write

int function (int array[]) {
   return array[3]; 
}

C will take the address of array, add (3 * sizeof(int)), and return that value. It can do this because it knows each element of the array has the size of int.

The problem with this is that you cannot pass two dimensional arrays in c. When you write

void function (int array[][]) {
    array[2][3];
}

C will first calculate the first index. The issue is that it does not know the size to offset the array by. What if your array is 4 by 4? what if it's 7 by 9? Each possible size would require a different offset, and c doesn't know which.

There are a few options. Either write the size in ahead-of-time, like this:

int function (int array[10][10]) {
    return array[2][3];
}

This tells c the size of the array, and lets it correctly calculate offsets.

Or, if you don't know the size ahead of time, manually pass the information in, like this:

int function (int * array, int width) {
    int index = (2 * width)   3;
    return array[index];
}

In this example, you're basically doing the math that c normally does for you, manually.

CodePudding user response:

Thank you Carson. So i tried to make matrix by making long array. Still I have problem with pointers I think.

My errors: Error C2540 non-constant expression as array bound line 30

Error C2440 'initializing': cannot convert from 'int (*)[1]' to 'int *' Line 30

Error C2664 'void random(int,int *)': cannot convert argument 2 from 'int' to 'int *' Line 32

Error C2664 'void wypisz(int,int *)': cannot convert argument 2 from 'int' to 'int *' Line 33

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

void random(int size, int * M)
{

    for (int i = 0; i < size^2; i  )
    {
            int x = rand() % 9;
            M[i] = x;
    }
}

void display(int size, int *M)
{
    for (int i = 0; i < size^2; i  )
    {
        printf("%d ", &M[i]);
        if(size%i==0)
        printf("\n");
    }
}

int main()
{
    printf("Gimme size: \n");
    int size;
    scanf_s("%d", &size);
    int* M = new int[size][size];

    random(size, *M);
    display(size, *M);

    return 0;
}
  • Related