Find max profit among input salary

This is a discussion on Find max profit among input salary within the C Programming forums, part of the General Programming Boards category; Hi: I want to inputed salaries from loop and compare the max profit and min profit among salaries. When exit ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    10

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.
    Last edited by tabstop; 11-16-2008 at 08:58 AM.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Hi Tapstop:

    Thanks for your help. What does absolute values mean?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #8
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  2. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  3. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM
  4. how to find greatest input
    By m712 in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2002, 06:05 AM
  5. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21