473,602 Members | 2,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

routine to return number of rows and columns in a matrix

Anyone know of a routine that will return the number of rows and
columns in a matrix?

Aug 23 '06 #1
6 3966
kilter wrote:
Anyone know of a routine that will return the number of rows and
columns in a matrix?
As C has no matrices, you should explain further what you mean
and want.
Aug 23 '06 #2

Nils O. Selåsdal wrote:
kilter wrote:
Anyone know of a routine that will return the number of rows and
columns in a matrix?

As C has no matrices, you should explain further what you mean
and want.
Thanks for your quick reply. The idea is that I open a file that has
numerical data in a form of a matrix but I don't know a priori the
number of columns and rows of this file. I want to know the number of
rows and columns of that data matrix, after I read it and store it as a
matrix in C, in order to apply operations to its entries.

I am a novice really and your help is very much appreciated.

Thank you

Aug 23 '06 #3

kilter <ri************ *@btopenworld.c omwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .

Nils O. Selåsdal wrote:
kilter wrote:
Anyone know of a routine that will return the number of rows and
columns in a matrix?

As C has no matrices, you should explain further what you mean
and want.
The idea is that I open a file that has
numerical data in a form of a matrix but I don't know a priori the
number of columns and rows of this file. I want to know the number of
rows and columns of that data matrix, after I read it and store it as a
matrix in C, in order to apply operations to its entries.
This will count all the rows and columns in an OPENED FOR READING
COMMA-SEPARATED TEXT data file where the file consists of nothing
else but data rows (if your file has a different format, well, you'll have
to
modify it in some way):

#define LINEMAX 512

unsigned char_idx=0;
unsigned line_length,num _cols=0,num_row s=0;

fgets(line,LINE MAX,fl_strm);
num_rows++;

line_length=str len(line);
while(char_idx< line_length) {

if(line[char_idx]=='\n'||'\0') {
num_cols++;
break;
}

if(line[char_idx]==',') num_cols++;

char_idx++;
}

while((fgets(li ne,LINEMAX,fl_s trm))!=NULL)
num_rows++;

---
William Ernest Reid

Aug 23 '06 #4
kilter wrote:
Nils O. Selåsdal wrote:
>kilter wrote:
>>Anyone know of a routine that will return the number of rows and
columns in a matrix?
As C has no matrices, you should explain further what you mean
and want.

Thanks for your quick reply. The idea is that I open a file that has
numerical data in a form of a matrix but I don't know a priori the
number of columns and rows of this file. I want to know the number of
rows and columns of that data matrix, after I read it and store it as a
matrix in C, in order to apply operations to its entries.

I am a novice really and your help is very much appreciated.

Thank you
It depends. If the numerical data is actually stored as a (ascii or
similar) matrix, and each value is separated by spaces, tabs, what have
you, and rows are written line by line you read in one number at a time,
keep track and signal the EOL.
If it's in some binary/structured form you probably want to read in a
structure like that all at once.

If it's just a consecutive row of numbers in a flat file, you may have
to look for delimiter values in the data ending each row, and if these
aren't there you can't be sure how the matrix looks like, as long as
their are multiple m,n where m * n satisfies the total count of numbers
read.

You could have a look at sscanf(), scanf() functions on how to read in
data in a formatted way. Maybe browsing the c-faq, in particular chapter
12 http://www.c-faq.com/stdio/index.html isn't such a bad idea, it must
have some useful stuff on reading in date from files (or basically any
data stream, as C treats them all the same)

For a good primer on C, Kernighan and Ritchie still is considered of
unparallelled value by many, I my self have learnt(ed?) a great deal of
(the little bit of) what I know from
http://publications.gbdirect.co.uk/c_book/
Pretty dated stuff, but clear, concise and to the point nonetheless.

HTH
Sh.
Aug 23 '06 #5
Thanks very much for your replies, they are great!

Regards,

George
Schraalhans Keukenmeester wrote:
kilter wrote:
Nils O. Selåsdal wrote:
kilter wrote:
Anyone know of a routine that will return the number of rows and
columns in a matrix?
As C has no matrices, you should explain further what you mean
and want.
Thanks for your quick reply. The idea is that I open a file that has
numerical data in a form of a matrix but I don't know a priori the
number of columns and rows of this file. I want to know the number of
rows and columns of that data matrix, after I read it and store it as a
matrix in C, in order to apply operations to its entries.

I am a novice really and your help is very much appreciated.

Thank you
It depends. If the numerical data is actually stored as a (ascii or
similar) matrix, and each value is separated by spaces, tabs, what have
you, and rows are written line by line you read in one number at a time,
keep track and signal the EOL.
If it's in some binary/structured form you probably want to read in a
structure like that all at once.

