Thread: Another Homework problem, finding and printing max value

  1. #1
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18

    Another Homework problem, finding and printing max value

    Hi all! So this homework problem is similar to that one I posted last time (that I completely gave up on! I will just take the hit on my grade). Anyway, my instructions are to find the average of column 2, the maximum, the location of the fist maximum, and the location of the last maximum of an external file. The number of the second column are only read in if the line in which they reside begin with a 1. So even though you would think 66 is the max, it is invalid because the first number is a 0.

    I have everything so far, except the last max. It is printing 0 instead of 7.

    The external file reads:
    0 2
    1 8
    0 66
    0 2
    1 6
    1 8
    1 8

    And here is my code:
    Code:
    int main(void)
    
    {
    int flag, x, count=0, j=1, flag1=0, max, locmax, last;
    float sum=0.0, ave;
    FILE *inf;
    
      inf = fopen("readme.dat", "r");
    
            while(fscanf (inf, "%i %i", &flag, &x )==2)
    {
          if (flag==1)
            {
            sum = sum + x;
            ++count;
            j++;
          if (flag == 0)
                    {
                    max=x;
                    flag1 = 1;
                    locmax = j;
                    }
          if (x>max)
                    {
                    max = x;
                    locmax = j;
                    }
            if (x>max)
                    {
                    max = x;
                    last = j;         <========= I am sure this is where my problem lies, I'm just not seeing what I am doing wrong
                    }
    
    }
    ave = sum/count;
    
    
    printf("Average = %7.2f \n", ave);
    printf("Max = %2i \n", max);
    printf("Loc of First Max = %2i \n", locmax);
    printf("Loc of Last Max = %2i \n", last);
    
    return 0;
    }
    Like I said, the program compiles correctly but it prints out 0 for the location of the last max. Any help would be greatly appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
          if (x>max)
                    {
                    max = x;
                    locmax = j;
                    }
            if (x>max)
                    {
                    max = x;
                    last = j;         <========= I am sure this is where my problem lies, I'm just not seeing what I am doing wrong
                    }
    The second if() statement is never true because if the first one is true, you're setting max equal to x. Then it gets to the second if() statement and max has already been modified.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User Aslan14's Avatar
    Join Date
    Mar 2009
    Posts
    18
    Oh yeah, I see what you're saying... I tried this, but it isn't working either. Been working on this for a couple of hours and my eyes are crossed!
    Code:
     if (a>max)
                    {
                    max = a;
                    last = max;
                    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First, set max to the first number, because it has to be the first max we get:

    In pseudo code:
    Code:
    if firstflag==0 
      set max=x
    else  //already a max, so test the x, is it  > max's current value?
      if x > max
        max = x
        firstmax = count
      end if
      else
        if x== max
          lastmax = count
    
        end if
      end else
    end else
    I don't quite understand what you are trying to do, so this may be way off base. Just a suggestion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing output according to letter chosen problem
    By Guti14 in forum C Programming
    Replies: 5
    Last Post: 08-12-2004, 03:13 AM
  2. Finding and Printing prime numbers using arrays
    By Sektor in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2003, 08:29 PM
  3. Finding a directory, and printing whats in it
    By booyah in forum C Programming
    Replies: 4
    Last Post: 11-12-2003, 11:52 AM