boolean matreix has parity property

This is a discussion on boolean matreix has parity property within the C++ Programming forums, part of the General Programming Boards category; can some one please fix this error for me .... Code: #include <iostream> #include <cstdlib> using namespace std; int ma(int ...

  1. #16
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    can some one please fix this error for me ....

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int ma(int n);
    
    int main()
    {
        int n;    
        
        cout<<"Enter: ";
        cin>>n;
    
        int matrix[n][n];
    //======================= initialise and insert values ========================= 
        cout << "Enter the matrix by rows\n";  
        for(int row = 0; row < n; row++)
            for(int col = 0; col < n; col++)
            cin >> matrix[row][col];
    //========================= sum of each row ====================================
        cout<<ma(n);
        
    system("pause");
    return 0;
    }
    
    int ma(int n)
    {
        int row;
        int col;
        int matrix[row][col];
    
        
        for(row = 0; row < n; row++)
        {    
            int sumRow = 0;
    
            for(col = 0; col < n; col++)
    
            sumRow = sumRow + matrix[row][col];
            cout<<"sum of row "<<row+1<<" = "<<sumRow<<endl; 
        }   
    }

  2. #17
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    It seems like this has all already been stated but....

    int matrix[n][n];
    You cannot declare an array with variables for size.

    Use new
    Code:
        int **matrix;
        int n;
    
        cout << "Enter: ";
        cin >> n;
    
        matrix = new int*[n];
        for(int i=0;i<n;i++) {
            matrix[i] = new int[n];
        }
    //======================= initialise and insert values =========================
    You will also want to delete this memory when you are done with it.


    You need to pass matrix to your sum function. Currently you are summing an uninialized local array.

    Code:
    int ma(int **matrix, int n)
    {
        int row;
        int col;
    
        for(row = 0; row < n; row++)
        {
            int sumRow = 0;
            for(col = 0; col < n; col++)
                sumRow = sumRow + matrix[row][col];
            cout << "sum of row "<<row+1<<" = "<<sumRow<<endl;
        }
    }
    call ma like
    Code:
    ma(matrix,n)
    there is no need to cout your function call. all printing is done within the function.
    Last edited by spydoor; 12-28-2005 at 07:40 AM.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Actually, I think the OP is trying to pass main()'s matrix to ma(). As an argument.

    And since ma() returns int, make it return a value.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Quote Originally Posted by dwks
    Actually, I think the OP is trying to pass main()'s matrix to ma(). As an argument.
    I agree, that's why I gave him an example of doing exactly that.
    Passing the array and the size. Before he was just passing the size and redefining matrix locally.

    Quote Originally Posted by dwks
    And since ma() returns int, make it return a value.
    I do not know what the intentions are. (It appeared he's trying to output the sum of each row)
    Maybe the function definition is wrong, maybe he forgot to return a value.
    All I know is right now cout<<ma(n) will output garbage.
    Last edited by spydoor; 12-28-2005 at 02:34 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. C++/CLI Property
    By psychopath in forum Windows Programming
    Replies: 5
    Last Post: 07-11-2006, 09:45 PM
  3. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 08:54 AM
  4. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM
  5. Property Sheets :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 05-09-2002, 03:04 PM

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