Thread: boolean matreix has parity property

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    boolean matreix has parity property

    hi, i have a problem with this, please be sure that this is not to ask for answer, this is to ask for what i want to do

    actually i have written some of the code but i have stuck with ...can someone give me some advice on what should i do. i am not asking for answer...

    this is my code:

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int n;
        
        cout<<"Please enter how many integers per line [ 1 < 100 ]: ";
        cin>>n;
        
        int row = n;
        int col = n;
        int matrix[row][col];
        
    //assign limitation between values
        
        if(n<100 && n>0)
        {
    //initialize and insert values into the matrix   
    
            for(int row = 0; row < n; row++)
            {
                    for(int col = 0; col < n; col++)
                    
                            cin>>matrix[row][col];
            }   
        
    //sum each row
    
            for(int row = 0; row < n; row++)
            {
                int sumRow = 0;
                
                for(int col = 0; col < n; col++)
                {
                    sumRow = sumRow + matrix[row][col];
                    cout<<"sum of row "<<row+1<<" = "<<sumRow<<endl;
                }    
    //sum each column
    
            for(int col = 0; col < n; col++)
            {
                int sumColumn = 0;
                
                for(int row = 0; row < n; row++)
                
                    sumColumn = sumColumn + matrix[row][col];
                    cout<<"sum of column "<<col+1<<" = "<<sumColumn<<endl;
            }
    
    
    //check parity 
    
    
    
        }
        else
        {
            return 1;
        }         
    system("pause");
    return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by gtr_s15
    hi, i have a problem with this, please be sure that this is not to ask for answer, this is to ask for what i want to do

    actually i have written some of the code but i have stuck with ...can someone give me some advice on what should i do. i am not asking for answer...

    this is my code:

    Code:
        int n;
        
        cout<<"Please enter how many integers per line [ 1 < 100 ]: ";
        cin>>n;
        
        int row = n;
        int col = n;
        int matrix[row][col];
    Pick a language that might be either C or C++ for starters. What you have posted is neither C++98 nor C99 but some bast^H^H^H^H extension lying elsewhere.

    Also, you may want to state what you expect an output to be for a specific input.

    [edit]Oh, and you might want to make sure that your curly braces match up.

    ALWAYS USE FULL BRACING. ALWAYS! AND ESPECIALLY WHEN STARTING!!!
    Last edited by Dave_Sinkula; 12-21-2005 at 11:17 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    well it is C++

    the code is aim to reads in a matrix and checks if it has the parity property. if not, the parity property can be established by changing only one bit. if it is not possbile either, the matrix should be classfied as corrupt..

    for example:

    1 0 1 0
    0 0 0 0
    1 1 1 1
    0 1 0 1

    1 0 1 0
    0 0 1 0
    1 1 1 1
    0 1 0 1

    1 0 1 0
    0 1 1 0
    1 1 1 1
    0 1 0 1

    the output :

    OK
    change bit(2,3)
    corrupt

    hope u understand what i mean

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    You can only have constants for array sizes, they can't be variable.
    You will have to use dynamic memory.

    Code:
     int n;
      int *matrix;  
        cout<<"Please enter how many integers per line [ 1 < 100 ]: ";
        cin>>n;
        matrix = new int[n];

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    the code is aim to reads in a matrix and checks if it has the parity property. if not, the parity property can be established by changing only one bit. if it is not possbile either, the matrix should be classfied as corrupt..
    Have you written code to check the matrix for the parity property? To do this, you need to add up each row and column. Try this, then post some code if it's not working.

    I would highly recommend you break up you program into functions. For example make a function to check for parity. This will make it much easier.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    ok i try and will put up my code, thank you all

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    Quote Originally Posted by jlf029
    You can only have constants for array sizes, they can't be variable.
    You will have to use dynamic memory.

    Code:
     int n;
      int *matrix;  
        cout<<"Please enter how many integers per line [ 1 < 100 ]: ";
        cin>>n;
        matrix = new int[n];

    hi, i dont understand this part... can you please explain.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    You cant put variables in arrays unless they are constants.

    const int SIZE = 10;
    int array[SIZE][SIZE];

    or dynamically
    int **matrix;
    int n;
    matrix = new int*[n];
    for(int i=0;i<n;i++)
    matrix[i] = new int[n];

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    thanks but it doesnt work..

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean the dynamic array code? Well, you have to set n to something:
    Code:
    int **matrix;
    int n;
    // set n
    matrix = new int*[n];
    for(int i=0;i<n;i++)
    matrix[i] = new int[n];
    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.

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    Quote Originally Posted by dwks
    You mean the dynamic array code? Well, you have to set n to something:
    Code:
    int **matrix;
    int n;
    // set n
    matrix = new int*[n];
    for(int i=0;i<n;i++)
    matrix[i] = new int[n];

    this works on this code
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int **matrix;
        int n = 4;
    
      
        
        cout<<"Enter: ";
        cin>>n;
        
        matrix = new int*[n];
        for(int i=0;i<n;i++)
        matrix[i] = new int[n];  
        
        cout<<matrix[0];
        
    system("pause");
    return 0;
    }
    but what does this code do as i do this cout<<matrix[0]; ,
    this comes out from the screen 0x3d3af0...

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    Quote Originally Posted by swoopy
    Have you written code to check the matrix for the parity property? To do this, you need to add up each row and column. Try this, then post some code if it's not working.

    I would highly recommend you break up you program into functions. For example make a function to check for parity. This will make it much easier.
    hi this is the code that i make into function but some error, can you please help 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; 
        }   
    }

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to pass your matrix as a parameter as well.
    All you're doing is summing some uninitialised local matrix, not the one you input your data to.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by gtr_s15
    this works on this code
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int **matrix;
        int n = 4;
    
      
        
        cout<<"Enter: ";
        cin>>n;
        
        matrix = new int*[n];
        for(int i=0;i<n;i++)
        matrix[i] = new int[n];  
        
        cout<<matrix[0];
        
    system("pause");
    return 0;
    }
    but what does this code do as i do this cout<<matrix[0]; ,
    this comes out from the screen 0x3d3af0...
    You're printting an address. Try initializing and printing matrix[0][0].
    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.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
        int row;
        int col;
        int matrix[row][col];
    row and col are uninitialized, so that's probably not what you want.
    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.

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