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

max min values?

P: 4
Hi,

Im relatively new to C programming and have an assignment due for next week

Task

Write a program which reads 10 numbers from a file "indata.txt", finds the minimum and maximum value, and writes them out to a file "result.txt" within a message.
For example, if the content of indata.txt is
4 9 2 -10 8 126 0 45 -313 92
the content of result.txt must be
The minimum value found is -313, the maximum 126
Both files must be in your working directory; do not use full path names in fopen().

Any help would be much appreciated,

thanks
Mar 5 '07 #1
Share this Question
Share on Google+
9 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hi,

Im relatively new to C programming and have an assignment due for next week

Task

Write a program which reads 10 numbers from a file "indata.txt", finds the minimum and maximum value, and writes them out to a file "result.txt" within a message.
For example, if the content of indata.txt is
4 9 2 -10 8 126 0 45 -313 92
the content of result.txt must be
The minimum value found is -313, the maximum 126
Both files must be in your working directory; do not use full path names in fopen().

Any help would be much appreciated,

thanks
So what have you tried on this, where are you stuck?
Mar 5 '07 #2

P: 4
the problem is, i dont know where to start. i know the task seems relatively simple but this is my first ever C programming assignment at college and im really stumped.
Mar 5 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
the problem is, i dont know where to start. i know the task seems relatively simple but this is my first ever C programming assignment at college and im really stumped.
Then you need to start out with your algorithm - develop a list of steps that you anyone can do, by hand or by computer, to figure this out.
Mar 5 '07 #4

P: 84
<Full Coded Solution has been removed. Please see our FAQ>

The text file should be something like this, otherwise it will not work


//Leave the first line empty
125
-474
16
5
6
21
45
21
748
12
//Leave the last line empty

Hope this works!!!!!!!!!!!!!!!!!!!!!!!!
Mar 6 '07 #5

P: 4
So what have you tried on this, where are you stuck?
hi,

sorry for the late reply. ok my code so far goes:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int array[10]; /* Declare array */
  4.  
  5. int main(void)
  6. {
  7.  
  8.  file *fi; /* Input file pointer */
  9.  fi=fopen("result.txt","r");
  10.  
  11.  
  12. }
  13.  fscanf(fi, "%d %d %d %d %d %d %d %d %d %d", 
  14.    array, array+1, array+2, array+3, array+4, array+5, array+6, array+7, array+8, array+9);
  15.  
  16. if (array[0]<array[1])
  17.   max=array[1];
  18. if (array[1]<array[2])
  19.   max=array[2];
  20. if (array[2]<array[3])
  21.   max=array[3];
  22. if (array[3]<array[4])
  23.   max=array[4];
  24. if (array[4]<array[5])
  25.   max=array[5];
  26. if (array[5]<array[6])
  27.   max=array[6];
  28. if (array[6]<array[7])
  29.   max=array[7];
  30. if (array[7]<array[8])
  31.   max=array[8];
  32. if (array[8]<array[9])
  33.   max=array[9];
  34. if (array[9]<array[10])
  35.   max=array[10];
  36.  
i dont know where to go from here though...
Mar 6 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
hi,

sorry for the late reply. ok my code so far goes:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int array[10]; /* Declare array */
  4.  
  5. int main(void)
  6. {
  7.  
  8.  file *fi; /* Input file pointer */
  9.  fi=fopen("result.txt","r");
  10.  
  11.  
  12. }
  13.  fscanf(fi, "%d %d %d %d %d %d %d %d %d %d", 
  14.    array, array+1, array+2, array+3, array+4, array+5, array+6, array+7, array+8, array+9);
  15.  
  16. if (array[0]<array[1])
  17.   max=array[1];
  18. if (array[1]<array[2])
  19.   max=array[2];
  20. if (array[2]<array[3])
  21.   max=array[3];
  22. if (array[3]<array[4])
  23.   max=array[4];
  24. if (array[4]<array[5])
  25.   max=array[5];
  26. if (array[5]<array[6])
  27.   max=array[6];
  28. if (array[6]<array[7])
  29.   max=array[7];
  30. if (array[7]<array[8])
  31.   max=array[8];
  32. if (array[8]<array[9])
  33.   max=array[9];
  34. if (array[9]<array[10])
  35.   max=array[10];
  36.  
i dont know where to go from here though...
Ok, so to start out, I'd recommend moving the closing bracket from above the fscan to below the last line of your code (which, as you declared int main, should be return 0;). You can move the array declaration inside main, but it's up to you - I think that will still work declared outside.

From there, I think the next thing should be to focus on arrays. I would recommend this tutorial on arrays - your fscan line will definitely throw an error.

Try cleaning that up, and let us know how that goes.
Mar 7 '07 #7

