Question

1) I want to create a (ersatz of) solver, where a simple expression such as :

Revenue = Cost * (1 + GrossMargin)

would lead to 3 questions to the user :

  • Revenue ?
  • Cost ?
  • GrossMargin ?

the user being asked to answer 2 out of those 3 questions, and the program calculating the third.

This is my (utter newbie) attempt :

//
//  main.c
//  LearningC
//  Created by ThG on 19/10/13
//

#include <stdio.h>

int main(int argc, char *argv[])
{
    // Variables
    float Cost = 0;
    float GrossMargin = 0;
    float Revenue = 0;

    // Ask the User
    printf("What is your Revenue ? : ");
    scanf("%f", &Revenue);
    printf("What is your cost ? : ");
    scanf("%f", &Cost);
        printf("What is your Gross Margin goal ? : ");
    scanf("%f", &GrossMargin);

    // Equation
    Revenue = Cost * (1 + GrossMargin);

    // Nested ifs or expressions "Solver"
    if (Revenue == 0)
    {
        printf("Your Revenue should be : %.2f $ * (1 + %.2f) = %.2f $\n", Cost, GrossMargin, Revenue);
    }
        else
        {
            if (Cost == 0)
                printf("Your Cost should not be higher than : %.2f $ / (1 + %.2f) = %.2f $\n", Revenue, GrossMargin, Cost);
            else
                printf("Your GrossMargin is : %.2f $ / %.2f -1 = %.2f $\n", Revenue, Cost, GrossMargin);
        }

    return 0;    
}

Of course, this failed miserably (btw, what does (lldb mean ?). How should I proceed ?

I hope this question is not irrelevant in StackOverflow's context. Should I precise that this is not homework (I am retired…) ?

2) I used to have on my (Jurassic) Palm Pilot a remarkable solver program called MathPad (by Rick Huebner, see http://www.palmspot.com/software/detail/ps52a_98295.html), maybe written in C.

Do you know if anything equivalent has been developed and maintained ?

Thanks in advance

Was it helpful?

Solution

Your program uses this calculation:

Revenue = Cost * (1 + GrossMargin);

If Revenue was entered as zero, it would be correct. But guess what happens if you intended to calculate Cost and entered it as zero while you entered valid values for Revenue and Cost. As you said, it will fail miserably.

The simplest solution is to check which of the entered quantities is zero and calculate it accordingly. Something like this:

if(Revenue == 0)
    Revenue = Cost * (1 + GrossMargin);
else if(Cost == 0)
    Cost = Revenue / (1+GrossMargin);
else
    GrossMargin = (Revenue-Cost)/Cost;

Then display your values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top