November 24th, 2012, 12:18 AM

2d array help(matrix)
Hello
i need assistance.
i was task by my instructor to solve this problem:
Write a program that fills the lefttoright diagonal of a square matrix of order 5 (5x5 array) with zeros, the lower left triangle with 1s and the upper right triangle with 1s.
and the output should be
0 1 1 1 1
1 0 1 1 1
1 1 0 1 1
1 1 1 0 1
1 1 1 1 0
Use nested loops in the assignment. Manual assignment using initializer is not allowed.
please i need help
November 24th, 2012, 12:22 AM

Help with what?
Can you for example
 declare the matrix
 print a matrix
 completely fill a matrix with a constant number
 just write the diagonal of a matrix
Your homework is YOUR homework
Post YOUR effort, and we'll guide you for the rest.
November 24th, 2012, 03:26 AM

Originally Posted by salem
Help with what?
Can you for example
 declare the matrix
 print a matrix
 completely fill a matrix with a constant number
 just write the diagonal of a matrix
Post YOUR effort, and we'll guide you for the rest.
i just need someone to explain how to do it
you don't need to code it
i haven't caught up with the logic yet.
November 24th, 2012, 03:38 AM

okay I am so close now
what i only need now is that the numbers must be 0s, and the lower left triangle must be all 1s, and the upper right triangle 1s.
how do I do this?
#include<stdio.h>
#define ROW 5
#define COL 5
int main(){
int array[ROW][COL];
int r, c;
for (r=0; r<ROW; r++){
for (c=0; c<COL; c++)
array[r][c] = (r+1)(c+1);
}
for (r=0; r<ROW; r++){
for (c=0; c<COL; c++)
printf("%5d", array[r][c]);
printf("\n");
}
printf("\n\n");
system("pause");
return 0;
}
November 24th, 2012, 04:26 AM

The diagonal is when r == c
Perhaps put this as a condition in your loop.
November 24th, 2012, 04:38 AM

Originally Posted by salem
The diagonal is when r == c
Perhaps put this as a condition in your loop.
it still gives the same output ?
November 24th, 2012, 04:57 AM

Really?
And where is your latest code, showing us how you used the latest hint?
Remember, post your code between [code][/code] tags for readability.
November 24th, 2012, 05:39 AM

Originally Posted by salem
Really?
And where is your latest code, showing us how you used the latest hint?
Remember, post your code between [code][/code] tags for readability.
more hints please?
November 24th, 2012, 06:49 AM

I'm not going to give you another hint until you've shown us exactly how you tried to use the last hint I gave.
Or more precisely, how you misused it to arrive at "it still gives the same output ?".
Besides, I find it hard to believe you came up with array[r][c] = (r+1)(c+1); to fill the array with +/1 to then come over all begging and clueless when it comes to filling a diagonal with zeros.
November 24th, 2012, 07:04 AM

Code:
#include<stdio.h>
#define ROW 10
#define COL 10
int main(){
int array[ROW][COL];
int r, c;
for (r=0; r<ROW; r++){
for (c=0; c<COL; c++)
array[r][c] = (r+1)*(c+1);
}
for (r=0; r<ROW; r++){
for (c=0; c<COL; c++)
printf("%5d", array[r][c]);
printf("\n");
}
printf("\n\n");
system("pause");
return 0;
}
i got the idea "array[r][c] = (r+1)(c+1);" with this previous activity
it's my 1st time learning 2D arrays so yeah I'm not a genius
November 24th, 2012, 08:12 AM

Originally Posted by salem
The diagonal is when r == c
Perhaps put this as a condition in your loop.
So in other words, we've wasted the last 4 hours to figure out that your code hasn't changed at all.
It is the most basic of instructions; If you couldn't figure this out, perhaps you want to reappraise your chosen subject.
Code:
if ( r == c ) {
banana;
} else {
custard;
}
> i got the idea "array[r][c] = (r+1)(c+1);" with this previous activity
Do you actually understand anything at all about that code, or are you just another one of these "found code" merchants, whose only real competence is the ability to use a search engine.
November 24th, 2012, 08:42 AM

