CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Nov 2014
    Posts
    13

    Matrix 4x4 in C++

    Here are my homeworks , print out the matrix code into the screen in C++
    1/
    1 0 0 0
    0 1 0 0
    0 0 1 0
    0 0 0 1
    2/
    1 1 1 1
    0 1 1 1
    0 0 1 1
    0 0 0 1
    3/
    2 3 5
    19 23 7
    17 13 11

    Tks in advance

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Matrix 4x4 in C++

    Cool. Have fun.

  3. #3
    Join Date
    Nov 2014
    Posts
    13

    Re: Matrix 4x4 in C++

    Plz help me .

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Matrix 4x4 in C++

    Cool! Hope you figure it out; keep us updated!
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix 4x4 in C++

    What problems are you encountering doing this? What code have got so far?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Nov 2014
    Posts
    13

    Re: Matrix 4x4 in C++

    I have no idea how to do all of those exercises so i posted it here . Could u guys mind send me a tutorial link for me ,tks

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Matrix 4x4 in C++

    Well, do you know how to define and access arrays, especially by looping over them? I suspect that your assignment is really about using them, since it is exceedingly trivial to complete your homework otherwise, e.g.,
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "1 0 0 0\n"
                     "0 1 0 0\n"
                     "0 0 1 0\n"
                     "0 0 0 1\n";
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix 4x4 in C++

    Could u guys mind send me a tutorial link for me ,tks
    http://www.learncpp.com/
    http://www.cplusplus.com/doc/tutorial/

    What book has been suggested by your tutor for you to use?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Matrix 4x4 in C++

    Quote Originally Posted by Ripery View Post
    I have no idea how to do all of those exercises so i posted it here . Could u guys mind send me a tutorial link for me ,tks
    Didn't your class come with a text book?

  10. #10
    Join Date
    Nov 2014
    Posts
    13

    Re: Matrix 4x4 in C++

    Tks all . I have done all my homeworks
    Here is my code for exercise 1

    Code:
    //Matrix-Cross
    #include<iostream>
    using namespace std; 
    int main() 
    {
     int i,  j; 
     for(i=1;i<=4;i++)
       { 
                      for(j=1;j<=4;j++)
       { 
                       if(i>j)
                       {
                       cout<<0<<" "; 
                       } 
    if(i==j) 
    { 
             cout<<1<<" "; 
    } 
    if(i<j)
    {
    cout<<0<<" "; 
    }
       }
      cout<<endl;
       }
        system("pause");
        return 0;
    }
    Last edited by Ripery; November 30th, 2014 at 05:24 AM.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix 4x4 in C++

    This code is inefficient. You are using multiple if statements without else statements. You first check for i greater than j and output '0'. You then check for i equals j and output '1'. Finally you check for i less than j and output a '0'. If i is not greater than j and i is not equal to j then i must be less than j. Therefore you are only displaying a '1' if i equals j and a '0' when i is not equal to j.
    Code:
    if (i == j )
         cout << 1 << " ";
    else
         cout << 0 << " ";
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Nov 2014
    Posts
    13

    Re: Matrix 4x4 in C++

    @2kaud: Tks very much . I got it

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix 4x4 in C++

    Quote Originally Posted by Ripery View Post
    @2kaud: Tks very much . I got it
    So displaying arrays 2) and 3) should now be easy!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Nov 2014
    Posts
    13

    Re: Matrix 4x4 in C++

    Could u plz check this too . I m not quite understand this code cuz this is my friend 's code . For example , the "w", what is it for ?

    Code:
    //Matrix-Prime Number Zigzag
    #include<iostream>
    #include<iomanip>
    using namespace std;
    bool prime(int number)
    { 
           int i=1,count=0;
         
           while(number>=i) 
             { 
             if(number%i==0)
                 { 
                     count++;
                 }
                     i++; 
             } 
             if(count==2) 
             return true; 
             else 
             return false; 
    }
    
    int main()
    {
          int i,j,n,w;
          w=2;
          int matrix[50][50];
          
          do
         {
              cout<<"Enter the size of matrix (n >=3 ): "<<endl;
              cin>>n;
         }
              while(!(n>=3));
              
        for(int i=0;i<n;i++)
        {
         if(i%2==0)
          {
          for(int j=0;j<n;j++)
          {
           if(prime(w)==true) 
           matrix[i][j] = w ;
           else 
           j--;
           w++;
          }
         }
         else
         {
          for(int j=n-1;j>=0;j--)
         {
           if(prime(w)==true)
           matrix[i][j] = w ;
           else
           j++;
           w++;
         }
         }
         }
              
              for(i=0;i<n;i++)
              {
              for(j=0;j<n;j++)
              {
                  cout<<matrix[i][j]<<setw(3);
              }
                   cout<<"\n";
              }
             system("pause");
             return 0;
    }
    Last edited by Ripery; November 30th, 2014 at 05:19 AM.

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Matrix 4x4 in C++

    When posting code, please use code tags and not quote tags. Go Advanced, select the formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured