# Thread: Find max profit among input salary

1. ## Find max profit among input salary

Hi:

I want to inputed salaries from loop and compare the max profit and min profit among salaries. When exit the loop should display the largest profit and smallest profit.

Problem occur when
1. Salary0 = 12000
2. Salary1 = 0
3. Salary2 = 6
4. Salary3 = 0
Output is
Max = 6.0
Min = 6.0

Can someone please help, I tried to fixed the problem many time still can't fix the problem.

See below for sample output and code:

Enter number of months in the financial period: 4
Enter the sales of month 1: (\$)12000
Enter the sales of month 2: (\$)0
Enter the sales of month 3: (\$)6
Enter the sales of month 4: (\$)0
Total sales: \$12006.000000
Average monthly sales: \$3001.500000
Maximum sales: \$12000.000000
Minimum sales: \$0.000000
Maximum sales rise: \$6.000000
Minimum sales drop: \$6.000000

Code:
```#include <stdio.h>

int main() {
int monthPeriod;
float salary;
float tempMin, tempMax;
float tempSalary;
float min, max;
float profit, tempProfit;
float maxProfit, minProfit;
float tmpMinProf, tmpMaxProf;
float sum;
int month;

printf("Enter number of months in the financial period: ");
scanf("%d", &monthPeriod);

if( monthPeriod < 0 )
sum = min = max = 0;

month = 1;
printf("Enter the sales of month %d: (\$)", month);
scanf("%f", &salary);

/* Initial values */
tempMin = salary;
tempMax = salary;
min = salary;
max = salary;

/*Copy value of salary; value will change in loop*/
tempSalary = salary;

/*Initial value for profit*/
tempProfit = tmpMaxProf = tmpMinProf = 0;

/* Month = 2; Start loop */
month++;
sum = salary;

while( month <= monthPeriod ) {
printf("Enter the sales of month %d: (\$)", month);
scanf("%f", &salary);

if( salary >= tempMax ) {
tempMax = salary;
max = tempMax;
}//End if

if( salary <= tempMin ) {
tempMin = salary;
min = tempMin;
}//End if

/*Find profit    */
profit = salary - tempSalary;
if(profit >= tempProfit) {
tmpMaxProf = profit;
maxProfit  = tmpMaxProf;
}//End if

if(profit < tempProfit) {
tmpMinProf = -profit;
minProfit  = tmpMinProf;
}

/*Update value*/
tempProfit = profit;
tempSalary = salary;

sum = sum + salary;
month++;
}/*End loop*/

printf("Total sales: \$%f\n", sum);
printf("Average monthly sales: \$%f\n", sum/monthPeriod);
printf("Maximum sales: \$%f\n", max);
printf("Minimum sales: \$%f\n", min);
printf("Maximum sales rise: \$%f\n", maxProfit);
printf("Minimum sales drop: \$%f\n", minProfit);

fflush(stdin);
getchar();
return 0;
}```

2. Enter number of months in the financial period: 4
Enter the sales of month 1: (\$)12000
Enter the sales of month 2: (\$)0
Enter the sales of month 3: (\$)6
Enter the sales of month 4: (\$)0
Total sales: \$12006.000000
Average monthly sales: \$3001.500000
Maximum sales: \$12000.000000
Minimum sales: \$0.000000
Maximum sales rise: \$6.000000
Minimum sales drop: \$6.000000
That's what I got, when I ran your code. Are you supposed to get something else?

3. > fflush(stdin);
See the FAQ.

What results were you expecting?

Having so may variables called 'temp' or 'tmp' doesn't make it easy to read.
Perhaps better names for things would help.

4. Hi all:

Thanks for your help. What went wrong with my code?

I am expecting

Enter the sales of month 1: \$12000
Enter the sales of month 4: \$0
Maximum sales rise: \$6.000000
Maximum sales drop: \$12000.0000000

Expected: Maximum sales drop: \$12000

Since:
1. MinSales = 0;
2. MaxSales = 12000
3. Difference = 0 - 12000 = -120000

5. If you want the maximum sales drop, why are you computing the minimum sales drop then? (And you're printing the word minimum too.)

Also: since you're storing the sales drop as a positive number (by using -profit), that test you're using will always be true when sales go down, since any negative number is less than any positive number. You need to be comparing absolute values.

6. Hi Tapstop:

Thanks for your help. What does absolute values mean?

7. Absolute value = distance from 0. So the absolute value of 5 is 5, since it is 5 units away from zero. And the absolute value of -5 is also 5, since it too is 5 units away from zero.

8. Hi Tapstop:

To check absolute value I can use either
1. abs(param1, parm2) in #include<math.h>;

or

2. use if statement like below:
if( -5.0 <= x && x <= 5.0)
some statement

where -5 is lower bound and 5 upper bound, number 5 is a selected num.

9. Well, since you need to check first whether profit is negative to find out that's its a drop anyway, you would know that the absolute value of profit is then -profit.