Andrés HM
Andrés HM

Reputation: 1

how do returns a matrix in C (include code)

i defined a matrix into a function. how do i return that matrix for print it when i call it with another function. i mean...

#include<stdio.h>
#include<conio.h>
#include<time.h>

void main() {
    int m,n;
    printf("type 2 numbers:");
    scanf("%i %i",&m,&n);
    declaration(m,n);\\HERE IS THE PROBLEM
    printing(matrix,m,n);
    getch();
}

void declaration(int a,int b) {
    srand(time(NULL));
    int i,j,matrix[a][b];
    for(i=0;i<a;i++){
        for(j=0;j<b;j++){
            matrix[i][j]=1+rand()%7;
        }
    }
}

void printing(int c[100][100],int a,int b) {
    int i,j;
    for(i=0;i<a;i++){
        for(j=0;j<b;j++){
            printf("%i\t",c[i][j]);
        }
        printf("\n");
    }
}

Upvotes: 0

Views: 101

Answers (3)

Srikanth Chekuri
Srikanth Chekuri

Reputation: 2254

Another simple way to do it is use double pointer to create 2-dimensional array. Keep it simple.

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

int** create_matrix(int rows, int cols) {
    int **matrix = malloc(rows*(sizeof(int *)));
    for(int i = 0; i < rows; i++) {
        matrix[i] = malloc(cols*sizeof(int));
    }
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            matrix[i][j] = 1 + rand()%7;
        }
    }
    return matrix;
}

void printing(int** matrix, int rows, int cols) {
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int rows, cols;
    rows = 3, cols = 3;
    int** matrix = create_matrix(rows, cols);
    printing(matrix, rows, cols);
    free(matrix);
    return 0;
}

Upvotes: 0

fnisi
fnisi

Reputation: 1223

You have a few points that require a bit more attention;

1 ) read warning and error messages given by your compiler

2 ) again, read warning messages given by your compiler

3 ) use indentation to make your code more readable.

4 ) Always return from main(), that's a good practice

The code below does what you want to achieve; have a look at it and keep on reading...

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

// You either have to declare your functions 
// or implement them before main()
void declaration(int a,int b, int m[a][b]);
void printing(int a,int b,  int m[a][b]);

int main(){ // always return from main()
  int m,n;
  printf("type 2 numbers:");
  scanf("%i %i",&m,&n);
  int matrix[m][n];

  declaration(m, n, matrix);
  printing(m, n, matrix);

  return 0;
}

void declaration(int a,int b, int m[a][b]){
  srand(time(NULL));
  int i,j;

  for(i=0;i<a;i++){
    for(j=0;j<b;j++){
        m[i][j]=1+rand()%7;
    }
  }
}

void printing(int a,int b, int m[a][b]){
  int i,j;
  for(i=0;i<a;i++){
    for(j=0;j<b;j++){
        printf("%i\t",m[i][j]);
    }
    printf("\n");
  }
} 

You need a way to transfer data from one function to another. You cannot simply declare an auto variable in one function and pass it to another as you did in the code below

declaration(m,n);
printing(matrix,m,n); /* where does matrix[][] come from? */

remember, C is a strongly typed language which means you have to declare your variables before using them. This applies to your functions as well. You either have to give your function declarations before main() (or more specifically, before using them), or implement them.

Look into your header files (i.e. .h files) and you will see lots of function declarations.

Since you use variable length arrays, make sure your compiler is at least capable of compiling code confirming C99 standard.

Some extras;

Normally, C passes arguments by value and you have to use a pointer if you want the value of your variable get changed within the function. If you have a close look at the code snippet I gave, I simply used an int m[a][b].In C, the name of an array is a pointer to its first element, hence you can change the value of array elements when actually array's name is passed to your function as an argument.

For further reading, you may want to look at

  • variable scope
  • global variables (you can define matrix[][] as a global variable and change the value of matrix elements)
  • declaration vs definition in C

Upvotes: 2

Joy Allen
Joy Allen

Reputation: 402

Define it like:

typedef struct {
    int rows;
    int cols;
    int *data;
} int_matrix_entity, *int_matrix;

int_matrix int_matrix_create(int rows, int cols, bool rand)
{
    int_matrix mt;
    int i;
    if ((mt = malloc(sizeof(int_matrix_entity))) == NULL)
    {
        return NULL;
    }

    if ((mt->data = malloc(sizeof(int) * cols * rows)) == NULL)
    {
        free(mt);
        return NULL;
    }

    if (rand)
    {
        srand(time(NULL));
        for (i = 0; i < cols * rows; i++)
        {
            mt->data[i] = 1 + rand() % 7;
        }   
    }
    else
    {
        memset(mt->data, 0, sizeof(int) * cols * rows);
    }

    return mt;
}

void int_matrix_printf(int_matrix mt)
{
    int i;
    int j;
    for (i = 0; i < mt->rows; i++)
    {
        for (j = 0; j < mt->cols; j++)
        {
            printf("%5d ", mt[i * cols + j]);
        }
        printf("\n");
    }
}

Upvotes: 2

Related Questions