0

I'm having trouble scanning values from a text file, I've never really used file i/o before and I'm still very shaky with user-defined functions so I'm almost positive that the error is in the first function, I don't know how to fix it however, I'm pretty new at programming so please bear with me.

my code so far is

  #include <stdio.h>
  #include <stdlib.h>

int read_temps(void)
{
    int i;
    int num;
    int fileArray[25];

    FILE *file;
    file=fopen("temperatures.txt", "r");

    if (file == NULL)
    {
        printf("Error Reading File\n");
        exit(0);
    }
    for (i=0; i<25; i++)
    {
    fscanf(file,"%d", &fileArray[i]);
    }

    fclose(file);
    return fileArray[i];

}


int calc_results (int a[], int n)
{
    int i, j, maximum, minimum, average,sum;
    float avg;

printf ("Temperature conditions on October 26th, 2015:\n");
 printf("Time  Temperature in Degrees F \n");

   for(i = 0; i<25;i++)
    {
    printf("%d\t%d\n", i, a[i]);

        if(maximum < a[i])
            maximum = a[i];
        if(minimum > a[i])
            minimum = a[i];

            sum+=a[i];
    }

    printf("Maximum Temperature for the day: %d\n", maximum);
    printf("Minimum Temperature for the day: %d\n", minimum);

    avg=(float)sum/25;
    printf("Average Temperature for the day: %f\n", avg);

    return 0;
}


int main ()
{
    int average,i;

    int temp[25];
        for (i=0;i<25;i++)
        temp[i]= read_temps(); //calling user function for file input

calc_results(temp,i); //calling user function

return 0;
}

the program is supposed to read the values from the .txt file in read_temps, calculate the max, min, and average value in calc_results. It does all this and compiles, but it doesnt read the values correctly. The .txt file contains numbers like 87, 65, 92 and when I run my program the output is the same large number like 19863578 repeated for almost all the values, expect for max, min and avg. I don't know what to do :(

these are the values on the textfil

    67
    67
    69
    71
    77
    79
    80
    80
    82
    85
    87
    88
    91
    93
    94
    95
    95
    96
    94
    90
    85
    82
    81
    77
    74

the output should be

    Temperature Conditions on October 9, 2015: 
    Time of Day     Temperature in degrees F 
    0               85
    1               80
    2               97
    3               90
    4               73
    ........
    25              68
3
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. Commented Oct 28, 2015 at 17:27
  • Possible duplicate of Reading from a File to an Array - C Commented Oct 28, 2015 at 17:29
  • do you have a fixed pattern of number in every line? if yes then what is that? cos it is important. Commented Oct 28, 2015 at 17:29

2 Answers 2

1

One mistake in your code is that You are calling

for (i=0;i<25;i++)
temp[i]= read_temps();

in your main function. And performing

for (i=0; i<25; i++)
    {
    fscanf(file,"%d", &fileArray[i]);
    }

in your read_temps function which will iterate 25 times all the time and return you the garbage value after the last integer in the file always.

Sign up to request clarification or add additional context in comments.

2 Comments

How would you properly write it though?
mly128 you can look my answer
0

It's because you return fileArray[i]; after for loop, when i = 25, and there is no such value in array

write instead:

    int* read_temps(void)
{
    int i;
    int num;
    int fileArray[25];

    FILE *file;
    file=fopen("temperatures.txt", "r");

    if (file == NULL)
    {
        printf("Error Reading File\n");
        exit(0);
    }
    for (i=0; i<25; i++)
    {
    fscanf(file,"%d", &fileArray[i]);
    }

    fclose(file);
    return fileArray;

}

and

int* temp;
temp = read_temps();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.