Originally Posted by salem
So in other words, we've wasted the last 4 hours to figure out that your code hasn't changed
at all.
It is the most basic of instructions; If you couldn't figure this out, perhaps you want to reappraise your chosen subject.
Code:
if ( r == c ) {
banana;
} else {
custard;
}
> i got the idea "array[r][c] = (r+1)(c+1);" with this previous activity
Do you actually understand anything at all about that code, or are you just another one of these "found code" merchants, whose only real competence is the ability to use a search engine.
yeah yeah
and i'm an alien learning c programming
November 24th, 2012, 09:01 AM

Originally Posted by salem
So in other words, we've wasted the last 4 hours to figure out that your code hasn't changed
at all.
It is the most basic of instructions; If you couldn't figure this out, perhaps you want to reappraise your chosen subject.
Code:
if ( r == c ) {
banana;
} else {
custard;
}
> i got the idea "array[r][c] = (r+1)(c+1);" with this previous activity
Do you actually understand anything at all about that code, or are you just another one of these "found code" merchants, whose only real competence is the ability to use a search engine.
4 hours wasn't wasted
finally figured it out
Code:
#include <stdio.h>
#define row 5
#define col 5
int main(){
int arr[row][col];
int i,y,w;
int r=0, c=0;
for(i=0;i<5;i++)
{for(y=0;y<5;y++)
{arr[i][y]=0;
for(w=1;w<5;w++)
{ arr[0][w]=1; }
for(w=2;w<5;w++)
{ arr[1][w]=1; }
for(w=3;w<5;w++)
{ arr[2][w]=1; }
for(w=4;w<5;w++)
{ arr[3][w]=1; }
for(w=5;w<5;w++)
{ arr[4][w]=1; }
for(w=1;w<5;w++)
{ arr[w][0]=1; }
for(w=2;w<5;w++)
{ arr[w][1]=1; }
for(w=3;w<5;w++)
{ arr[w][2]=1; }
for(w=4;w<5;w++)
{ arr[w][3]=1; }
}}
for(i=0;i<5;i++)
{ printf("\n");
for(y=0;y<5;y++)
{
if(arr[i][y]==1)
{
for(i=0;i<row;i++){
for(y=0;y<col;y++)
printf("%5d",arr[i][y]);
printf("\n");
}
}
if(arr[i][y]==0)
{
for(i=0;i<row;i++){
for(y=0;y<col;y++)
printf("%5d",arr[i][y]);
printf("\n");
}
}
if(arr[i][y]==1)
{
for(i=0;i<row;i++){
for(y=0;y<col;y++)
printf("%5d",arr[i][y]);
printf("\n");
}
}
}}
getch ();
return 0;
}
no thanks to you
November 24th, 2012, 09:46 AM

Trivial in j.
Code:
i. 5 NB. integers
0 1 2 3 4
/~ i. 5 NB. subtraction table of the integers
0 _1 _2 _3 _4
1 0 _1 _2 _3
2 1 0 _1 _2
3 2 1 0 _1
4 3 2 1 0
* /~ i. 5 NB. sign of subtraction table of the integers
0 _1 _1 _1 _1
1 0 _1 _1 _1
1 1 0 _1 _1
1 1 1 0 _1
1 1 1 1 0
 * /~ i. 5 NB. negative sign of subtraction table of the integers
0 1 1 1 1
_1 0 1 1 1
_1 _1 0 1 1
_1 _1 _1 0 1
_1 _1 _1 _1 0
: * /~ i. 5 NB. transpose sign of subtraction table of the integers
0 1 1 1 1
_1 0 1 1 1
_1 _1 0 1 1
_1 _1 _1 0 1
_1 _1 _1 _1 0
* ~/~ i. 5 NB. sign of (passive (meaning swapped) subtraction) table of the integers
0 1 1 1 1
_1 0 1 1 1
_1 _1 0 1 1
_1 _1 _1 0 1
_1 _1 _1 _1 0
* /~ i. _5 NB. sign of subtraction table of the descending integers
0 1 1 1 1
_1 0 1 1 1
_1 _1 0 1 1
_1 _1 _1 0 1
_1 _1 _1 _1 0
[code]
Code tags[/code] are essential for python code and Makefiles!