473,378 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

req help please, with program, max, min

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
10 2743
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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
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
3,652 Expert 2GB
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
4,677 Expert Mod 4TB
I have merged these threads due to their similar content and questions.
Mar 8 '07 #7
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
4,677 Expert Mod 4TB
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
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
horace1
1,510 Expert 1GB
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

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

Similar topics

20
by: da Vinci | last post by:
Hello again. I have a question regaring pass-by-reference and multiple functions. This is an assignment that I have to use pass-by-reference for everything. First off, I made the following...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
3
by: promiscuoustx | last post by:
I am trying to get my program to compile, but it will not complete. At line 79 it states, cannot convert 'float()()' to 'float' in assignment. Here is my code. #include <iostream> #include...
0
by: raa abdullah | last post by:
Overview A conflict-serializbility\recoverability checker, SCR, is a program that takes in a schedule and outputs 2 decisions: Conflict serialzable or Not confilict serializable AND Recoverable or...
2
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really...
4
by: fatboySudsy | last post by:
Hi, I have constructed a client program that has given me some error codes that i just cannot see. I was wondering if a different set of eyes with much more experience than me could help me out. ...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
5
by: mohammaditraders | last post by:
Question # 1 Write a program which consists of a class named Student, the class should consists of three data members Name, Ob_marks, Total_marks and two member functions...
6
by: fido19 | last post by:
Once upon a time, there lived a chimpanzee called Luycha Bandor (aka Playboy Chimp). Luycha was unhappily married to Bunty Mona, a short but cute little lady chimp. Luycha was tall and handsome –...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.