How do I solve this middle number problem? - c++

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;
}

Related

Why does the output always show 0 as LCM?

https://github.com/mehedihasrifat
Please correct my mistake**
How can I solve this issue?
Did I do something wrong here?**
I have been trying to debug this code but ultimately I can't. Please help me, I'm totally new to this platform.
This picture shows the following code
/*
Written by Mehedi Hasan Rifat
Written on October 2, 2022
*/
#include <stdio.h>
int main()
{
int a, b, t, gcd, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a == 0)
gcd = b;
else if (b == 0)
gcd = a;
else
{
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
}
lcm = (a * b) / gcd;
printf("LCM: %d\n", lcm);
return 0;
}
As jasonharper says in the comment, when you finished the gcd calculation, b will be always zero.
One quick fix is to add
int original_a = a;
int original_b = b;
and calculate lcm using:
lcm = (original_a * original_b) / gcd;
Or just use the __gcd() function in the <algorithm> library.
#include <algorithm>
int main()
{
...
lcm = (a * b) / std::__gcd(a, b);
}

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);.

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

How to order ascending the variables a , b , 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.

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.