1 Replies - 763 Views - Last Post: 07 March 2007 - 06:32 AM Rate Topic: -----

#1 jrt6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 14-February 07

Having problems with inputting data, please help

Posted 07 March 2007 - 06:18 AM

I have been trying to run this program for days and I have almost given up. I am just a beginner and am having difficulties trying to input data from another file, then calculate the max and min.

I have wrote the second part of the program that will write it to another document but have not merged them together, as I cannot do the first part.

This is what I am having problems with

can someone please check it and try and fix it please.
Thanks for your help
#include <stdio.h>
int main() {
  FILE	*fi;
  float  val, maximum = 0, minimum = 0, sum=0.0;
  int	 i, n=10;
  if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
	fprintf(stderr,"ERROR opening file!\n"); return 1; }
	fscanf(fi,"%f",&val);

	while ( val >= 0 ) {
	   for (i=0;i<n;i++)
	 if ( val > maximum ) maximum = val;
	 else if ( val < minimum ) minimum = val;
	fscanf(fi,"%f",&val);
  }
  {
 printf(" min=%f, max=%f\n"
		,minimum,maximum,);
  }
  fclose(fi);
  return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with inputting data, please help

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Having problems with inputting data, please help

Posted 07 March 2007 - 06:32 AM

there is a , after maximum in the printf() statement and you need to initialise minimum to some large value, e.g.
float val, maximum = 0, minimum = 10000000, sum=0.0;


otherwise your program will work. If I run the program with rainfall.dat containing
1
2
3
6
89
7
5
56
-1


the output is then
min=1.0000, max=89.0000

This post has been edited by horace: 07 March 2007 - 02:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1