print matrix

R

Ronen Kfir

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!
 
M

Mark A. Odell

(e-mail address removed) (Ronen Kfir) wrote in

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().
 
A

Arthur J. O'Dwyer

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
 
R

Ronen Kfir

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.
 
R

Ralmin

Ronen Kfir said:
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.
 
O

osmium

Ralmin said:
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:

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

Arthur J. O'Dwyer

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


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
 
C

Chris Torek

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top