show matrix

This is a discussion on show matrix within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> int show_matrix(int column, int row, int matrix[column][row]); int main(void) { int m = 2; int ...

  1. #1
    C Newbie
    Join Date
    Oct 2005
    Posts
    13

    show matrix

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int show_matrix(int column, int row, int matrix[column][row]);
    
    int main(void)
    {
    int m = 2;
    
    int matrix[m][m] = { {1,2}, {1,2} };
    
    show_matrix(m,m,matrix);
    return 0;
    }
    
    int show_matrix(int column,int row,int matrix[column][row])
    {
            int m,n;
            for( m = 0; m < column; m++ )
            {
                    printf("\n");
                    for(n = 0; n < row; n++)
                    {
                            printf("%3d",matrix[m][n]);
                    }
            }
    when i compile this,it can run,but it gives me a lot of warning,can someone give me any solution?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about fixing your warnings? Oh, and unless you're compiling this as C99, you can't have your array done that way, which is probably what it's complaining about.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    C Newbie
    Join Date
    Oct 2005
    Posts
    13
    so,without c99,is it possible to use 2d variable array?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. Simulate it with dynamic memory allocation. A quick forum search on the topic should yield lots of code for you to play with.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    C Newbie
    Join Date
    Oct 2005
    Posts
    13
    thanks quzah,i really appreciate ur help...

  6. #6
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    I hav made some modifications:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define m 2
    int show_matrix(int,int,int matrix[m][m]);
    
    int main(void)
    {
    int matrix[m][m] = { {1,2}, {1,2} };
    
    show_matrix(m,m,matrix);
    return 0;
    }
    
    int show_matrix(int row,int coloum,int matrix[m][m])
    {
    	int c,r;
    	for(r=0;r<row;r++)
    	{
    		printf("\n");
    		for(c = 0; c < coloum; c++)
    		{
    			printf("%3d",matrix[r][c]);
    		}
    	}
    	return 0;
    }

    In the function declaration u only need to specify the data type except for a matrix.
    It is a good practice to declare m as a constant using #define.
    and ur row-coloum convension is wrong. The outer loop should vary row and inner loop must vary coloum.

    Finally try to code it more simple.
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 02:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 08:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21