If it's just a consecutive row of numbers in a flat file, you may have
to look for delimiter values in the data ending each row, and if these
aren't there you can't be sure how the matrix looks like, as long as
their are multiple m,n where m * n satisfies the total count of numbers
read.

You could have a look at sscanf(), scanf() functions on how to read in
data in a formatted way. Maybe browsing the c-faq, in particular chapter
12 http://www.c-faq.com/stdio/index.html isn't such a bad idea, it must
have some useful stuff on reading in date from files (or basically any
data stream, as C treats them all the same)

For a good primer on C, Kernighan and Ritchie still is considered of
unparallelled value by many, I my self have learnt(ed?) a great deal of
(the little bit of) what I know from
http://publications.gbdirect.co.uk/c_book/
Pretty dated stuff, but clear, concise and to the point nonetheless.

HTH
Sh.
Aug 23 '06 #6
kilter wrote:
Nils O. Selåsdal wrote:
>kilter wrote:
>>Anyone know of a routine that will return the number of rows and
columns in a matrix?
As C has no matrices, you should explain further what you mean
and want.

Thanks for your quick reply. The idea is that I open a file that has
numerical data in a form of a matrix but I don't know a priori the
number of columns and rows of this file. I want to know the number of
rows and columns of that data matrix, after I read it and store it as a
matrix in C, in order to apply operations to its entries.

I am a novice really and your help is very much appreciated.
Since you don't know the size in advance you will have to use dynamic
memory allocation, something that takes a bit of work to get right.
Since this is a 2D matrix you will also need to work out how to
dynamically allocate a 2D array. Question 6.20 of the comp.lang.c FAQ
will help with this. I suggest you also read through the rest of section
6 to help you avoid a lot of other potential areas of confusion.

If you can specify the format of the file it will simplify things if you
say that the first line just tells you how many rows and columns.

Reading the file can be done using fgets (don't use gets) and then parse
it one line at a time. Precisely how you parse it depends on how the
file is formatted.

This should get you started.

The comp.lang.c FAQ is available at http://c-faq.com/ and you should
always check it to see if it answers your questions.
--
Flash Gordon
Aug 23 '06 #7

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

Similar topics

2
4437
by: Neil Zanella | last post by:
Hello, I would like to know whether having a <table> element with two <tr> elements each containing one <td> elements and two <td> elements, respectively, is legal from the point of view of standard W3C HTML/XHTML. In particular, is it legal for table rows to contain differing number of columns, even when the colspan attribute is not used? Thanks,
4
10378
by: rod | last post by:
Hello SQL gurus! I am trying to write a query that will return a set of continguous rows from a table, and limit the number of rows returned when a maximum total has been reached by adding a value in one of the columns. For example, the two columns below represent 2 columns in a table. a 2 b 2
2
6258
by: Howard William | last post by:
Help. I am a bit flummoxed by the problem of how to transpose "normalized" (in the database sense) data records into columns of for a data entry form, and then back again when the user is finished. My previous solution to this was to filter by query or SQL for the observations for a particular set of criteria, then go through the resulting records and assign them to a non-normalized work table containing ONE RECORD with the rows x...
4
320
by: rburdette | last post by:
I need to do a sum of rows and sum of columns in Visual Basic. Is there another way to do it other than the one I have below? 5 7 3 9 12 4 8 9 13 4 0 -`1 -7 13 8 4 4 4 4 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
6
4835
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID SalesManName AT Alan Time
1
2066
by: k_baum217 | last post by:
I'm new to fortran 90 and I need a code which will read the number of rows and columns of a matrix of dimensions n x m. I understand how to get the number of rows, but can't figure out how to find the number of columns. This is part of the program which will find the number of rows, n: DO READ(13,*, iostat = error) IF (error == -1) EXIT
5
3798
by: A | last post by:
I am wondering if there's an efficient way of deleting rows or columns of a gsl_matrix? The only way I can think of is copying individual elements or may be rows, columns or sumatrices to a new matrix, then free the original matrix and have the pointer of the original matrix point towards the new matrix. Any suggestions for making this better? How does MATLAB do similar things in it's mwArray datatype? Thanks,
6
3261
by: Gary Wessle | last post by:
hi I have a data file with equal number of columns for each row. I need to get the number of rows and columns to allocate a matrix in gsl. getline (in, line) and parse the line for the number of spaces then add one to get the number of words then number_of_rows = 1; while getline (in, line) and number_of_rows++ does the number of rows I am not sure how to go about the number of words task, is there a c++
0
6177
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a discussion I have made a macro that works on a template spreadsheet with a fixed number of columns to be "macro'd" bounded by data that changes in its number of rows. That is, the bounding data is imported to the spreadsheet before the calculated columns are run. But the rows of this data...
0
8401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.