### Thread: Need help with matrix in C

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0

#### Need help with matrix in C

hi there
im newbie in C programming, started a few months ago, and i got an assignment wich involves matrix
I have to rearrange columns of a matrix in ascending order, in dependence of column characteristics, now a column characteristic is considered
module sum of odd negative elements
for example:
-7 2 1
-3 -5 2
1 4 -3
the characteristics are 10 5 and 3
My code so far

Code:
```#include <stdio.h>
#include <conio.h>
#define M 5 /* Max matrix dimension*/
void main ()
{   int nl,nc;   /* nl  lines, nc columns*/
int tab1[M][M], i, j, s=0, tab2[M][M], tab3[M],temp,g,temp2,tab4[M],k;

printf("Enter matrix dimension: \n");
printf ("line number: "); scanf("%d",&nl);
printf ("column number: "); scanf ("%d",&nc);
if (nl >M || nc >M)
{  printf("Error: dimensions >5 \n"); return;}

printf("Enter matrix elements\n");
for (i=0;i<nl;i++)
for (j=0;j<nc;j++)
{printf(" elem[%d][%d]:",i,j);
scanf ("%d", &tab1[i][j]);
}

for (i=0;i<nl;i++)
{
for (j=0;j<nc;j++)
printf(" %3d ", tab1[i][j]);
printf("\n");
}

for (i=0;i<nl;i++)
for (j=0;j<nc;j++)
if(tab1[i][j]<0 && tab1[i][j]%2!=0) // searching negative odd elements
tab2[i][j]=fabs(tab1[i][j]);
else
tab2[i][j]=0;

for (j=0;j<nc;j++){
s=0;
for (i=0;i<nl;i++)
s+=tab2[i][j];
tab3[j]=s;}

printf("tab3\n");
for(j=0; j<nc; j++)
printf(" %3d ",tab3[j]);
printf("\n");

do
{g=0;

for (j=0;j<nc;j++)

if (tab3[j]>tab3[j+1])
{temp2=tab3[j];
tab3[j]=tab3[j+1];
tab3[j+1]=temp2;
for (j=0;j<nc;j++)
{for (i=0;i<nl;i++)

temp=tab1[i][j];
tab1[i][j]=tab1[i][j+1];
tab1[i][j+1]=temp;

g=1;}}

}while (g==1);

printf("Final matrix\n");
for (i=0;i<nl;i++)
{
for (j=0;j<nc;j++)
printf(" %3d ", tab1[i][j]);
printf("\n");

}```
unfortunately it doesnt do the job

Few can be bothered to even look through unformatted code. Those that do will only do so if they have time at the end, and only if the other thing on offer is even worse (like root canal denture work).
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0
any ideas? it will realy help
4. Well I was hoping for something better, but still - early days I suppose.
Code:
```#include <stdio.h>
#include <math.h> //!! for fabs
//#include <conio.h>
#define M 5                     /* Max matrix dimension */

//!! Define some useful functions, so your main doesn't grow
//!! to some unmanageable size.
void inputMatrix ( int rows, int cols, int (*mat)[M] ) {
int i, j;
printf("Enter matrix elements\n");
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++) {
printf(" elem[%d][%d]:", i, j);
scanf("%d", &mat[i][j]);
}
}
void printMatrix ( int rows, int cols, int (*mat)[M] ) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf(" %3d ", mat[i][j]);
printf("\n");
}
}

int main()  //!! yes, really, it does return int
{
//!! picking better (more descriptive) variable names
//!! would be a good idea.
int nl, nc;                   /* nl  lines, nc columns */
int tab1[M][M], i, j, s = 0, tab2[M][M], tab3[M], temp, g, temp2, tab4[M], k;

printf("Enter matrix dimension: \n");
printf("line number: ");
scanf("%d", &nl);
printf("column number: ");
scanf("%d", &nc);
if (nl > M || nc > M) {
printf("Error: dimensions >5 \n");
return;
}

//!! use the utility functions
inputMatrix( nl, nc, tab1 );
printMatrix( nl, nc, tab1 );

//!! does what?
for (i = 0; i < nl; i++)
for (j = 0; j < nc; j++)
if (tab1[i][j] < 0 && tab1[i][j] % 2 != 0)  // searching negative odd elements
tab2[i][j] = fabs(tab1[i][j]);
else
tab2[i][j] = 0;

//!! does what?
for (j = 0; j < nc; j++) {
s = 0;
for (i = 0; i < nl; i++)
s += tab2[i][j];
tab3[j] = s;
}

printf("tab3\n");
for (j = 0; j < nc; j++)
printf(" %3d ", tab3[j]);
printf("\n");

do {
g = 0;
for (j = 0; j < nc; j++)
if (tab3[j] > tab3[j + 1]) {
temp2 = tab3[j];
tab3[j] = tab3[j + 1];
tab3[j + 1] = temp2;
for (j = 0; j < nc; j++) {
for (i = 0; i < nl; i++)
temp = tab1[i][j];

//!! Here is your possible bug
//!! whatever your indentation may say, the
//!! next two lines are NOT part of the inner
//!! for loop.  So the value of i used here
//!! is off the end of the matrix
tab1[i][j] = tab1[i][j + 1];
tab1[i][j + 1] = temp;

g = 1;
}
}
} while (g == 1);

printMatrix( nl, nc, tab1 );

}```
Pay attention to all the //!! comments.

