File I/O & Array Problem!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nr7489
    New Member
    • Mar 2008
    • 2

    File I/O & Array Problem!

    Hi there. I have been given an assignment to do which counts towards my final grade but I'm struggling quite badly with it.
    THE 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


    Here is what I have so far...

    #include <stdio.h>

    int main(void)
    {
    FILE *fp = fopen("indata.t xt", "r");
    if (fp == NULL) {
    fprintf(stderr, "Can't open input file %s!\n", indata.txt);
    exit(1);
    }

    ofp = fopen("result.t xt", "w");
    if (ofp == NULL) {
    fprintf("Can't open output file %s!\n", result.txt);
    exit(1);
    }

    float min(float a[], int size) {
    assert(size < 0);
    float minVal = a[0];
    for (int i=1; i<size; i++) {
    if (a[i] < minVal) {
    minVal = a[i];
    }
    }
    return minVal;
    }//end min

    float max(float a[], int size) {
    assert(size > 0);
    float maxVal = a[0];
    for (int i=1; i<size; i++) {
    if (a[i] > maxVal) {
    maxVal = a[i];
    }
    }
    return maxVal;
    } /*end max*/


    fprintf("Minimu m = %f\n", minVal); /*prints min value*/
    fprintf("Maximu m = %f\n", maxVal); /*prints max value*/

    return 0;
    }

    If you could help me in any way with this I would be extremely grateful.

    Thanks
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    You need to ask a question. (A code dump is not a question.) You also need to learn to use CODE tags, because unformatted code is unreadable. From reading the forum rules, you should realize that we, like other technical forums, have pretty strict rules in helping with homework questions. Do not dump code, and sit back hoping for us to do the rest of the work.

    Comment

    • nr7489
      New Member
      • Mar 2008
      • 2

      #3
      Oh no thats not the case at all. I need help thats all need pointers and tips to help me take the problem forward.

      Comment

      • mac11
        Contributor
        • Apr 2007
        • 256

        #4
        Originally posted by nr7489
        Oh no thats not the case at all. I need help thats all need pointers and tips to help me take the problem forward.
        People are glad to help with a specific task if you narrow it down and explain your issue(s).

        What exactly is giving you trouble? What part of your code is broken? What don't you know how to do that you need to do?

        Comment

        Working...