The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Need help with matrix in C
Discuss Need help with matrix in C in the C Programming forum on Dev Shed. Need help with matrix in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 16th, 2013, 03:39 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 4
Time spent in forums: 3 h 30 m 31 sec
Reputation 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]);
}
printf("Your matrix [%d][%d]\n",nl,nc);
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
|

February 16th, 2013, 03:59 AM
|
 |
Contributed User
|
|
|
|
|
Well you could edit your post to use [code][/code] tags instead of inventing your own.
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).
|

February 16th, 2013, 04:58 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 4
Time spent in forums: 3 h 30 m 31 sec
Reputation Power: 0
|
|
|
any ideas? it will realy help
|

February 16th, 2013, 08:36 AM
|
 |
Contributed User
|
|
|
|
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;
printf("Your matrix [%d][%d]\n", rows, cols);
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 );
return 0; //!! added
}
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.
|

February 16th, 2013, 08:59 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 4
Time spent in forums: 3 h 30 m 31 sec
Reputation Power: 0
|
|
|
ok, but this may take a while
|

February 16th, 2013, 09:39 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 4
Time spent in forums: 3 h 30 m 31 sec
Reputation 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;
printf("Your matrix [%d][%d]\n", rows, cols);
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?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|