Defining a Matrix ..

This is a discussion on Defining a Matrix .. within the C Programming forums, part of the General Programming Boards category; Hi, first i want user to enter the matrix dimension. User have to define a number cuz of the Matrix ...

  1. #1
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12

    Exclamation Defining a Matrix ..

    Hi, first i want user to enter the matrix dimension. User have to define a number cuz of the Matrix is square. After that i want user to enter rows values row by row. I want it to look like that;

    Enter the 1st row: bla bla
    Enter the 2nd row: bla bla
    .
    .
    Enter the n row: bla bla

    and so on.

    I guess i have to use 'for' to make this but i cant carry out. So how could i do ?

    Thanx in advance..

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

  3. #3
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    Here what i ve done,its not working. it still gets all rows in once.

    Code:
    for(r=0;r<n;r++)
                    {
                    printf("Enter the row \n");
                    for(i=0;i<n;i++)
                    {
                                    for(j=0;j<n;j++)                
                                    scanf("%d",&m1[i][j]);
                                    }
                                    }

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    print "Enter the no. of ROW"
    read ROW
    
    print "Enter no. of COLUMN"
    read COLUMN
    
    for i < ROW
        for j < COLUMN
           read a[i][j]
           
    print "This is what you entered"
    
    for i < ROW
       for j < COLUMN
          print a[i][j]
    That is how you should go ahead on doing this. So can you now take that pesudo code and implement it?

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ssharish2005 View Post
    So can you now take that pesudo code and implement it?
    That pseudo code does not match the OP's specs, I would say!

    Have a look at this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void showmatrix (int *matrix[], int rows, int cols) {   /* generalized function */
            int i, ii;    
            for (i=0; i<rows; i++) {
                    for (ii=0; ii<cols; ii++) printf("%5d", matrix[i][ii]);
                    printf("\n");
            }
    }
    
    int main() {
            int size, **matrix, i, ii; 
            printf("Size of matrix: ");
            scanf("%d",&size);
            matrix = malloc(size*sizeof(int*)); /* allocate for rows of int POINTERS */
            for (i=0; i<size; i++) {
                    matrix[i]=malloc(size*sizeof(int)); /* allocate for columns of INTS */
                    printf("Data for row %d: ",i+1);
                    for (ii=0; ii<size; ii++) {
                            scanf("%d",&matrix[i][ii]);
                    }   
            }
            showmatrix(matrix,size,size);
            return 0;
    }
    Tested, -Wall, works (enter the row data as numbers separated by spaces).

    The most important thing for you to understand is the use of malloc; a dynamic matrix is actually an array of pointers; each pointer represents a row.

    This does not error check the user data!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And the most important thing YOU can learn is to stop doing the whole goddamn thing for people who don't do anything!

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

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by MK27 View Post
    That pseudo code does not match the OP's specs, I would say!

    Have a look at this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    void showmatrix (int *matrix[], int rows, int cols) {   /* generalized function */
            int i, ii;    
            for (i=0; i<rows; i++) {
                    for (ii=0; ii<cols; ii++) printf("%5d", matrix[i][ii]);
                    printf("\n");
            }
    }
     
    int main() {
            int size, **matrix, i, ii; 
            printf("Size of matrix: ");
            scanf("%d",&size);
            matrix = malloc(size*sizeof(int*)); /* allocate for rows of int POINTERS */
            for (i=0; i<size; i++) {
                    matrix[i]=malloc(size*sizeof(int)); /* allocate for columns of INTS */
                    printf("Data for row %d: ",i+1);
                    for (ii=0; ii<size; ii++) {
                            scanf("%d",&matrix[i][ii]);
                    }   
            }
            showmatrix(matrix,size,size);
            return 0;
    }
    Tested, -Wall, works (enter the row data as numbers separated by spaces).

    The most important thing for you to understand is the use of malloc; a dynamic matrix is actually an array of pointers; each pointer represents a row.

    This does not error check the user data!
    And the code which you've is after all a memory leak!

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User QuaX's Avatar
    Join Date
    Apr 2009
    Posts
    12
    I didnt want the whole code. At least i didnt want to mean it, i just stucked at some point and asked for a advice that pseudo code however its not i was asking was highly enough for me. I havent have enough time to continue my project since my post, thanks for answers(Especially MK27). Later im gonna ask a few things more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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