How to order ascending the variables a , b , c? - c++

How to order ascending the variables a , b , c? (in a simpler way)
void Crescator(int a, int b, int c)
{
int z = (a > b) ? a : b;
if(z < c)
{
cout << a << c << b;
}
}

My favourite way; hopefully self-explanatory
if (a > c) std::swap(a, c);
if (a > b) std::swap(a, b);
if (b > c) std::swap(b, c);
Don't forget to pass the parameters to Crescator by reference if you want to make them sorted in the caller.

Related

warning C4715: not all control paths return a value c++ - cant pass a test

I'm having an issue with this code:
The problem is I'm constantly getting warning C4715 despite the fact .exe is running correctly and it gives correct answer to a problem I'm trying to resolve. The warning makes it impossible to pass the task inside the app. Please give me a clue why 'return' used by me in the if sentences doesn't work.
#include <utility>
#include <iostream>
std::pair<int, int> solve(int a, int b) {
if (a == 0 || b == 0) {
std::pair <int, int> kek(a, b);
return kek;
}
else if (a >= 2 * b) {
a = (a - (2 * b));
solve(a, b);
}
else if (b >= 2 * a) {
b = (b - (2 * a));
solve(a, b);
}
else {
std::pair <int, int> kek(a, b);
return kek;
}
}
int main() {
bool result{ solve(22, 5) == std::make_pair(0,1) };
std::cout << result;
return 0;
}
Your solve function won't execute return statement if a == 0 || b == 0 is not true and either one of a >= 2 * b or b >= 2 * a is true.
It seems that the two solve(a, b); in the solve function should be return solve(a, b);.

Can I make enumerations of variables in if statemants?

