Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 24th, 2012, 01:18 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
2d array help(matrix)

Hello

i need assistance.
i was task by my instructor to solve this problem:

Write a program that fills the left-to-right 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.

#2
November 24th, 2012, 01:22 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 30 sec
Reputation Power: 1827
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.
__________________
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

#3
November 24th, 2012, 04:26 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
Quote:
 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.

#4
November 24th, 2012, 04:38 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
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;
}

#5
November 24th, 2012, 05:26 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 30 sec
Reputation Power: 1827
The diagonal is when r == c

Perhaps put this as a condition in your loop.

#6
November 24th, 2012, 05:38 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
Quote:
 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 ?

#7
November 24th, 2012, 05:57 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 30 sec
Reputation Power: 1827
Really?
And where is your latest code, showing us how you used the latest hint?

#8
November 24th, 2012, 06:39 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
Quote:
 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.

#9
November 24th, 2012, 07:49 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 30 sec
Reputation Power: 1827
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 mis-used 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.

#10
November 24th, 2012, 08:04 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
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

#11
November 24th, 2012, 09:12 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,260
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 3 m 30 sec
Reputation Power: 1827
Quote:
 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.

#12
November 24th, 2012, 09:42 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
Quote:
 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

#13
November 24th, 2012, 10:01 AM
 needhelpalways
Registered User

Join Date: Nov 2012
Posts: 8
Time spent in forums: 3 h 21 m 14 sec
Reputation Power: 0
Quote:
 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

#14
November 24th, 2012, 10:46 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,163
Time spent in forums: 1 Month 3 Weeks 2 Days 9 h 25 m 16 sec
Reputation Power: 455
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!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > 2d array help(matrix)