Need help with C programming!

Jordannn15

Honorable
Jun 2, 2012
17
0
10,560
Code:
/*
        program: proj4

        Name: Jordan Ward
        Email: jw363912@ohio.edu

        Description: This program reads from an input file and
                     prints out the min, max, and average of the numbers.
*/

#include <stdio.h>

int main(void)
{
        FILE *inp;
        FILE *out;

        int count = 0;
        int sum = 0;
        int min;
        int max;
        int input_status;

        inp = fopen("data.dat", "r");
        out = fopen("data.out", "w");

        input_status = fscanf(inp, "%d", &min);

        while(input_status == 10)

            count++;

            if(min > input_status)
               max = min;
            else if(min < input_status)
               max = input_status;

            sum++;

                                                                           [ Wrote 49 lines ]

bash-3.00$ !g
gcc assign4.c
bash-3.00$ gcc -Wall assign4.c
assign4.c: In function 'main':
assign4.c:49:1: warning: control reaches end of non-void function
bash-3.00$ nano assign4.c
  GNU nano 2.2.4                                           File: assign4.c

        int max;
        int input_status;

        inp = fopen("data.dat", "r");
        out = fopen("data.out", "w");

        input_status = fscanf(inp, "%d", &min);

        while(input_status == 1)

            count++;

           (not sure what to put here read my question down low)

            sum++;

            input_status = fscanf(inp, "%d", &min);


        fprintf(out, "Minimum value = %d", min);
        fprintf(out, "Maximum value = %d", max);
        fprintf(out, "Average value = %4lf", (double) (min + max) / 2);

        fclose(inp);
        fclose(out);
}

Okay this is an assignment and I have to make a pointer called whatever i want but i need two pointers, one for data file and one for an output file. I am having trouble in the while loop, I don't understand what to put in the () for the argument and then I don't understand how to find the min and max of my data file. Thanks!
 

Jordannn15

Honorable
Jun 2, 2012
17
0
10,560
Code:
/*
        program: proj4

        Name: Jordan Ward
        Email: jw363912@ohio.edu

        Description: This program reads from an input file and
                     prints out the min, max, and average of the numbers.
*/

#include <stdio.h>

int main(void)
{
        FILE *inp;
        FILE *out;

        int count = 0;
        int sum = 0;
        int min;
        int max;
        int input_status;

        inp = fopen("data.dat", "r");
        out = fopen("data.out", "w");

        input_status = fscanf(inp, "%d", &min);

        while(input_status == 1)

            count++;

            sum++;

            input_status = fscanf(inp, "%d", &min);


        fprintf(out, "Minimum value = %d", min);
        fprintf(out, "Maximum value = %d", max);
        fprintf(out, "Average value = %4lf", (double) (min + max) / 2);

        fclose(inp);
        fclose(out);
}
 

Sunius

Distinguished
Dec 19, 2010
390
0
19,060
I'll try to help you. I've rewritten some of your code:

Code:
#include <stdio.h>

int main()
{
	FILE *inp;
	FILE *out;
	int count = 0;
	int sum = 0;
	int min = 65535;    // Minimum and maximum values
	int max = -65536;
	int input;                // Input buffer
	int input_status;
	inp = fopen("data.dat", "r" );
	out = fopen("data.out", "w" );
	do
	{
		input_status = fscanf(inp, "%d", &input);
		if (input_status == 1)
		{
			count++;
			sum += input;
			if (min > input)
				min = input;
			if (max < input)
				max = input;
		}
	}
	while(input_status == 1);
	fprintf(out, "Minimum value = %d\n", min);
	fprintf(out, "Maximum value = %d\n", max);
	fprintf(out, "Average value = %4lf\n", (double)sum / (double)count);
	fclose(inp);
	fclose(out);
}

Changed the formula for Average value, changed loop to
Code:
do ... while
, added a variable for input buffer, set starting minimum and maximum values. Just ask if something does not work.

Here's the data I've tested it with.

data.dat
Code:
5 10 -100 15 30 60 80 1 0

data.out
Code:
Minimum value = -100
Maximum value = 80
Average value = 11.222222