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

This is a discussion on I need help with this C++ program that adds matrices and edits the result. within the C++ Programming forums, part of the General Programming Boards category; The program adds 2 matrices that are 3x3 using arrays and then stores them into another matrix (array) and then ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,632
    It compiled because this:
    Code:
    if(i=j)
        c[i][j]=0;
    amounts to:
    Code:
    i = j;
    if (i)
        c[i][j] = 0;
    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
    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
    21,632
    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.
    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

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


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