Thread: Minor Loop Question - Help!!!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Smile Minor Loop Question - Help!!!

    Below is the body of a program I am trying to write. It is supposed to request a number of receipts, accept the receipt values and output the total amount, the average amount, and the highest and lowest amounts. I have created a program with an if ... else loop which does everything but figure out the lowest amount correctly. Can somebody help me figure what I'm missing? I have consulted many resources and the book I'm learning from sucks. I know it is probably something pretty simple. Any help would be greatly apprecriated. Thanks.

    printf("how many rec. \n");
    scanf("%d",&nr);
    for ( i=0; i< nr; i++){
    printf("Enter item# %d>",i+1);
    scanf("%lf",&amt);printf("%6.2f\n",amt);
    sum= sum + amt;

    if (amt > highest)
    highest = amt;
    else if (amt <= highest)
    lowest = amt
    }

    /***** prints the sum, the average, and the high and low amounts ***/

    printf("-----\nyou spend $ %8.2f\n",sum);
    average = sum/nr;
    printf( "The average of your data is $%6.2lf\n", average);
    printf( "The highest amount you entered is $%6.8f\n", highest);
    printf( "The lowest amount you entered is $%6.8f\n", lowest);
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Set lowest to the max input amount when you initalize it and do:
    Code:
    else if(amt < lowest)
      lowest = amt;
    See if that works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM