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

req help please, with program, max, min

P: 18
I am having a problem with this program, I have been trying to do it all week and am stuck. I am writing a program that will take ten values from an input file, find the max and min then print them to an output file. I have managed to open and input the values, as well as print them out, I have wrote a program that will find the max and min of ten values, but this is in the program. I need to turn the 10 values at the int line, and change them to the 'val' values that have been scaned in. how do i do this.

Thanks for your help
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3.  
  4.  
  5. {
  6.  FILE *fi;
  7.  FILE *fo;
  8. int val, val1, val2, val3, val4, val5, val6, val7, val8, val9;
  9. int i, d;
  10. int a[10] = { 12, -78, 888, 4, 234, 1, 30, 40, 22, 34 }; // this section must change to include the val values instead of the numbers.
  11. int max = a[0];
  12. int min = a[0];
  13.  
  14.  
  15. if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
  16.     fprintf(stderr,"ERROR opening file!\n"); return 1; }
  17. fscanf(fi,"%d %d %d %d %d %d %d %d %d %d", &val, &val1, &val2, &val3, &val4, &val5, &val6, &val7, &val8, &val9);
  18.  
  19.  
  20. for (i = 0; i < 10; i++)
  21. {
  22. if (a[i] > max)
  23. {
  24. max = a[i];
  25. }
  26. else if (a[i] < min)
  27. {
  28. min = a[i];
  29. }
  30.  
  31.  
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.    if ( (fo=fopen("outfile.dat","w")) == NULL ) {
  40.       printf("ERROR opening file!\n");
  41.       return 1;
  42.    }
  43.    fprintf(fo,"Maximum : %d\n %d\n %d\n %d\n %d\n %d\n %d\n %d\n %d\n %d\n,", val, val1, val2, val3, val4, val5, val6, val7, val8, val9);
  44.    fprintf(fo,"Maximum : %d\n", max); /* output text to stream */
  45.    fprintf(fo,"Minamum : %d\n", min);
  46.    fclose(fo);
  47.    return 0;
  48. }
Mar 8 '07 #1
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 1,510
why not include the reading of your data in the loop where you determine the maximum and minimum values, e.g.
Expand|Select|Wrap|Line Numbers
  1. if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
  2.     fprintf(stderr,"ERROR opening file!\n"); return 1; }
  3.  
  4. for (i = 0; i < 10; i++)
  5. {
  6. fscanf(fi,"%d", a[i]);  // read next value into a[i]
  7.  
  8. if (a[i] > max)
  9. {
  10. max = a[i];
  11. }
  12. else if (a[i] < min)
  13. {
  14. min = a[i];
  15. }
  16.  
  17.  
  18. }
  19.  
  20.  
Mar 8 '07 #2

P: 18
I am a bit confused still, how will i get it to read the next value in with a[1] with the loop. am just realy confused with this C language. lol.
Mar 8 '07 #3

Expert 100+
P: 1,510
I am a bit confused still, how will i get it to read the next value in with a[1] with the loop. am just realy confused with this C language. lol.
in
Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i < 10; i++)
  2. {
  3.    fscanf(fi,"%d", a[i]);  // read next value into a[i]
  4.  
i increments from 0 to 9 in steps of 1
i=0 on the first loop the first number is read into a[0]
i=1 on the second loop the next number is read into a[1]
i=2 on the third loop the next number is read into a[2]
..
i=9 on the last loop the next number is read into a[9]
Mar 8 '07 #4

P: 18
I have been trying this code for ages and cant seem to get it to work can someone help me please.

I am trying to get the max and min of inputted data.

This is what I have done so far and my head hurts.

Thanks

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main() {
  3.    FILE *fi;
  4.    FILE *fo;
  5.  
  6.    float val, maximum = 0, minimum = 10000000, sum=0.0;
  7.  
  8.    int i, n=10;
  9.  
  10.    // open input file and checks it opens ok//
  11.  
  12.    if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
  13.       fprintf(stderr,"ERROR opening file!\n");
  14.       return 1;
  15.    }
  16.    fscanf(fi,"%f",&val);
  17.  
  18.    //loop to calculate max and min values//
  19.  
  20.    while ( val >= -10000 )
  21.    {
  22.       for (i=0;i<n;i++)
  23.          if ( val > maximum ) maximum = val;
  24.          else if ( val < minimum ) minimum = val;
  25.       fscanf(fi,"%f",&val);
  26.    }
  27.  
  28.    //opens output file and checks to make sure it opens//
  29.  
  30.    if ( (fo=fopen("outfile.dat","w")) == NULL ) {
  31.       printf("ERROR opening file!\n");
  32.       return 1;
  33.    }
  34.    {
  35.       // prints values to the file//
  36.       fprintf(fo,"Maximum : %f\n", maximum); /* output text to stream */
  37.       fprintf(fo,"Minamum : %f\n", minimum);
  38.    }
  39.    fclose(fi);
  40.    fclose(fo);
  41.    return 0;
  42. }
