print matrix

Discussion in 'C Programming' started by Ronen Kfir, Jun 4, 2004.

  1. Ronen Kfir

    Ronen Kfir Guest

    I need to take as an input matrix N*M from stdin & print it as a
    matrix shape, something like:


    |6 8 2 4 7|
    |4 0 1 8 2|
    |3 1 5 2 6|
    |9 3 8 4 0|

    I don't have any idea how exactly I do it.
    Help will be appreciated!
     
    Ronen Kfir, Jun 4, 2004
    #1
    1. Advertising

  2. (Ronen Kfir) wrote in
    news::

    > I need to take as an input matrix N*M from stdin & print it as a
    > matrix shape, something like:
    >
    >
    >|6 8 2 4 7|
    >|4 0 1 8 2|
    >|3 1 5 2 6|
    >|9 3 8 4 0|
    >
    > I don't have any idea how exactly I do it.
    > Help will be appreciated!


    I would start with printf().

    --
    - Mark ->
    --
     
    Mark A. Odell, Jun 4, 2004
    #2
    1. Advertising

  3. Ronen Kfir wrote:

    > I need to take as an input matrix N*M from stdin & print it as a
    > matrix shape, something like:
    >
    >
    > |6 8 2 4 7|
    > |4 0 1 8 2|
    > |3 1 5 2 6|
    > |9 3 8 4 0|
    >
    > I don't have any idea how exactly I do it.
    > Help will be appreciated!


    Take a look at
    The ANSI C Numerical Class Library:

    http://www.netwood.net/~edwin/svmtl/
     
    E. Robert Tisdale, Jun 4, 2004
    #3
  4. On Fri, 4 Jun 2004, Ronen Kfir wrote:
    >
    > I need to take as an input matrix N*M from stdin & print it as a
    > matrix shape, something like:


    First, pick one group and stick to it. Don't multi-post your
    message to many different groups. If you *must* post to multiple
    groups, then cross-post, like I did in this response (a response
    written in response to your question in comp.lang.c, BTW).

    Secondly, no matter what language you're using, you need to
    know *what* you're doing before worrying about the *how*. What
    kind of input is the user going to be typing? Is he going to
    enter

    1 2 3 4 5 6 7 8 9

    and you're by magic going to print

    1 2 3
    4 5 6
    7 8 9

    or is the user going to have to tell the program how many dimensions
    the matrix has, and what size it is, like this?

    4 2
    1 2 3 4 5 6 7 8

    and then you would print a 4-by-2 matrix

    1 2 3 4
    5 6 7 8

    Maybe the user is going to enter his matrix himself, and use some
    special code to indicate that he's done entering rows, like this:

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15
    done

    In this last case, would you output

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15

    or would your user rather see

    1 2 3 4 5
    6 7 8 9 10
    11 12 13 14 15

    In the latter case, should the entries by justified toward the right,
    the left, or centered: e.g.,

    1 2 3
    42 567 8
    17 42 725

    Should the columns all be equally wide, e.g.

    1 2 65535
    3 4 5

    or should they have variable width, e.g.

    1 2 65535
    3 4 5

    If the latter, then would it be acceptable to overlap
    columns on particularly odd inputs, e.g.

    1 2 1234567890 3 4
    2 456 89 10112 7

    If not, then how many spaces between columns? Are hard
    tabs okay? (Hint: no. ;) Answer these questions for
    yourself, and *then* think about what algorithms you'll
    need to implement.

    Once you know not only *what* you're doing, but also
    *how* you'll do it (i.e., the algorithms involved), then
    you'll finally be ready to start asking questions that
    might be topical on comp.lang.c and alt.comp.lang.learn.c-c++.
    We deal with the C *language* here, not algorithms; for
    algorithm help, try comp.programming.

    -Arthur
     
    Arthur J. O'Dwyer, Jun 4, 2004
    #4
  5. Ronen Kfir

    Ronen Kfir Guest

    thanx alot for all rsponses! I keep on learning...
    The answer should be written in C.
    The N&M are #defined. the user will be asked to enter N*M nubers &
    will write one per line; number enter number enter number enter, etc.
    like:
    1
    2
    3
    4
    5

    cheers.

    "Arthur J. O'Dwyer" <> wrote in message news:<>...
    > On Fri, 4 Jun 2004, Ronen Kfir wrote:
    > >
    > > I need to take as an input matrix N*M from stdin & print it as a
    > > matrix shape, something like:

    >
    > First, pick one group and stick to it. Don't multi-post your
    > message to many different groups. If you *must* post to multiple
    > groups, then cross-post, like I did in this response (a response
    > written in response to your question in comp.lang.c, BTW).
    >
    > Secondly, no matter what language you're using, you need to
    > know *what* you're doing before worrying about the *how*. What
    > kind of input is the user going to be typing? Is he going to
    > enter
    >
    > 1 2 3 4 5 6 7 8 9
    >
    > and you're by magic going to print
    >
    > 1 2 3
    > 4 5 6
    > 7 8 9
    >
    > or is the user going to have to tell the program how many dimensions
    > the matrix has, and what size it is, like this?
    >
    > 4 2
    > 1 2 3 4 5 6 7 8
    >
    > and then you would print a 4-by-2 matrix
    >
    > 1 2 3 4
    > 5 6 7 8
    >
    > Maybe the user is going to enter his matrix himself, and use some
    > special code to indicate that he's done entering rows, like this:
    >
    > 1 2 3 4 5
    > 6 7 8 9 10
    > 11 12 13 14 15
    > done
    >
    > In this last case, would you output
    >
    > 1 2 3 4 5
    > 6 7 8 9 10
    > 11 12 13 14 15
    >
    > or would your user rather see
    >
    > 1 2 3 4 5
    > 6 7 8 9 10
    > 11 12 13 14 15
    >
    > In the latter case, should the entries by justified toward the right,
    > the left, or centered: e.g.,
    >
    > 1 2 3
    > 42 567 8
    > 17 42 725
    >
    > Should the columns all be equally wide, e.g.
    >
    > 1 2 65535
    > 3 4 5
    >
    > or should they have variable width, e.g.
    >
    > 1 2 65535
    > 3 4 5
    >
    > If the latter, then would it be acceptable to overlap
    > columns on particularly odd inputs, e.g.
    >
    > 1 2 1234567890 3 4
    > 2 456 89 10112 7
    >
    > If not, then how many spaces between columns? Are hard
    > tabs okay? (Hint: no. ;) Answer these questions for
    > yourself, and *then* think about what algorithms you'll
    > need to implement.
    >
    > Once you know not only *what* you're doing, but also
    > *how* you'll do it (i.e., the algorithms involved), then
    > you'll finally be ready to start asking questions that
    > might be topical on comp.lang.c and alt.comp.lang.learn.c-c++.
    > We deal with the C *language* here, not algorithms; for
    > algorithm help, try comp.programming.
    >
    > -Arthur
     
    Ronen Kfir, Jun 5, 2004
    #5
  6. Ronen Kfir

    Ralmin Guest

    "Ronen Kfir" <> wrote:
    > thanx alot for all rsponses! I keep on learning...
    > The answer should be written in C.
    > The N&M are #defined. the user will be asked to enter N*M nubers &
    > will write one per line; number enter number enter number enter, etc.
    > like:
    > 1
    > 2
    > 3
    > 4
    > 5


    Well, here's one obvious algorithm:

    1. Define an two-dimensional array with dimensions N by M.
    2. For each element in the array, read an integer value into the array.
    3. For each line of the matrix:
    3.1. print a | character
    3.2. for each value in the line, print the value
    3.3. print another | character and newline

    For a toy program like this with an matrix of ints, scanf("%d", ...) should
    be sufficient for the input. Remember that scanf requires the address of the
    place where you want to store the input value.

    Give it a go, if you have problems then post your code here.

    --
    Simon.
     
    Ralmin, Jun 6, 2004
    #6
  7. Ronen Kfir

    osmium Guest

    Ralmin writes:

    > "Ronen Kfir" <> wrote:
    > > thanx alot for all rsponses! I keep on learning...
    > > The answer should be written in C.
    > > The N&M are #defined. the user will be asked to enter N*M nubers &
    > > will write one per line; number enter number enter number enter, etc.
    > > like:
    > > 1
    > > 2
    > > 3
    > > 4
    > > 5

    >
    > Well, here's one obvious algorithm:
    >
    > 1. Define an two-dimensional array with dimensions N by M.


    You didn't read the problem closely enough. He said:

    > > The answer should be written in C.


    C does not support two-dimensional arrays. Writing a Fortran program will
    not solve his problem!
    <snip>
     
    osmium, Jun 6, 2004
    #7
  8. On Sun, 6 Jun 2004, osmium wrote:
    >
    > Ralmin writes:
    > > "Ronen Kfir" <> wrote:
    > > > thanx alot for all rsponses! I keep on learning...
    > > > The answer should be written in C.
    > > > The N&M are #defined. the user will be asked to enter N*M nubers &
    > > > will write one per line; number enter number enter number enter, etc.


    > > Well, here's one obvious algorithm:
    > >
    > > 1. Define an two-dimensional array with dimensions N by M.

    >
    > You didn't read the problem closely enough. He said:
    >
    > > > The answer should be written in C.

    >
    > C does not support two-dimensional arrays. Writing a Fortran program will
    > not solve his problem!


    osmium, your last two posts in this newsgroup (comp.lang.c) have
    been singularly unhelpful. Do try to keep your signal-to-noise ratio
    above 50%, at least.
    For the OP's benefit, C supports 2-dimensional arrays using the
    following syntax:

    #define N 10
    #define M 5

    int myMatrix[N][M];

    The array is then accessed in the obvious manner; e.g., 'myMatrix[j]'
    is an object of type 'int'.

    Getting back to the OP's problem, I would strongly suggest he rethink
    this interface unless it's *absolutely required* by his instructor.
    Can you imagine trying to enter successfully and without errors 50
    integers, one per line, as per the above declarations and problem
    statement? What happens if the user messes up an entry? Can he go
    back and fix it, or must he run the entire program again? Remember
    that on most systems, once the user hits "Enter," he's lost the ability
    to edit that line of his input.

    -Arthur
     
    Arthur J. O'Dwyer, Jun 6, 2004
    #8
  9. Ronen Kfir

    Chris Torek Guest

    In article <news:p>
    Arthur J. O'Dwyer <> writes:
    > For the OP's benefit, C supports 2-dimensional arrays using the
    >following syntax:
    >
    > #define N 10
    > #define M 5
    >
    > int myMatrix[N][M];
    >
    >The array is then accessed in the obvious manner; e.g., 'myMatrix[j]'
    >is an object of type 'int'.


    It is worth noting, though, that these are indeed not "true"
    multidimensional arrays, in the sense offered by other languages
    (including modern Fortran, such as F90). Instead, these are
    one-dimensional arrays whose elements are in turn one-dimensional
    arrays. The object "myMatrix" is a one-dimensional array of size
    10. Each element myMatrix is another (different) one-dimensional
    array of size 5 -- hence the need for a second set of indexing
    brackets, myMatrix[j], to select the j'th element of the i'th
    array.

    In languages with "real" multidimensional arrays, one generally
    writes something like myMatrix[i,j]. Omitting either the row or
    column selector is not permitted unless the operation is an "array
    slice", e.g., myMatrix[,j] means "every element of myMatrix whose
    second index is j". One might then write:

    myMatrix[, j] +:= 3; -- this is not C code!

    to add 3 to myMatrix[i,j] for all i in [0..N). Of course, C does
    not offer this, and it *can*not because its arrays are really all
    one-dimensional underneath.

    C's "fake" multidimensional arrays will, of course, do the job in
    this case.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
     
    Chris Torek, Jun 7, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,675
    Jonathan Bromley
    Jul 5, 2006
  2. keto
    Replies:
    0
    Views:
    964
  3. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    410
    Holgerson
    Oct 26, 2007
  4. Terry Reedy
    Replies:
    0
    Views:
    557
    Terry Reedy
    Apr 2, 2009
  5. Robert Kern
    Replies:
    0
    Views:
    597
    Robert Kern
    Apr 2, 2009
Loading...

Share This Page