0

I'm new to C so sorry in advance if this is so obvious.

Basically, I'm trying to take an input like below and put into a 2d array.

C 3.25 18. 0.01 .01 .02 .04 .08 .02 .02
A 0 7.5 .054 .031 .016 .008 .116 .124 .147
D -1.5 0.5 .012 .025 .05 .1 .1 .1 .025 

I wrote a function in order to know the number of variables(so the number of rows) and I've taken the number of floating points.

count = numofvar(tokens);
scanf("%d %ld", &count_inter, &count_exper);
float data[count][count_inter + 3];

for(i=0; i<count; i++)
{
    for(j=0; j<count_inter+3; j++)
    {
        scanf("%f", &data[i][j]);
    }
}

printf("\n");

for(i=0; i<count; i++)
{
    for(j=0; j<count_inter+3; j++)
    {
        printf("%f ", data[i][j]);
    }
    printf("\n");
}

Besides the floats, I'm also trying to store the letters in the beginning in to the array. (To understand what these values correspond to). However I get this weird output:

-3.817211 0.000000 0.000000 0.000000 -3.817268 0.000000 -3.817295 
0.000000 0.000000 -3.817238 0.000000 -3.817232 0.000000 0.000000 
-309364117239383605807310438400.000000 0.000000 20366038377015803904.000000 -65873193319006208.000000 0.000000 0.000000    -311890318855195825296505831424.000000 
pc:~$ A 0 7.5 .054 .031 .016 .008 .116 .124 .147
A: command not found
pc:~$ D -1.5 0.5 .012 .025 .05 .1 .1 .1 .025 

What should I do?

pengs
  • 29
  • 2
  • 1
    You cannot store the letters. Your array contains `float`s. – Marievi May 08 '17 at 13:18
  • Thanks, is there a way for me to store these values with this kind of input format? – pengs May 08 '17 at 13:23
  • 1
    What letters? Where do you read and store letters? You don't do this anywhere in the posted code. – Lundin May 08 '17 at 13:24
  • By the way at line `float data[count][count_inter + 3]` the `count` and the `count_inter` must be integer constants greater than zero. – Akira May 08 '17 at 13:29
  • @Akira: are you using ante-diluvian C? C99 and later allows the code shown. – Jonathan Leffler May 08 '17 at 13:47
  • You need to check the return value from `scanf()` in your code, but you need to revisit your code. If you want line-based input (and your data does require it, really), then you need to use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html) to read the lines, and then [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) to read the data (see [Using `sscanf()` in a loop](http://stackoverflow.com/questions/3975236/) for details on how). – Jonathan Leffler May 08 '17 at 13:49

2 Answers2

2

Your code to parse the input lines appears to have no relationship to the input format, so you have work to do there. I'd suggest putting the storage arrays to one side for a while, and just concentrating on writing a program that reads the input and writes it out with annotations, e.g.

Label: C
List length: 9 
1: 3.25 
2: 18.0
3: 0.01 
... etc.

... just to prove to yourself that you can read the input format.

Then you need to find a way to store your values. You can't store a char in a float[] or a float in a char -- or at least, not without some dirty tricks like casting.

Avoid dirty tricks. The language's type system is there to help you. Don't fight it.

Better to store the labels in a separate array, so:

 char labels[count];
 float labels[count][count_inter];

This should be enough to get a working program going.

There are other approaches, using constructs such as struct and union. When you learn about these, you could perhaps come back to this program and experiment with those as ways to make the code more elegant.

slim
  • 40,215
  • 13
  • 94
  • 127
  • Overall, the right approach. Yet certainly code _can_ store a `char` in a `float` simply with `char ch = some_float;` with no cast and no expected loss of information and well defined behavior. IAC, that is not OP's best approach and this answer outlines a good alternative. – chux - Reinstate Monica May 08 '17 at 14:11
  • @chux Well defined perhaps, but still not a good idea. `float f = 'c'; char c = 0.3; printf("%c %0.2f\n", (char) f, (float) c);` prints `c 0.00` for me. (So forcing a `char` value into a `float` variable works, at least, but you have to cast on the way out. Omitting the casts results in compiler warnings and unwanted output I can't be bothered to diagnose. – slim May 08 '17 at 15:14
-1

Set the data type of float to char. Then you can store both alphabets and numbers.

Instead of:

float data[count][count_inter + 3];

Use this:

char data[count][count_inter + 3];
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278