Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Slight problem with grouping a set of numbers

  1. Dec 2, 2007 #1
    For some reason, the code I has compiles, but it just doesn't seem to do what it should. The code I have atm, involves opening a file of numbers, reading the numbers, and sorting them into groups.

    Code (Text):

    int nmax;               // This is the number of lines in the file
    int i;                     // Index
    float num; groups[15];

    // File is opened - I know thats not where the error is
    FILE *file;
    file = fopen("log.txt", "r");

    for(i=0; i<nmax; i++)
    fscanf(file, "%f\n", &num);

    case '0': groups[0]++; break;
    case '1': groups[1]++; break;
    /* etc... */
    Any ideas why it doesn't work. When I use another loop to print the values of groups to the screen, all the elements of the array are zero. which I think is rather odd.
  2. jcsd
  3. Dec 2, 2007 #2
    First off, it would help if before switch()ing you'd printf out the value of num. That way you'd know at least whether the numbers are being read in correctly.

    Second off, it kind of seems like you're mixing three different kinds of things. You're reading in a float, then casting it to an int (which is okay, but will cause you to always round decimal numbers down), but then in your switch statement you check against things like case '0' and case '1'? '0' is not equal to 0, it's a char ascii value and it's equal to like 65 or something.

    Also I don't see nmax being set anywhere?!
    Last edited: Dec 2, 2007
  4. Dec 4, 2007 #3


    User Avatar

    Coin's answers is very good!

    to Cinimod :
    there are no case matched if you use the switch (*some type of int*)
    than case: '1'.
    note that: '1' is a char who's size is ONE BYTE.
    and an int type always occupise FOUR BYTES.
    they are obviously different!
  5. Dec 6, 2007 #4

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    Giving you code is not the best idea but there are several issues with your code.
    This example reads freely from log.txt until it reaches either end of file or it has trouble converting the input. It does rudimentary error checking. It also assigns the group array in a more readable way. coin pointed out some of these issues.
    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>
    #define NMAX 25
    #define ck(X) \
      if((X)==NULL){ perror("input file error"); \

    int main()
        int groups[NMAX+1]={0};
        FILE *in=fopen("log.txt","r");
        float value=0.;
                 ck(in);   /* make sure we have a file to read */
        while (fscanf(in,"%f\n", &value)> 0) /* read until EOF or problems */
            if((int)value<=NMAX) /* limit check the values so you do not exceed array bounds */
        for(int i=0; i<NMAX; i++)
            printf("%3d groups: %3d\n", i, groups[i]);
        return 0;  

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Slight problem with grouping a set of numbers