C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 16th, 2013, 03:39 AM
Stone99 Stone99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 Stone99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 16th, 2013, 03:59 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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).
__________________
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

Reply With Quote
  #3  
Old February 16th, 2013, 04:58 AM
Stone99 Stone99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 Stone99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 30 m 31 sec
Reputation Power: 0
any ideas? it will realy help

Reply With Quote
  #4  
Old February 16th, 2013, 08:36 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,905 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 9 m 41 sec
Reputation Power: 1774
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.

Reply With Quote
  #5  
Old February 16th, 2013, 08:59 AM
Stone99 Stone99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 Stone99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 30 m 31 sec
Reputation Power: 0
ok, but this may take a while

Reply With Quote
  #6  
Old February 16th, 2013, 09:39 AM
Stone99 Stone99 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 4 Stone99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Need help with matrix in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap