473,721 Members | 2,373 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 3975
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
4447
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
10394
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
6261
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
4845
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
2070
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
3808
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
3266
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
6183
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
8851
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9373
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
9227
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...
1
9138
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5992
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4491
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
4761
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3201
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
3
2137
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.