count array value

This is a discussion on count array value within the C Programming forums, part of the General Programming Boards category; Iím having problem counting the value for my array. The program should work this way >open file text data (thousands ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Question count array value

    Iím having problem counting the value for my array.

    The program should work this way
    >open file text data (thousands of data containing integer and float)
    >for every 1-15 data (either integer or float)
    >read the 4th data value
    >compare the 4th data with the threshold value
    >if 4th data is less than threshold value
    >count, add 1 to sum
    >endloop
    >print the data sum that is lesser than threshold value
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int i,sum;
            float avg=5.828;
            float f[15];
    
            FILE *data;
    
            data = fopen("avg_data.txt","r");
    
            while(feof(data)==0)
            {
                    for( i=0; i<15; i++ )
                    {
                            fscanf(data," %f",&f[i]);
                    }
                    printf("%.2f\n",f[3]); 
            }
            if(f[3]< avg)
            sum++;
            printf("Number of data that is below average is %d\n",sum);
    
            return 0;
    }
    Thanks in advace!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    I don't quite understand what your data file looks like...a file of unknown length that consists of lines of 15 space-separated numeric values? Is that right?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    the file contains only numbers (integer and float)

    eg. 98 5 31 17.62 19.95 6.31 2.66 4.44 12.55 1.66 2.99 18.30 12.60 14.32 5.99 7.69 8.99 2.33 97 5 20 6.93 2.66 12.44 11.03 18.23 14.17 12.27 11.20 10.39 5.64 8.69 7.56 98 12 8 1.36 7.96 12.34 19.83

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Well, your sum incrementation is outside the loop so it will never be more than 1.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    well i tried changing it within the loop.. but it still does not count.

    It should count...

    eg.
    if data is < 5.282
    count 1
    loop again
    (condition true)
    count 2
    loop again
    (condition false )
    loop again
    (condition true)
    count 3
    How do I go about in counting the element in the array?
    It is such a bugger!

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Maybe this will solve your problem
    Code:
    #include <stdio.h>
    
    int main(void)
    {
            int i,sum=0;/*you must explicitly stated what value is sume*/
    		/*otherwise what is sum++ that is equal undefined value++ and that is unexpected;*/
            float avg=5.828f;/*this is float value*/
            float f[15],helper[3];
    		int j,flag=3;
    
    
            FILE *data;
    
            data = fopen("test.txt","r");
    
            while(feof(data)==0)
            {
    			sum=0;
                    for( i=0; i<15; i++ )
                    {
                            if((fscanf(data," %f",&f[i]))==EOF)
    						{	flag=i;
    							for(j=i;j<15;j++)
    								f[j]=avg+1;/*to ensure if there are less element in array because of EOF*/
    							break;
    						}
                    }
    				for(j=0,i=3;j<(flag<3 ? flag:3) ;j++,i+=4)/*if you don't want first element(f[0])*/
    					helper[j]=f[i];
    
    				for(j=0;j<(flag<3 ? flag:3);j++)
    				{
    					if(helper[j]<avg)
    						sum++;
    				}
    
                  printf("Number of data that is below average (among first 15 elements) is %d\n",sum);
            }
            
    
            return 0;
    }
    Try this, I don't have much time to test it properly. If problem still exists I'll try that later
    Last edited by Micko; 10-05-2004 at 03:03 AM.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    It should be continuous... as one line can have many numbers.
    As long as it falls on a block of 15 numbers it should read and compare the 4th number with the threshold value.

    Thanks for you help!

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    I think you are doing too much work Micko...

    For starters, in the while loop you initialize sum to 0, so every line read will reset the sum.

    Also, there is no need to keep an array of values since its only the 4th value were interested in. A loop something like this might work.

    Code:
    ...
    int count = 0;
    int sum = 0;
    float thresh = .58f;
    float value = 0.0f;
    int end = 0;
    
    while (feof(data)==0)
    {
        count = 0;
        while (count < 15)
        {
            if (fscanf(data," %f",&value) == EOF)
            {
                 end = 1;
                 break;
            }
            if (count == 3)
            {
               if (value < thresh)
               {
                  sum++;
               }
            }
             count++;
         }
         if (end)
         {
              break;
         }
    } 
    
    ...
    There is a FAQ about not using feof in a loop...here

    I didnt test this, but its a lot less work and will always check the fourth value read in a count of 15.

    PK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21