473,387 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

print matrix

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!
Nov 14 '05 #1
8 2763
ro****@tauex.tau.ac.il (Ronen Kfir) wrote in
news:b0**************************@posting.google.c om:
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 ->
--
Nov 14 '05 #2

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

Nov 14 '05 #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/
Nov 14 '05 #4
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" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi**********************************@unix43. andrew.cmu.edu>...
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

Nov 14 '05 #5
"Ronen Kfir" <ro****@tauex.tau.ac.il> 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.
Nov 14 '05 #6
Ralmin writes:
"Ronen Kfir" <ro****@tauex.tau.ac.il> 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>
Nov 14 '05 #7

On Sun, 6 Jun 2004, osmium wrote:

Ralmin writes:
"Ronen Kfir" <ro****@tauex.tau.ac.il> 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[i][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
Nov 14 '05 #8
In article <news:Pi**********************************@unix48. andrew.cmu.edu>
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> 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[i][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[i] is another (different) one-dimensional
array of size 5 -- hence the need for a second set of indexing
brackets, myMatrix[i][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.
Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
1
by: SUPER_SOCKO | last post by:
Suppose I have a matrix M x N dimension. Normally, to print all of the elements in the matrix. I write the following codes: Suppose M = 5, N = 4: for(int j=0; j<5; ++j) { for(int k=0; k<4;...
2
by: Fred | last post by:
Hi. Sorry, but I don't know where else to ask. I wrote a access shipping database and we currently use the web interface for DHL to print shipping labels. That means we have to manualy transpose...
7
by: bclegg | last post by:
Hi, I have a Monitoring application that needs to output single line summaries to a local dot matrix printer loaded with line flow. ie. It will have exclusive use of the printer which is connected...
0
by: mahmoodbh | last post by:
Deal All, I have an Epson LX-300+ dot matrix printer and I would like to print one line data raw using arabic character. As we now, the printdocument calss will not help becuase it is use the...
2
by: candexis | last post by:
Hi!!!!!!!, well i want to know how to print in screen a matrix like this: Matrix = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; And the output must be like this: 1 2 3 4 5 6 7 8 9...
0
by: tasukiph | last post by:
How do I print to a dot matrix printer? I tried using pdf but when I choose to print the document there is always a margin. How do I delete the margins in pdf? Do I have to set the invoice size?...
1
by: Rillusion | last post by:
Hi,,, just want to know how to write a a fuction to print a matrix using a call as following: MatPrint(A,3,4); where A is the matrix name, 3 is the number of row, 4 is the number of...
1
Thekid
by: Thekid | last post by:
Hi, I found this sudoku solver online and it works good but I was wondering how I could get the answer that's in matrix form, to also print out in a single comma-delimited line, instead of 9 rows of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.