If there is one lesson you should learn from this, it's that you should treat all optional use of { } as being compulsory.

Look at your big for / if / for / for nested construct with only TWO pairs of braces.

Now I want you to go through all your code, and put in { } in all the places where you're treating them as optional.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0
ok, but this may take a while
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
4
Rep Power
0
I did it! works like a charm
here is my new code
Code:
```#include <stdio.h>
#include <math.h>
#define M 5                     /* Max matrix dimension */

void inputMatrix ( int rows, int cols, int (*mat)[M] ) {
int i, j;
printf("Enter matrix elements\n");
for (i = 0; i < rows; i++)
for (j = 0; j < cols; j++) {
printf(" elem[%d][%d]:", i, j);
scanf("%d", &mat[i][j]);
}
}
void printMatrix ( int rows, int cols, int (*mat)[M] ) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++)
printf(" %3d ", mat[i][j]);
printf("\n");
}
}

int main()
{

int nl, nc;                   /* nl  lines, nc columns */
int tab1[M][M], i, j, s = 0, tab2[M][M], tab3[M], temp, g, temp2;

printf("Enter matrix dimension: \n");
printf("line number: ");
scanf("%d", &nl);
printf("column number: ");
scanf("%d", &nc);
if (nl > M || nc > M) {
printf("Error: dimensions >5 \n");
return;
}

inputMatrix( nl, nc, tab1 );
printMatrix( nl, nc, tab1 );

// searching negative odd elements
for (i = 0; i < nl; i++)
for (j = 0; j < nc; j++)
if (tab1[i][j] < 0 && tab1[i][j] % 2 != 0)
tab2[i][j] = fabs(tab1[i][j]);
else
tab2[i][j] = 0;

//!! Copying the sum of negative odd elements
for (j = 0; j < nc; j++)
{
s = 0;
for (i = 0; i < nl; i++)
s += tab2[i][j];
tab3[j] = s;
}

printf("tab3\n");
for (j = 0; j < nc; j++)
printf(" %3d ", tab3[j]);
printf("\n");

do {
g = 0;
for (j = 0; j < nc; j++)
if (tab3[j] > tab3[j + 1])
{
temp2 = tab3[j];
tab3[j] = tab3[j + 1];
tab3[j + 1] = temp2;

for (i = 0; i < nl; i++)
{temp = tab1[i][j];
tab1[i][j] = tab1[i][j + 1];
tab1[i][j + 1] = temp;

g = 1;

}}
} while (g == 1);

printMatrix( nl, nc, tab1 );

return 0;
}```
thanks a lot, you realy helped me
btw int (*mat)[M] why did you put this pointer?