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