P: 4
Hi again, i had a go at computing the min and max values and printing them out. but i think i might have gone a bit off. Bearing in mind this is my first c program...i need help! lol

heres what i got:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int array[10]; /* Declare array */
  4.  
  5. int main(void)
  6. {
  7.  
  8. file *fi; /* Input file pointer */
  9. fi=fopen("result.txt","r");
  10.  
  11.  
  12.  
  13.  fscanf(fi, "%d %d %d %d %d %d %d %d %d %d", 
  14.    array, array+1, array+2, array+3, array+4, array+5, array+6, array+7, array+8, array+9);
  15.  
  16. if (array[0]<array[1])
  17.   max=array[1];
  18. if (array[1]<array[2])
  19.   max=array[2];
  20. if (array[2]<array[3])
  21.   max=array[3];
  22. if (array[3]<array[4])
  23.   max=array[4];
  24. if (array[4]<array[5])
  25.   max=array[5];
  26. if (array[5]<array[6])
  27.   max=array[6];
  28. if (array[6]<array[7])
  29.   max=array[7];
  30. if (array[7]<array[8])
  31.   max=array[8];
  32. if (array[8]<array[9])
  33.   max=array[9];
  34. if (array[9]<array[10])
  35.   max=array[10];
  36. }
  37.  
  38.  min = max = X[0];        /* loop to calculate min and max*/ 
  39.   minpos = maxpos = 0;
  40.   for (i=0; i<N; i++)
  41.   {
  42.     if (X[i] <= min)
  43.     {
  44.       min = X[i];
  45.       minpos = i;
  46.     }
  47.     if (X[i] >= max)
  48.     {
  49.       max = X[i];
  50.       maxpos = i;
  51.     }
  52.  
  53.   }
  54.  
  55.   printf("Minimum  = %f (position = %ld) \n", min, minpos); /*prints min value*/
  56.   printf("Maximum  = %f (position = %ld) \n", max, maxpos); /*prints max value*/
  57.  
  58.   return 0;
  59. }
  60.  
'
Mar 7 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
Hi again, i had a go at computing the min and max values and printing them out. but i think i might have gone a bit off. Bearing in mind this is my first c program...i need help! lol

heres what i got:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int array[10]; /* Declare array */
  4.  
  5. int main(void)
  6. {
  7.  
  8. file *fi; /* Input file pointer */
  9. fi=fopen("result.txt","r");
  10.  
  11.  
  12.  
  13.  fscanf(fi, "%d %d %d %d %d %d %d %d %d %d", 
  14.    array, array+1, array+2, array+3, array+4, array+5, array+6, array+7, array+8, array+9);
  15.  
  16. if (array[0]<array[1])
  17.   max=array[1];
  18. if (array[1]<array[2])
  19.   max=array[2];
  20. if (array[2]<array[3])
  21.   max=array[3];
  22. if (array[3]<array[4])
  23.   max=array[4];
  24. if (array[4]<array[5])
  25.   max=array[5];
  26. if (array[5]<array[6])
  27.   max=array[6];
  28. if (array[6]<array[7])
  29.   max=array[7];
  30. if (array[7]<array[8])
  31.   max=array[8];
  32. if (array[8]<array[9])
  33.   max=array[9];
  34. if (array[9]<array[10])
  35.   max=array[10];
  36. }
  37.  
  38.  min = max = X[0];        /* loop to calculate min and max*/ 
  39.   minpos = maxpos = 0;
  40.   for (i=0; i<N; i++)
  41.   {
  42.     if (X[i] <= min)
  43.     {
  44.       min = X[i];
  45.       minpos = i;
  46.     }
  47.     if (X[i] >= max)
  48.     {
  49.       max = X[i];
  50.       maxpos = i;
  51.     }
  52.  
  53.   }
  54.  
  55.   printf("Minimum  = %f (position = %ld) \n", min, minpos); /*prints min value*/
  56.   printf("Maximum  = %f (position = %ld) \n", max, maxpos); /*prints max value*/
  57.  
  58.   return 0;
  59. }
  60.  
'
Ok, that's looking better - but you still need to change your fscan(). Right now, the calls to array are not accessing anything because they don't have an index - the compiler will get mad at you there.

(And with your second try on the for loop - you can remove the if statements above - it only compares one element with its next, it doesn't do a comparison with element 1 to, say, element 4 or 5, etc...)
Mar 7 '07 #9

P: 13
you can read the content from indata.txt to a char[].and analysis it by LOOP.
The problem is the content of indata.txt must be fixed format, at less must have a known char(s) between each number, or you can not split the char[] and get the number .
Mar 8 '07 #10

Post your reply

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