I am trying to learn C++ (Beginner). But I wonder how I can make an enumeration of variables in statements like if.
Do I just put a comma between variables?
What is the right syntax for this.. or is this all good?
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d, e;
cin >> a >> b >> c >> d >> e;
if (a, b, c > e && a, b, c > d)
{
cout << a + b + c;
}
}
No, there is nothing in C++ like that. You'll need to split each of those into their own statement, like:
if (a > e && b > e && c > e && a > d && b > d && c > d){
However, the logic here can be simplified.
If you want a > e AND a > d, then you only need to show that a is greater than the larger of e and d. The reverse is true for a, b and c. In other words, you only need to check that the smallest of a/b/c is greater than the largest of e/d.
So this can become:
if (min({a, b, c}) > max(e, d)){
You can not do what you have tried there in the code.
The obvious way (beginner)way has been shown in the #scohe001's answer. However, when you learn at some point the templates and fold expressions, the following solution would be much compact and similar to what you have tried to do.
#include <iostream>
#include <utility>
template<typename... Args>
constexpr bool all_of_greater(const int lhs, Args&&... rhsArgs)
{
return ((lhs < std::forward<Args>(rhsArgs)) && ...);
}
Now you could do similar to what you have done in the code:
if (all_of_greater(e, a, b, c) && all_of_greater(d, a, b, c))
{
std::cout << a + b + c;
}
(See Online Demo)
Here's a reworked approach where the "values" and the "targets" are split into two separate vector structures to ease comparison:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
// Helper function to read an arbitrary number of entries into a vector
void read_n(std::vector<int>& list, const size_t n) {
for (size_t i = 0; i < n; ++i) {
int v;
std::cin >> v;
list.push_back(v);
}
}
int main() {
// Container to hold the values
std::vector<int> values;
read_n(values, 3);
// Container to hold the targets
std::vector<int> targets;
read_n(targets, 2);
// Ensure that for each target...
for (auto target : targets) {
// ...all of the values exceed that target...
if (!std::all_of(values.cbegin(), values.cend(), [target](int i) { return i > target; })) {
// ...or else it's a fail.
return -1;
}
}
// Use accumulate to compute the sum and display it.
std::cout << std::accumulate(values.cbegin(), values.cend(), 0) << std::endl;
return 0;
}
When writing code try and think in terms of structure and loops rather than just copy-pasting code to add more variables.

How do I solve this middle number problem?

Write a program where the user inputs 3 float numbers and the program checks which is medium size number. Example : a = 1.5, b = 7.8, and c = 3.0 and output should be c.
This is what I've tried and it worked for one case, but I'm still doing too much spaghetti code and I'm still learning how to write code efficiently.
My code:
#include <stdio.h>
int main(){
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
if(a < b && c < a)
printf("%.1f", a);
else if(b < a && b > c)
printf("%.1f", b);
else if(c > a && c < b)
printf("%.1f", c);
else
{
printf("not good"); //I wrote this part to check if the code is good
}
return 0;
}
I'm still trying to get the hang of the if loops and I was just confused with this problem. Do you have any suggestions?
think of like, if a is medium , then b is medium, then c is medium. Check if that hepls!
#include <stdio.h>
int main(){
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
if((a > b && a < c) || (a > c && a < b) )
printf("%.1f", a);
else if((b > a && b < c) || (b > c && b < a))
printf("%.1f", b);
else if((c > a && c < b) || (c > b && c < a))
printf("%.1f", c);
else
{
printf("not good"); //I wrote this part to check if the code is good
}
return 0;
}
If you can use C++:
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
float a, b, c;
scanf("%f %f %f", &a, &b, &c);
// Put them in a vector. Could use array, but vector more flexible
vector<float> vals = {a,b,c};
// Sort in numerical order
sort (vals.begin(),vals.end());
// Display the middle one
printf("%.1f", vals[1]);
return 0;
}

How to calculate HCF a very large number and a small number without causing stack overflow. I am using euclid algorithm

I am using Euclid algorithm but it is causing run time error due to stack overflow.
I am unable to calculate HCF of a very large number and a small number
I believe you're writing a function like this:
int hcf(int a, int b){
if (a == 0){
return b;
}
else if (b == 0){
return a;
}
else if (a > b){
return hcf(b, a - b); // this is subtraction
}
else if (a < b){
return hcf(a, a - b); // this is subtraction
}
}
...and you're calling it with something like
int q = hcf(100000000, 1);
Well... Without optimisation that will create 1 billion recursion calls. It's definite that your program will run out of stack capacity.
My personally preferred solution is give up recursive methods and use an iterative one. The code can then be simplified to a single loop:
int hcf(int a, int b){
while(a != 0 && b != 0){
if (a > b){
a = a - b;
}
else{
b = b - a;
}
}
if (a == 0){
return b;
}
else{
return a;
}
}
If you insist on using recursive methods, replace subtraction with modulus.
else if (a > b){
-> return hcf(b, a % b); // this is modulus
}
else if (a < b){
-> return hcf(a, a % b); // this is modulus
}
Correctly implemented algorithm shall use at most log(number) steps, and thus not cause stack overflow. I suppose you use the following algorithm:
gcd(a, 0) = a
gcd(a, b) = gcd(a-b, b)
which looks like this in C++:
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(std::max(a, b) - std::min(a, b), std::min(a, b));
}
}
This is not optimal. Instead you shall use the following relation
gcd(a, 0) = a
gcd(a, b) = gcd(b, a mod b)
which looks like this in C++:
int gcd(int a, int b) {
if (b == 0) {
return a;
} else {
return gcd(b, a % b);
}
}
This code will actually take only log(ab) steps, and thus not cause stack overflow
Also you may try to enable optimisation: it should allow to collapse both of the functions call into non-recursive versions (as this is a tail recursion). Note that it is not certain if it will increase speed.
As a matter of caution: be careful with the negative numbers, the % operator works incorrectly for them

Relatively Prime Numbers

How to make a function in c++ to determine if two entered numbers are relatively prime (no common factors)?
For example "1, 3" would be valid, but "2, 4" wouldn't.
Galvanised into action by Jim Clay's incautious comment, here is Euclid's algorithm in six lines of code:
bool RelativelyPrime (int a, int b) { // Assumes a, b > 0
for ( ; ; ) {
if (!(a %= b)) return b == 1 ;
if (!(b %= a)) return a == 1 ;
}
}
Updated to add: I have been out-obfuscated by this answer from Omnifarious, who programs the gcd function thus:
constexpr unsigned int gcd(unsigned int const a, unsigned int const b)
{
return (a < b) ? gcd(b, a) : ((a % b == 0) ? b : gcd(b, a % b));
}
So now we have a three-line version of RelativelyPrime:
bool RelativelyPrime (int a, int b) { // Assumes a, b > 0
return (a<b) ? RelativelyPrime(b,a) : !(a%b) ? (b==1) : RelativelyPrime (b, a%b);
}
One of the many algorithms for computing the Greatest Common Denominator.