C++ gurus

Bluga

Banned
Nov 28, 2000
4,315
0
0

How can i set the main diagonal of a n*n Matrix to 1 and other elements to 0?

thanks alot.
 

Bluga

Banned
Nov 28, 2000
4,315
0
0
ok i think i know how to set the "\" diagonal to one, but what about other elements? Can these be done in one step?

so far i have:

for (s=0; s<=n; i++)
for (j=0; j<=n, j++){
if s=j;
A[j] =1;
else
A[j]=0;
}
 

Drekce

Golden Member
Sep 29, 2000
1,398
0
76
create a nested loop that goes n^2 (n on the main loop, and n in the nested) times, and everytime through the loop set the value at [n][n] to 1 and every other value to zero

EX:

for( i=0 ; i<n ; i++ ) {

for (j = 0 ; j < n ; j++) {

VAR[j] = 0;

}

VAR = 1;
}


Should work.
 

tweakmm

Lifer
May 28, 2001
18,436
4
0
for your matrix matrix[n][m]

int columncheck=0;

for(int i=0;i<n;i++)
{
for(int j =0;j<m;j++)
{
if (columcheck == j)
matrix[.I][j] = 1;
else
matrix[.I][j] = 0;
}
columcheck++;
}

EDIT:
sorry about the .Is, if I dont have those, the forums thinks that i want italics




 

Cat

Golden Member
Oct 10, 1999
1,059
0
0
int columns, rows;

int matrix[columns][rows];

memset( &matrix[0], 0, sizeof(int) * columns * rows);
Will zero out the matrix.

Then you can set the diagonals to 1 in a separate step.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
Cat is right ( although the example he gave should have rows and columns as const ). You don't need nested loops to set the identity matrix, the following snippet of code declares and creates 6*6 identity matrix.


const int SIZE = 6;
int MATRIX[SIZE][SIZE];

memset(MATRIX, 0, sizeof(int)*SIZE*SIZE);
for(int k=0; k<SIZE; k++)
MATRIX[k][k] = 1;


 

Rotorvator

Member
Jan 7, 2001
37
0
0
actuall, that won't work either - need to
#define SIZE x

const int will only work when dynamically allocating the array, and then only on one dimension.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0


<< actuall, that won't work either - need to
#define SIZE x

const int will only work when dynamically allocating the array, and then only on one dimension.
>>



from the C++ ANSI schema ...

array declaration ...



<<
In a declaration T D where D has the form
D1 [constant-expressionopt]
and the type of the identifier in the declaration T D1 is "derived-
declarator-type-list T," then the type of the identifier of D is an
array type. T is called the array element type; this type shall not
be a reference type, the type void, a function type or an abstract
class type. If the constant-expression (_expr.const_) is present, it
shall be an integral constant expression and its value shall be
greater than zero. ...
>>



constant expression definition



<<
...
An integral constant-expression can involve only literals (_lex.lit-
eral_), enumerators, const variables or static data members of inte-
gral or enumeration types initialized with constant expressions
...
>>



with that in mind const int s = 5; is a constant expression, hence the array declaration int d is valid according to the ANSI C++ specs. I'm not event sure how you mixed in the "one dimension" component into this.
 

Rotorvator

Member
Jan 7, 2001
37
0
0
Bear in mind I learned C++ prior to finalization of the ANSI std, and still work with an (more or less) ARM compiler.

Every now and then someone whips out a stroustrup book and whaps me upside the head with it, for which I am greatful

Prior to ansi you couldn't declare stack allocated arrays with a variable bound (const or otherwise).
i.e.
const int a = 8;
int b[a];
would break, but
int * b = new int[a];
would not.

Also, you could only do this in one dimension -
int * b = new int[a][a];
would not compile, but
int * b = new int[a][8];
would.

Which is why I suggested the #define
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |