Help with function/arrays in program

This is a discussion on Help with function/arrays in program within the C Programming forums, part of the General Programming Boards category; Hey, I'm pretty new to programming in general and I would like some help on a program of mine. Basically ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    10

    Help with function/arrays in program

    Hey, I'm pretty new to programming in general and I would like some help on a program of mine. Basically i'm trying to use a set of data points that are stored in an array to use in a function. When the function returns its value, I get an error of +NAN(i think means not a number?). The function is called dc_offset. None of you can use the code as it requires a .txt file with data points to work but maybe someone can spot an error somewhere? Thanks in advance.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    int i;
    double sum;
    double dc_offset(double []);
    
    int main(void)
    {
    FILE *inputfp;
    double data[2048], time[2048];
    int  input_status;
    char file_name[15];
    
    printf("Enter the file name with the .txt extension: ");
    gets(file_name);
    
    inputfp = fopen(file_name, "r");
    for (i=0; i<2048; i++) {
    for (input_status = fscanf(inputfp, "%lf", &data[i]); input_status!=EOF; input_status = fscanf(inputfp, "%lf", &data[i]));
    }
    
    sum = dc_offset(data);
    printf("DC offset is: %.8lf\n", sum);
    
    fclose(inputfp);
    time[0]=0;
    for(i=1; i<2048; i++){
    	time[i] = time[i-1] + ((double)1/455);
    }
    return(0);
    }
    
    
    //Functions
    
    double dc_offset(double data[]){
    	for(i=0; i<2048; i++){
    		sum += data[i];
    	}
    return(sum);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not really sure why you're even returning anything. After all sum is already global. So what's the point in returning anything and assigning it?

    Let's simplify this program and see if it works. Skip your data set.
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    int i;
    double sum;
    double dc_offset(double []);
    
    int main(void)
    {
        double data[5] = { 1.2, 3.4, 5.6, 7.8, 9.0 };
    
        printf("DC offset is: %.8lf\n", sum);
    
        return(0);
    }
    
    void dc_offset(double data[])
    {
        for(i=0; i<5; i++)
        {
            sum += data[i];
        }
    }
    Now, ignoring the globals, gets, and your file, this is your code, cleaned a bit. Also, you make no check at all to even see if your array is full. However, later you assume it's full, and use all 2048 elements of your arrays.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    Hey, thanks a lot for your help and fast reply. Actually I'm not supposed to be even using global variables as a friend just told me. I inserted the modified code into mine and it did get rid of the +NAN error but it gave me a value of 0 for sum when it shouldn't. How could I get rid of the global variables like "sum" and replace it into the main function while not screwing up the program? Also, like you said I didn't check to see if the array was full but I put printf statement after:

    Code:
    inputfp = fopen(file_name, "r");
    for (i=0; i<2048; i++) {
    for (input_status = fscanf(inputfp, "%lf", &data[i]); input_status!=EOF; input_status = fscanf(inputfp, "%lf", &data[i]));
    }
    and it displayed all the values in each array. This means the arrays have been filled by the for loops correct? Thanks again!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Local variables aren't initialized to zero. Thus, every entry in the local array is going to have some value. What value exactly, is undefined. It could be anything. Here's an example:
    Code:
    #include<stdio.h>
    int main( void )
    {
        int x;
        printf("x is %d\n", x );
    
        return 0;
    }
    Who knows what x will be? It could be anything. The same thing will happen with your array. Each element will have some seemingly random value in it. So if your file doesn't contain at least 2048 entries, it'll simply use whatever value's in the ones you don't actually put anything in.

    You have another fun little thing going on. Your inner loop never changes i, so it just ends up overwriting that spot in the array over and over with the whole file. Then, it continues on to i = 1, and tries to do it again. Except now you're already at the end of the file...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    10
    Hi again, the file that gives the data for the data array has exactly 2048 entries. So I don't think it's filling in the arrays with random values because I checked and the values matched. But what I'm concerned with is: at the end of the two for-loops, does each element in the array have a value. I ask this because if i put a printf statement in the loop to show data[i], it shows data[0] to data[2047] with all the correct values. But since the dc_offset, which is just the addition of all the data values, is giving 0, I'm starting to wonder.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Indented so it's actually readable, look at what it does.
    Code:
    for (i=0; i<2048; i++) {
        for ( input_status = fscanf(inputfp, "%lf", &data[ i ]); /* fill data[ i ] with something */
              input_status != EOF; /* while the status isn't EOF... */
              input_status = fscanf(inputfp, "%lf", &data[ i ]) ) /* do it again... */
            ;
    }
    Indented so it's actually readable, look at what it does.

    1 - i is zero, it enters the second loop.
    2 - i is zero, it reads something from the file.
    3 - i is zero, if fscanf doesn't fail, continue...
    4 - i is zero, read from the file again, and put it in array[ i ]
    5 - When we reach the end of the file, do 1 - 4 another 2047 times.

    When i hits 1, you're already at the end of the file. So what gets put in that index?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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