1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Simple C coding

  1. Nov 26, 2014 #1
    1. The problem statement, all variables and given/known data
    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    int main()
        int i,j,k,n;
        float A[3][4];
        float x[3],c,sum=0;
        printf("first row\n");
        printf("second row\n");
        printf("third row\n");
        printf("designated value\n");
        for(j=0; j<=n; j++)
            for(i=0; i<=n; i++)
                    for(k=0; k<=n+1; k++)
        for(i=n-1; i>=0; i--)
                   for(j=i+1; j<=n; j++)

        printf("\n X = %f",x[0]);
        printf("\n Y = %f",x[1]);
        printf("\n Z = %f",x[2]);
    i use this code for elimination on matrix, but i need to input 4 times at the first row even though i only put 3 scanf
    what should i do to make it only need 3 input
    thanks for replying
  2. jcsd
  3. Nov 26, 2014 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    Does your problem statement require the use of scanf() ? You may know that scanf returns an int - you have to compare the int returned against EOF (# a define in stdio.h). This checks for a matching failure. Also note that what is already in the keyboard buffer carries over to the next scanf call, e.g., the return key.

    Decent programs try to avoid having multiples of the same statement over and over again. Why? Because it is harder to make changes in 20 lines of code than to change (and understand) 5 lines that do the same. Think loop.
  4. Nov 26, 2014 #3


    User Avatar
    Homework Helper

    Try removing the "\n" in the scanf calls, for example scanf("%f",&A[0][2]); without the "\n". scanf() is supposed to skip past whitespace on each call.
  5. Dec 1, 2014 #4
    yes i need the scanf because i have to input numbers on the matrix

    thank you
    when i removed the "/n" it does solve the problem
  6. Dec 1, 2014 #5
    C has looping features.

    // through magic, increase a number by 1.
    // through magic, increase a number by 1.

    Is really ugly. What if you had a 4 x 4 ? 6 x 6 ?
  7. Dec 3, 2014 #6
    so it is better if i scanf the "n" and put it into a loop

    so my loop will be like this?
    Code (Text):
    for (i=0; i<=n; i++)
              for (j=0; j<=n; j++)
    for my input?[/I]
  8. Dec 3, 2014 #7
    Supposing you code works (not going to compile and check for you), yes, this is the way to go. See how simpler and easier to refactor it got?

    Note that if n is the dimension of the square matrix (3, for instance), you want i < n as 3 would refer to the fourth row or column.

    Good luck.
  9. Dec 3, 2014 #8
    sorry about that
    i forgot it go n-1
  10. Dec 3, 2014 #9
    okay i have done it all, but i cant display them at the end of the code
    i use this
    Code (Text):

        for(i=0; i<n+1; i++)
        printf("\n X[i] = %f",x[i]);
    how can i make it display x1, x2 ... xn ?
  11. Dec 3, 2014 #10
    Supposing that from a bidimensinoal array you want something like:
    [tex]\begin{array}{cc} a_{1, 1} & a_{1, 2} \\ a_{2, 1} & a_{2, 2} \end{array}[/tex]

    You should iterate i and j and get
    Code (Text):
    Placing ", " between terms on the same row (same value of j) and "\n" after the last term of a row.
  12. Dec 3, 2014 #11
    maybe you get me wrong,
    i mean i want to display X1 X2, ... Xn
    not the full matrix
    so the results will be like
    X1 = ...
    X2 = ...
    X3 = ....
    X4 = ....
    for 4x4 matrix
  13. Dec 3, 2014 #12
    Yes, I got you wrong. So why are you having trouble with that?

    Code (C):
    for (int i = 0; i < 4; i++) {
      printf("X%d = %d\n", i + 1, a[row_index - 1][I]);
    Is this what you want?
  14. Dec 3, 2014 #13
    ahh, yes
    thank you so much
  15. Dec 3, 2014 #14
    No problem. Are you new to programming? Why are you using C?
  16. Dec 3, 2014 #15
    yeah i am new to C
    just a project for my test
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted