Create a matrix with user-defined dimensions

This is a discussion on Create a matrix with user-defined dimensions within the C Programming forums, part of the General Programming Boards category; Well, I am trying to figure out how I can create a matrix with user-defined dimensions. I do need something ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    9

    Question Create a matrix with user-defined dimensions

    Well, I am trying to figure out how I can create a matrix with user-defined dimensions. I do need something like that since I am programming some numerical methods that involve matrices. So, I made this code as a test:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <alloc.h>
    
    void main ()
    {
      int *table=NULL,n,i;
    
      scanf("%d",&n);
      table=(int *) calloc(n,sizeof(n));
      printf ("Input matrix items:\n");
      for (i=0;i<n;i++) {
        scanf("%d",table[i]);
        }
      for (i=0;i<n;i++) {
        printf ("%d,",table[i]);
        }
    }
    Still it does not work and the output I get is: "0,0,...,0,Null pointer assignment." As I am not very familiar with pointers I suppose that is the problem.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
      int *table,n,i;  
      
      scanf("%d",&n);  
      table=calloc(n,sizeof(n));
      
      printf ("Input matrix items:\n");  
      
      for (i=0;i<n;i++) 
      {
        scanf("%d",&table[i]);    
      }  
      
      for (i=0;i<n;i++) 
      {    
        printf ("%d,",table[i]);
      }
      
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    How could I extend this code so as to enable the user to define a 2-dimensional NxM matrix?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sum of User Defined Matrix Powers
    By QuaX in forum C Programming
    Replies: 1
    Last Post: 04-19-2009, 11:33 PM
  2. Create Sparse Matrix
    By kavka3 in forum C Programming
    Replies: 12
    Last Post: 02-08-2009, 02:23 PM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  4. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM
  5. win32 and user defined classes
    By paulf in forum Windows Programming
    Replies: 4
    Last Post: 04-16-2002, 06:12 PM

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