Mar 8 '07 #5

Ganon11
Expert 2.5K+
P: 3,652
I had to make a couple changes to your program before it would work in my C++ compiler, but I found the problem.

Why are you using the while() loop as well as the for... loop? This keeps reading in values while value >= -10000 or some ridiculous value, but if there is no value like that in the file, it doesn't work.

Also, your for... loop has no curly brackets around it, so the only statement that gets executed is the if...else branch - the fscanf call is not included. Finally, you start by reading in a value, but then read in 10 more values (or try to) in the for...loop - if there are only 10 values, this will cause problems. You should start the index at 1 instead of 0 so that you read in 9 values after the first.
Mar 8 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
I have merged these threads due to their similar content and questions.
Mar 8 '07 #7

P: 18
I have changed the program slightly and have tried a new way as it was not working the other way. Can someone check it and see if there is any problems with it. I have only written it and cannot check it as am at home and not at uni.

Thanks for all your help


#include <stdio.h>
int main()


{
FILE *fi;
FILE *fo;

int i;
int a[10];
int max = a[0];
int min = a[0];

if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
fprintf(stderr,"ERROR opening file!\n"); return 1; }
fscanf(fi,"%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);


for (i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}


}


if ( (fo=fopen("outfile.dat","w")) == NULL ) {
printf("ERROR opening file!\n");
return 1;
}

fprintf(fo,"Maximum : %d\n", max); /* output text to stream */
fprintf(fo,"Minamum : %d\n", min);
fclose(fo);
return 0;
}
Mar 8 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
I have changed the program slightly and have tried a new way as it was not working the other way. Can someone check it and see if there is any problems with it. I have only written it and cannot check it as am at home and not at uni.

Thanks for all your help


#include <stdio.h>
int main()


{
FILE *fi;
FILE *fo;

int i;
int a[10];
int max = a[0];
int min = a[0];

if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
fprintf(stderr,"ERROR opening file!\n"); return 1; }
fscanf(fi,"%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);


for (i = 0; i < 10; i++)
{
if (a[i] > max)
{
max = a[i];
}
else if (a[i] < min)
{
min = a[i];
}


}


if ( (fo=fopen("outfile.dat","w")) == NULL ) {
printf("ERROR opening file!\n");
return 1;
}

fprintf(fo,"Maximum : %d\n", max); /* output text to stream */
fprintf(fo,"Minamum : %d\n", min);
fclose(fo);
return 0;
}
Looks good at first glance, but I would recommend running it through a compiler before you submit it.
Mar 8 '07 #9

P: 18
I have just ran the program the now but there is a slight problem, the max does not work but the min does, this is the code.

can someone help me please.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int main()
  3.  
  4.  
  5. {
  6. FILE *fi;
  7. FILE *fo;
  8.  
  9. int i;
  10. int a[10];
  11. int max = a[0];
  12. int min = a[0];
  13.  
  14. if ( (fi=fopen("rainfall.dat","r")) == NULL ) {
  15. fprintf(stderr,"ERROR opening file!\n"); return 1; }
  16. fscanf(fi,"%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
  17.  
  18.  
  19. for (i = 0; i < 10; i++)
  20. {
  21. if (a[i] > max)
  22. {
  23. max = a[i];
  24. }
  25. else if (a[i] < min)
  26. {
  27. min = a[i];
  28. }
  29.  
  30.  
  31. }
  32.  
  33.  
  34. if ( (fo=fopen("outfile.dat","w")) == NULL ) {
  35. printf("ERROR opening file!\n");
  36. return 1;
  37. }
  38. fprintf(fo,"Maximum : %d\n %d\n %d\n %d\n %d\n %d\n %d\n %d\n %d\n %d\n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9]);
  39. fprintf(fo,"Maximum : %d\n", max); /* output text to stream */
  40. fprintf(fo,"Minamum : %d\n", min);
  41. fclose(fo);
  42. return 0;
  43. }
Mar 9 '07 #10

Expert 100+
P: 1,510
you initialise max and min to a[0] before you read data into a[0] and it is a local variable its contents are undefined! Initialise max and min after you read your data
Expand|Select|Wrap|Line Numbers
  1. scanf("%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
  2.  
  3. int max = a[0];   // << initialse here!
  4. int min = a[0];
  5.  
  6. for (i = 0; i < 10; i++)
  7. {
  8. if (a[i] > max)
  9. {
  10. max = a[i];
  11. }
  12. else if (a[i] < min)
  13. {
  14. min = a[i];
  15. }
  16.  
Mar 9 '07 #11

Post your reply

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