Counting number of elements for each row

May 22, 2013 at 2:39am
Hello.

I have the following text file:

54
164.3 201.6 226.7 63.4 71.2 210.2
94.8 147.7 47.9 155.5 212.5 184.9 182.4 187.8
60.3 59.1 64.7 141.7
215.1 83.2 72.5 118.0 192.3 184.9 174.5
232.6 99.5 238.2 84.1 65.4 145.1 167.3 128.2 168.1 188.2 203.7
210.7 83.1 190.1 121.2 67.3 62.2


My assignment is to make a ragged array. In order to allocate memory for rows, I need to take the first integer from first row and allocate. That's the easy part. However, in order to allocate memory for the remaining rows, I need to first get the number of elements for each row. The problem is that each row does not have the exact number of elements.

I can do the rest of the assignment (calculating the mean and sorting).

Please show me how to do bold portion of this assignment.

Thanks! :)


PS: Please write the code in C not C++.
Last edited on May 22, 2013 at 2:45am
May 22, 2013 at 3:00am
If you do not know the size before hand consider vector over array in your case it would be a vector of vectors
Last edited on May 22, 2013 at 3:01am
May 22, 2013 at 4:40am
Please write the code in C not C++.


You could just make a set size array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  //Take this example with a grain of salt; I am not very knowledgeable with C
const int rows(10);
const int columns(15);
float* f_ptr = malloc(rows*columns);
   //Initialize values
for(int I=0; I < rows*columns; ++I){
   *(f_ptr + I) = 0;
}
//...Retrieve info from file... 

for(int I = 0; I < rows*columns; ++I){
   if(
      ((f_ptr + I) != 0) &&
      (*(f_ptr+I) != 0)
   )
      //...do something
}
//...
free(f_ptr);
Last edited on May 22, 2013 at 9:21pm
May 22, 2013 at 8:27am
As it was already said you can use either a vector of vectors, or an array of vectors. The problem with an array of dynamically allocated arrays for rows is that you should keep sizes of rows in an additional separate array

To read a row you should use std::getline and then use std::istringstream to extract numbers.
May 22, 2013 at 8:43am
To read a row you should use std::getline and then use std::istringstream to extract numbers.

That would be the C++ way. The equivalents in C are probably fgets and sscanf. For counting values, could one count whitespace between them?
May 22, 2013 at 10:48pm
I need to first get the number of elements for each row.

Well, you're going to have to count them, then.

Read the file line into a char buffer, count the values on the row, and then convert and store them.

I can see that there are only 42 value which is bigger than the value of 53 you are given on the first line. And you don't even know how may rows you're expecting, so you can only allocate a linear array up front.

I assume you've been learning the (C !) techniques required to solve your problem. I take it you are planning to write (or by now, using) functions to manage access to your ragged array.

Andy

Last edited on May 22, 2013 at 10:51pm
May 30, 2013 at 6:38am
Thanks guys
Topic archived. No new replies allowed.