About an indefinite matrix

This is a discussion on About an indefinite matrix within the C++ Programming forums, part of the General Programming Boards category; Hi to All, I need to fill a matrix as this: Code: char mat [N] [N] N is an integer, ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    22,302
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 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