-
November 29th, 2014, 11:55 AM
#1
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
-
November 29th, 2014, 12:08 PM
#2
-
November 29th, 2014, 12:09 PM
#3
Re: Matrix 4x4 in C++
Plz help me .
-
November 29th, 2014, 12:09 PM
#4
Re: Matrix 4x4 in C++
Cool! Hope you figure it out; keep us updated!
-
November 29th, 2014, 12:15 PM
#5
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)
-
November 29th, 2014, 12:25 PM
#6
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
-
November 29th, 2014, 12:33 PM
#7
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";
}
-
November 29th, 2014, 01:50 PM
#8
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)
-
November 29th, 2014, 02:02 PM
#9
Re: Matrix 4x4 in C++
Originally Posted by Ripery
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?
-
November 30th, 2014, 12:18 AM
#10
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.
-
November 30th, 2014, 04:59 AM
#11
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)
-
November 30th, 2014, 05:04 AM
#12
Re: Matrix 4x4 in C++
@2kaud: Tks very much . I got it
-
November 30th, 2014, 05:08 AM
#13
Re: Matrix 4x4 in C++
Originally Posted by Ripery
@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)
-
November 30th, 2014, 05:11 AM
#14
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.
-
November 30th, 2014, 05:16 AM
#15
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|