By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

Having problems with inputting data and then using it, please help

P: 18
I have been trying this program for ages and am not getting very far. I am trying to input data from a seperate file and use it to work out the max and min values of the data. I have done the second part of this program as well that asks for it to be printed out to a seperate document, but have not combined them yet, well until this part works.

This is what I have managed to do,

Can someone help me please as am really confused,

Thanks for your help

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main() {
  3.   FILE    *fi;
  4.   float  val, maximum = 0, minimum = 0, sum=0.0;
  5.   int     i, n=10;
  6.   if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
  7.     fprintf(stderr,"ERROR opening file!\n"); return 1; }
  8.     fscanf(fi,"%f",&val);
  9.  
  10.     while ( val >= 0 ) {
  11.        for (i=0;i<n;i++)
  12.      if ( val > maximum ) maximum = val;
  13.      else if ( val < minimum ) minimum = val;
  14.     fscanf(fi,"%f",&val);
  15.   }
  16.   {
  17.  printf(" min=%f, max=%f\n"
  18.         ,minimum,maximum,);
  19.   }
  20.   fclose(fi);
  21.   return 0;
  22. }
Mar 7 '07 #1
Share this Question
Share on Google+
3 Replies


P: 94
Code tags aren't there for you health, it's so other people can understand what you are trying to say, so please use them!
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main() {
  3.   FILE    *fi;
  4.   float  val, maximum = 0, minimum = 0, sum=0.0;
  5.   int     i, n=10;
  6.   if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
  7.     fprintf(stderr,"ERROR opening file!\n"); return 1; }
  8.     fscanf(fi,"%f",&val);
  9.  
  10.     while ( val >= 0 ) {
  11.        for (i=0;i<n;i++)
  12.      if ( val > maximum ) maximum = val;
  13.      else if ( val < minimum ) minimum = val;
  14.     fscanf(fi,"%f",&val);
  15.   }
  16.   {
  17.  printf(" min=%f, max=%f\n"
  18.         ,minimum,maximum,);
  19.   }
  20.   fclose(fi);
  21.   return 0;
  22. }
Mar 7 '07 #2

Banfa
Expert Mod 5K+
P: 8,916
Your only real error looks like it is that you have a , after maximum in your printf line.
Mar 7 '07 #3

P: 9
Hi,
try these changes to your code.

#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 ) && (!feof(fi))) {
for (i=0;i<n;i++) {
if ((minimum == 0.0) && (val > minimum)) minimum = val;
if ( val > maximum ) maximum = val;
if ( val <= minimum ) minimum = val;
}
fscanf(fi,"%f",&val);
}
{
printf(" min=%f, max=%f\n",minimum,maximum);
}
fclose(fi);
return 0;
}

regards,
andersod
Mar 9 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.