Thread: Pls advise why is the output like this?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    39

    Pls advise why is the output like this?

    Code:
     1 #include <stdio.h>
      2 
      3 int main(void)
      4 {
      5     double num=0.000000, highest=0.000000, second_highest=0.000000;
      6     double total=0.000000, temp=0.000000;
      7 
      8     scanf("%lf", &highest);
      9     scanf("%lf", &num);
     10 
     11     if (num>highest)
     12     {
     13         temp=highest;
     14         highest=num;
     15         second_highest=temp;
     16 
     17     }
     18 
     19     else
     20     {
     21         second_highest=num;
     22     }
     23 
     24     while (num != 0)
     25     {
     26         scanf("%lf", &num);
     27         if (num>highest)
     28         {
     29             temp=highest;
     30             highest=num;
     31             second_highest=temp;
     32 
     33         }
     34 
     35         else if (num>second_highest)
     36         {
     37             second_highest=num;
     38         }
     39 
     40         total=total+num;
     41 
     42     }
     43 
     44     total= (total-highest-second_highest);
     45 
     46     printf("%.2lf\n", total);
     47 
     48 
     49 
     50 
     51     return 0;
     52 }
    Hi guys,

    I'm trying to create a programme that will sum up all user inputs except the largest two input value. The programme should run until the user input zero, and its output should be the sum of all inputs except the two largest values.

    Sample run:
    123.5
    414.5
    145
    98.4
    333
    206
    0

    Output: 572.90

    I do not understand why my code is giving wierd outputs...what's wrong with the logic?

    Pls advise.

    Thanks

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Firstly, you don't need all those zeros in your initializers! 0.0 is (more than) enough.

    Secondly, 572.9 is the correct answer.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    572.9 is the correct answer but when i run my programme...it gives 34.90...can u help me to trace my code and tell me what went wrong? Is the logic fine? Pls help.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Well, you're not actually adding in the first two numbers you read. Try putting total=highest+second_highest on line 23. (Unfortunately, I can't run your code because of the line numbers.)

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    39
    Oh right Thanks a million

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advise for the day.
    By Mario F. in forum General Discussions
    Replies: 8
    Last Post: 04-01-2010, 03:51 PM
  2. please advise....
    By manny in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 06:09 AM
  3. Need some advise
    By ILoveVectors in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2005, 09:24 AM
  4. Some Advise..
    By VaSt in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2003, 01:52 PM
  5. need a piece of advise here
    By asim0s in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2002, 10:03 AM