Hi all, can someone please help me with my code:

Its suppose to input a text file of values then display them and give the max and its index, and inputed values are floats.
But I can seem to get the max right.

Thanks

#include <stdio.h>

int main()

{
    FILE *in;

        float input[500];  // Allows the array to input a maximum of 500 values

          if
            ((in = fopen("input.txt", "r")) == NULL) // Opens the text file containing values

  {
      printf("The file cannot be opened\n");  // Displays message if text file cannot be opened

  }

      printf("Values from inputed text file:\n");

    int u = 1,c = 1;

         while
            (fscanf(in, "%f", &input[u])!=EOF)
  {
             printf("%f\n", input[u]);  // Displays the inputed values

    u ++;
    c = u;
  }

    float max = 0;

         for
           (u = 1; u <= c; u ++)
  {
         if
           (max < input[u])
  {

            max = input[u];
    c = u;
  }

  }
            printf("The max value is = %f and is at location %u\n", max, c);


   fclose(in);

   return 0;
}

What are you trying to do with the variables int u and int c?
It looks like you're trying to count the number of elements with both with u, but every time you increment it, you also make c the same value.

In your for loop, you're setting c to u, thus making the loop terminate instantly. Because you initialize u to 1 when you declare it (and also in your for loop), you're actually leaving the first element of input unused. Give your variables more descriptive names and you'll be able to figure out the problem much easier.

I dont know how else to do it because im using floating numbers, everytime i try something else i just get 0 everywhere, I have been trying to sort out this one issue for the past 3 hours. Would you mind please just editing for me what will make it work and explain wht you did??

From line 31:

float max = 0;
for (u = 1; u <= c; u ++)
{
    if (max < input[u])
    {
        max = input[u];
        c = u;                              // ** means that c is updated to u, hence the loop will break, so discard this line.
    }
}

Also you have at line 10:

if ((in = fopen("input.txt", "r")) == NULL) // Opens the text file containing values
{
    printf("The file cannot be opened\n"); // Displays message if text file cannot be opened
    return 0;                              // ** add this otherwise you'll needlessly proceed to the next part of the code.
}

That works, thanks. One question though? why is it telling me its location is at 102 when its at 41?

It seems to be displaying the amount of values in the array and not the location of the maximum

You need to declare a variable that actually stores the position of the maximum. Right now, you're using c, which is equal to the number of values you have stored, so that's exactly the number it's gonna print out. If I were to rewrite this program, I would even go so far as to get rid of max altogether and just store the index of the maximum value (while keeping in mind that arrays in C start at 0, not 1)

#include <stdio.h>

int main()
{
    FILE *infile;
    float input[500];   // Allows the array to input a maximum of 500 values

    int datacount = 0;  //Number of values stored.
    int rover = 0;      //An iterator

    int maxposition = 0;    //Initialized to the first element.

//Terminate the program if the file can't be opened.
    if((infile = fopen("input.txt", "r")) == NULL)
    {
        printf("The file cannot be opened\n");
        return 1;   //Return 1 for failure.
    }

    printf("Values from inputed text file:\n");

//Read the data from input.txt and print it out to the screen.
    while(fscanf(infile, "%f", &input[datacount]) != EOF)
    {
        printf("%f\n", input[datacount]); // Displays the inputed values
        datacount++;
    }

//Rover through the data and find the maxiumum value.
    for(rover = 0; rover < datacount; rover++)
    {
        if(input[maxposition] < input[rover])
            maxposition = rover;
    }

//Print out the result and exit.
    printf("The max value is = %f and is at location %u\n", input[maxposition], maxposition+1);
    fclose(infile);

    return 0;
}

EDIT: Ugh, I really don't remember how to get syntax highlighting to work on this website anymore.

Thnks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.