Thread: I need help with this C++ program that adds matrices and edits the result.

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    17

    I need help with this C++ program that adds matrices and edits the result.

    The program adds 2 matrices that are 3x3 using arrays and then stores them into another matrix (array) and then it's edited to show a diagonal line of "0" through it, btw I'm pretty new to programming and this is my first prog.language to learn.

    insert
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int x[3][3],y[3][3],c[3][3],i,j;
        cout<<"Enter your numbers"<<endl;
    
    
    for(i=0;i<=2;i++)       // Entry stage
        for(j=0;j<=2;j++)
        {
            cin>>x[i][j];
            cin>>y[i][j];
        }
     
        
    for(i=0;i<=2;i++)      //Addition stage
        for(j=0;j<=2;j++)
        c[i][j]=y[i][j]+x[i][j];
    
    
    for(i=0;i<=2;i++)
        for(j=0;j<=2;j++)
        {
            if(i=j)
                c[i][j]=0;
        }
    
    
    
    
    for(i=0;i<=2;i++)      // Plotting the zero line and displaying the result
    {
          cout<<endl;
        
       for(j=0;j<=2;j++)
    cout<<c[i][j]<<" ";
       
    }
    return 0;
    }
    It works almost just fine lol, Except that the first portion of the diagonal line does not become zero and instead displays the normal addition result :/, please ,i would be grateful for any help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Check your if statement.
    You're using = where you should be using ==
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    Quote Originally Posted by Salem View Post
    Check your if statement.You're using = where you should be using ==
    Yes, it worked! Why didn't I notice that?! Thank you,but I AM a bit curious about the fact that it did work but made that small mistake even with just an assignment operator instead on an equality operator. Would you be kind enough to tell me why ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It compiled because this:
    Code:
    if(i=j)
        c[i][j]=0;
    amounts to:
    Code:
    i = j;
    if (i)
        c[i][j] = 0;
    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
    Jun 2013
    Posts
    66
    Quote Originally Posted by laserlight View Post
    It compiled because this:
    Code:
    if(i=j)
        c[i][j]=0;
    amounts to:
    Code:
    i = j;
    if (i)
        c[i][j] = 0;
    Which subsequently is evaluated as
    Code:
    i = j;
    if (i != 0)
        c[i][j] = 0;
    Not everyone may be aware of the implicit comparison against 0.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sonjared
    Which subsequently is evaluated as
    Code:
    i = j;
    if (i != 0)
        c[i][j] = 0;
    Not everyone may be aware of the implicit comparison against 0.
    Good clarification, though technically there is an implicit conversion to bool, not an implicit comparison against 0, though the net effect (and probably the translation performed by the compiler) is the same.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c-program on multiplication of matrices using functions
    By dishagarg in forum C Programming
    Replies: 4
    Last Post: 02-28-2013, 11:14 AM
  2. How many edits does it take?
    By VirtualAce in forum General Discussions
    Replies: 15
    Last Post: 09-15-2009, 04:42 PM
  3. Incorrect result with my program
    By JoshR in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 03:46 PM
  4. Edits and buttons
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 05-10-2002, 08:49 AM

Tags for this Thread