Thread: About an indefinite matrix

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    Question About an indefinite matrix

    Hi to All,

    I need to fill a matrix as this:
    Code:
    char mat [N] [N]
    N is an integer, that I don't know.
    In my program I have a construct for its Input as this.
    Code:
    cin >> N;
    Can work this scenario ?

    For Example can work this program?
    Code:
    #include <iostream.h>
     
    int main()
    {
       int N;  /max element for Matrix
       char mat[N][N];
       int i, j; /Counters
     
       cout << 'Enter the number of Rows and Colums ';
       cin >> N;
       cout << endl;
     
       cout << 'Enter the Matrix elements ' << endl;
     
       for (i=0; i<N; i++)
       {
       for (j=0; j<N; j++)
       cout << mat[i][j];
          cout << endl;
       }
       cout << endl;
     
    }
    This code is correct ? How can resolve my problem ?

    Thank you for help. Be patient with me; I am new in c++ programming.

    Best Regards

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. use <iostream>
    2. use vector of vectors
    3. Check value entered by the user before using it
    4. Don't use variables without initializing them
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    26
    Quote Originally Posted by vart
    2. use vector of vectors
    Hi,
    Thank You for fast reply.
    Please, what is vector of vectors ?
    Have You a sample or a link, in order to learn this concept ?

    Best Regards

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please, what is vector of vectors ?
    In your context, it would be:
    Code:
    std::vector<std::vector<char> > mat;
    You would need to #include <vector>, of course.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    If you still want to use your char array you must make it dynamic otherwise your compiler expects a constant.
    For example:

    Code:
    cin >> N;
    char** mat = new char*[N];
    for(int i=0;i<N;i++) 
        mat[i] = new char[N];

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, 01: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, 06:36 AM