Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C programming help nedded ?what is the wrong of this code?

  1. May 8, 2009 #1
    C programming help nedded ??what is the wrong of this code?

    #include<stdio.h>
    int matA[3][4],matB[4][3],matAB[3][3],i,j,k;
    void read();
    void multiply();
    void display();
    int main()
    {
    read();
    multiply();
    display();
    return 0;
    }

    void read()
    {
    printf("Enter your matrix A:\n");

    for (i=0;i<3;i++)
    {
    for (j=0;j<4;j++)
    {
    scanf("%d",&matA[j]);
    }}

    printf("Enter your matrix B:\n");

    for (i=0;i<4;i++)
    {
    for (j=0;j<3;j++)
    {
    scanf("%d",&matB[j]);
    }}
    }

    void multiply()
    {
    for (i=0;i<3;i++)
    {
    for (j=0;j<3;j++)
    {
    matAB[j]=0;
    for (k=0;k<4;k++)
    {
    matAB[j]=matAB[j]+matA[k]*matB[k][j];
    }}}}

    void display()
    {
    printf("Here is your multiplied matrix:\n");
    for (i=0;i<3;i++);
    {
    for (j=0;j<3;j++);
    {
    printf("%d",matAB[j]);
    }
    printf("\n");
    }}


    i wrote this multiply matA 3*4 by matB 4*3 ...........its compiling but doesn't give me the required output??/it gives me a 0..........i dont know how to find the error ...........im totally stuck can someone please check this code and find the error for me ........
     
  2. jcsd
  3. May 8, 2009 #2
    Re: C programming help nedded ??what is the wrong of this code?

    Aside from the code not being very well-written, here is the problem:

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

    Try the following:

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

    The semicolons after the "for" statements basically kill the looping... so there is no looping at all, it sees the semicolon and assumes that's the loop. It's a no-op loop.
     
  4. May 8, 2009 #3
    Re: C programming help nedded ??what is the wrong of this code?

    yr yr i got it ...............thanks alot .........anyway im a newbie to programming .these are my first programmes ..so u have said that the "code is not well written".can u please show me the places where i have used bad methods or wrong algorithms .if u can do it it will be a great help.
     
  5. May 8, 2009 #4
    Re: C programming help nedded ??what is the wrong of this code?

    Since you asked, I would suggest you write your program with proper indenting. You may already have done so, but if you post with code delimiters bracketing your code, the indents will show, and will help others appreciate your code in less time.
    For example, instead of:

    for (i=0;i<3;i++)
    {
    for (j=0;j<4;j++)
    {
    scanf("%d",&matA[j]);
    }}

    you write:
    Code (Text):

      for (i=0;i<3;i++)
      {
        for (j=0;j<4;j++)
        {
          scanf("%d",&matA[i][j]);
        }
      }
     
    In my case I would forget the braces when there is only one line after the loop (but may be objectionalble to many people):
    Code (Text):

      for(i=0;i<3;i++) for(j=0;j<4;j++)  scanf("%d",&matA[i][j]);
     
     
  6. May 8, 2009 #5

    sylas

    User Avatar
    Science Advisor

    Re: C programming help nedded ??what is the wrong of this code?

    The indentation idea is really crucial. The layout of a program doesn't affect how it compiles, but it makes a huge difference for reading and maintaining it. (And contrary to mathmate, I strongly advise being absolutely consistent on indentation, with steely determination. Include braces {} every single time you have a loop or an if statement -- as you have done.)

    Another thing is that you should start using some local variables. That is, put variable definitions inside your functions, instead of all at the top.

    Ultimately you will want to use parameters, but for now you can leave the arrays matA, matB and matAB at the top of the program for simplicity.

    But variables i, j and k would be better defined inside each function where they are used. This prevents problems with unintended interference and side effects. It doesn't matter here... but it will, it will...

    Cheers -- sylas
     
  7. May 8, 2009 #6
    Re: C programming help nedded ??what is the wrong of this code?

    yr these ideas are so helpful to me for improving my programming knowledge ......but ..............i dont know how to set indentations in the programme.i use KWRITE for writing my C programmes .so can u please tell me that is there an easy way to put indentation to the program without applying them manually one by one.
     
  8. May 8, 2009 #7
    Re: C programming help nedded ??what is the wrong of this code?

    There are products on the market for formatting source code from external sources.
    If you want to pursue a career in programming, you are better off learning how to do it manually, using spaces or tabs, so that you know what you want.
    Anyway, here are some suggested links:
    http://en.wikipedia.org/wiki/Prettyprint
    http://uncrustify.sourceforge.net/
    I have not used these myself, so I cannot comment on the quality or reliability of these products. Google on 'c beautifier' and you will have a much wider choice.
     
  9. May 8, 2009 #8
    Re: C programming help nedded ??what is the wrong of this code?

    There's your problem: you're using a word processor to do the job of a text editor. They are very different things.

    You should switch a real text editor right away. You already have one: you're on a KDE desktop (I infer), so instead of Kwrite, use Kate:

    http://kate-editor.org/kate

    http://kate-editor.org/files/images/kate-editing-homepage_0.preview.png [Broken]

    Or get an IDE (integrated development environment) like NetBeans: (on the download, you only need the C/C++ modules)

    http://www.netbeans.org/features/cpp/

    http://www.netbeans.org/images/v6/5/screenshots/cpp-cut.png [Broken]

    Whatever you do, stay away from "expert" editors like vi, vim, and emacs. These have extremely steep learning curves.
     
    Last edited by a moderator: May 4, 2017
  10. Jul 20, 2009 #9
    Re: C programming help nedded ??what is the wrong of this code?

    And hey dude if you get errors try debuging by printlining. Add some statemnts that shows the state of the variables or at which line of execution your programs at.
    I've learned to apreciate this method because it makes it easy to spot bugs and is way more fun that using a debugger.

    Wel then cheers and happy coding :-)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C programming help nedded ?what is the wrong of this code?
Loading...