|
-
March 14th, 2018, 08:18 PM
#1
[RESOLVED] Round Up/Down Function? I'm looking to build a program that solves basic equations?
I know a little bit of Objective-C and I'm not too familiar with the differences between C++ and Obj. C I'm looking for a solution to a business need that I have. I'm looking to create a C++ program on my Windows computer to solve a few simple equations, one after the other. I don't know where to start or if using Python would be better suited for this problem.
Problem: I want to be able to open up the application on my computer and then be prompted to enter a value for 2 different variables (these values will be called x and p). I then need the program to run through a set of equations (6 in total). I then need the program to return the value of 3 different variables (we'll call them: c, g and h). A big issue I'm finding, is that I need to round up/down a couple of variables for the other equations to work, yet I don't know what function I need to use to make this happen. Here is a breakdown of how it should go:
Prompt-
Please enter the Buy Price:
1. 1000/x = t
2. t (rounded to the nearest one) = c (this value to display as the result)
3. c(x) = f
4. (p + f) / c = k
5. k (rounded to the nearest hundredth) = g (this value to display as the result)
6. g(c) - f = h (this value to display as the result)
--Translated----
Prompt-
Please enter the Buy Price: 30
1. 1,000/30 = 33.33333333333333
2. 33.33333333333333 = 33
3. 33(30) = 990
4. (140 + 990) / 33 = k
(1,130) / 33 = 34.24242424242424
5. 34.24242424242424 = 34.24
6. 34.24(33) - 990 = 139.92
Result:
Contracts: 33
Sell Price: $34.24
Gross Profit: $139.92
--------------------------------------------------
Has anyone ever worked with a website or program builder that can easily go through these set of equations? Or can anyone tell me how to use the round up/down function in C++? Also how would I go about carrying the value of a variable over to the next equation in order to solve it?
Much appreciated!
-
March 15th, 2018, 04:19 AM
#2
Re: Round Up/Down Function? I'm looking to build a program that solves basic equation
Objective-C and C++ have a common ground in C so if you keep your program basic and simple an Objective-C version of it would probably work in C++ with only minor changes.
Last edited by wolle; March 15th, 2018 at 04:28 AM.
-
March 15th, 2018, 05:19 AM
#3
Re: Round Up/Down Function? I'm looking to build a program that solves basic equation
Consider for c code
Code:
const int x = 30;
const int p = 140;
const int c = 1000 / x;
const int f = c * x;
const double k = (p + f) / (double)c;
const double g = ((int)(k * 100.0)) / 100.0;
const double h = g * c - f;
printf("Contracts: %d\nSell price: $%.2f\nGross Profit: $%.2f\n", c, g, h);
which displays
Code:
Contracts: 33
Sell price: $34.24
Gross Profit: $139.92
PS Note that this code truncates, not rounds as I misread the question.
Last edited by 2kaud; March 16th, 2018 at 05:54 AM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 15th, 2018, 04:15 PM
#4
Re: Round Up/Down Function? I'm looking to build a program that solves basic equation
Add .5 and truncate. For example
Code:
double d = 33.333;
d += .5; // d now = 33.833
int n = (int)floor(d); // n = 33
double d = 33.73.
d += .5; // d now == 34.23
int n = (int)floor(d); // n == 34
If you want to round to two decimal places, add .005.
-
March 16th, 2018, 07:53 AM
#5
Re: Round Up/Down Function? I'm looking to build a program that solves basic equation
For rounding without using maths library functions (and assuming all positive values), consider
Code:
double x;
const double p = 140.0;
printf("Enter the buy price :");
scanf("%lf", &x);
const int c = (int)(1000.0 / x + 0.5);
const double f = c * x;
const double k = (p + f) / (double)c;
const double g = ((int)((k + 0.005) * 100.0)) / 100.0;
const double h = g * c - f;
printf("\nContracts: %d\nSell price: $%.2f\nGross Profit: $%.2f\n", c, g, h);
giving
Code:
Enter the buy price :30
Contracts: 33
Sell price: $34.24
Gross Profit: $139.92
Last edited by 2kaud; March 16th, 2018 at 10:05 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 18th, 2018, 04:10 AM
#6
Re: Round Up/Down Function? I'm looking to build a program that solves basic equation
 Originally Posted by Combz
A big issue I'm finding, is that I need to round up/down a couple of variables for the other equations to work, yet I don't know what function I need to use to make this happen.
The C++ language has all basic rounding functions in its standard library. You can use them to round up, down and fairly (up or down so that no part is at disadvantage over a large number rounding interchanges). Here's a very basic C++ program using these functions. I've standardized on double to avoid expressions where int and double are mixed which may cause unexpected results. This means care must be taken to explicitly round all doubles that are supposed to be integral or money values.
Code:
#include <cmath>
#include <iostream>
#include <string>
double roundUp(double n) { // round n up
return std::ceil(n);
}
double roundDown(double n) { // round n down
return std::floor(n);
}
double roundFair(double n) { // round n fairly
return std::round(n);
}
double roundFair(double n, unsigned d) { // round n fairly at the d'th decimal position
const double s = std::pow(10, d);
return roundFair(n * s) / s; // shift n to the left d decimal positions, round fairly, shift back
}
double roundMoney(double n) { // round n at the second decimal position like money is usually rounded
return roundFair(n, 2);
}
std::string moneyString(double n) { // turn n into a money string
std::ostringstream stream;
stream << std::fixed << std::setprecision(2) << "$" << roundMoney(n);
return stream.str();
}
void test() {
double x = 33.0;
const double p = 140.0;
const double c = roundDown(1000.0 / x);
const double f = c * x;
const double k = (p + f) / c;
const double g = roundMoney(k);
const double h = roundMoney(g * c - f);
std::cout << "Contracts: " << std::to_string((int)c) << std::endl;
std::cout << "Sell price: " << moneyString(g) << std::endl;
std::cout << "Gross Profit: " << moneyString(h) << std::endl;
}
Last edited by wolle; March 25th, 2018 at 01:01 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|