why is my code not printing the largest number? - c++

#include <iostream>
void isMax(int a, int b, int c, int d) {
int ans1, ans0, x;
a = !b, a = !c, a = !d,
b = !c, b = !d,
c = !d, c = !a;
if (a<b && b>c) { // ans0 starts
if (b > d) {
std::cout << b << std::endl;
}
else {
std::cout << d << std::endl;
}
}
else if (a < b && b < c) {
if (c > d) {
std::cout << c << std::endl;
}
else {
std::cout << d << std::endl;
}
}
//ans1
else if (a > b&& a > c) {
if (a > c&& a > d) {
std::cout << a << std::endl;
}
else if (a > c&& a < d) {
std::cout << d << std::endl;
}
else if (a<c && c>d) {
std::cout << c << std::endl;
}
else if (a < c && c < d) {
std::cout << d << std::endl;
}
else {
std::cout << "invalid numbers ahead." << std::endl;
}
}
std::cout << a << b << c << d << std::endl;
};
int main() {
isMax(3, 4, 6, 5);
return 0;
}
it kind of worked before i added
a = !b, a = !c, a = !d,
b = !c, b = !d,
c = !d, c = !a;
this part but did not work when i give the same values for a,b,c,d that`s why i added that part and now it does not work at all.
What is my mistake here ?
This image shows what i am trying to turn into code. Basically i`m trying to write a function that prints out the largest of four variables.

a != b; is true when a and b are different. What you wrote is a = !b; which is "assign to a the logically negated value of b".
I do not see how such assignment could make any sense here. That's why you get correct results without it and non-sense results when you add that assignments.
PS: Even if you "fix" it by writing a != b; the statement alone will have no effect. I suppose you want to return early or skip some of the later conditions when some input is equal.

From your schema, it should be:
void DisplayMax(int a, int b, int c, int d) {
if (b < a) {
if (c < a) {
if (d < a) {
std::cout << a;
} else { // a <= d
std::cout << d;
}
} else { // b < a <= c
if (c < d) {
std::cout << d;
} else {
std::cout << c;
}
}
} else { // a <= b
if (c < b) {
if (d < b) {
std::cout << b;
} else { // b <= d
std::cout << d;
}
} else { // a <= b <= c
if (c < d) {
std::cout << d;
} else {
std::cout << c;
}
}
}
}
But, as shown in other answer, there is simpler ways.
or with std:
void DisplayMax(int a, int b, int c, int d) {
std::cout << std::max({a, b, c, d});
}

I see a few issues:
Your logic seem over complicated for printing the largest of 4 variables.
The name of the isMax function doesn't reflect what it does.
I don't understand the need for a "invalid numbers ahead" if indeed printing the largest of four integers is intended.
I propose a simpler logic with fewer conditions but another variable on the heap.
#include <iostream>
int largestOf(int a, int b, int c, int d) {
int max = a;
if (b > max)
max = b;
if (c > max)
max = c;
if (d > max)
max = d;
return max;
}
void isMax(int a, int b, int c, int d) {
std::cout << largestOf(a, b, c, d) << std::endl;
}

It's not clear at all what you expect the a = !b, ... to accomplish, so I'm going to ignore that.
I am also going to ignore the existence of std::max since this is clearly an exercise.
Your logic tree makes sense, but you can find a more convenient solution if you bark up a different but similar tree.
Consider the case when a > b, i.e. you know for certain that b is not the maximum.
Instead of continuing with a, now look at only c and d.
If c > d, you know that the maximum is not d, so it must be either a or c.
Likewise, if d > c, the maximum is either a or d.
The same line of reasoning for the case when b > a leads to the following algorithm:
Determine max(a,b).
Determine max(c,d).
The maximum of all four numbers is the maximum of the previous two results.
In code:
void isMax(int a, int b, int c, int d) {
int ans0 = a > b ? a : b;
int ans1 = c > d ? c : d;
std::cout << (ans0 > ans1 ? ans0 : ans1) << std::endl;
}
On a side note, a function whose name begins with "is" usually returns a boolean, and you will find more use for a function that returns the maximum than you will of one that prints it.
You can always print that value later if you want to look at it.
Like this:
int max(int a, int b, int c, int d) {
int ans0 = a > b ? a : b;
int ans1 = c > d ? c : d;
return ans0 > ans1 ? ans0 : ans1;
}

This code is over complicated for something fairly simple. If you're new to programming, I suggest you try using arrays, in that way you can easily decide which number is max.
For example:
void isMax(int a, int b, int c, int d) {
int j[4] = { a,b,c,d };
int max = j[0];
for (int i = 1; i < 4; i++) {
if (j[i] > max){ max = j[i]; }
}
cout << max;
}
This is a better way to do it.

Related

How do you interpret this code correctly step by step? (New to programming)

My problem is that I dont know how the compiler runs through the different if statements correctly and why he skips some in this case.
I tried checking if the conditions from start to bottom are true or false and thus find the correct output of the program. But why doesnt the program output 84 here:
if (a > c) cout << 84;
else cout << 48
Full Program:
int main()
{
constexpr int a{8};
constexpr int b{4};
constexpr int c{1};
if (a < b < c)
if (c > b > a)
if (a > c) cout << 84;
else cout << 48;
else
if (b < c) cout << 14;
else cout << 41;
else
if (b < a < c)
if (a < c) cout << 81;
else cout << 18;
else
if (b < c) cout << 17;
else cout << 71;
return 0;
}
The program outputs only 41. Why?
This statement is a nonsense:
if (a < b < c)
It will be evaluated as:
if (a < bool(b < c))
The lt/gt/eq/ne/le/ge operators are binary - i.e. they require two arguments. You should be doing something like:
if (a < b && b < c)
Firstly if you are a newbie. Don't skip braces. Now Let's go through it step by step
In your first if-else. Here your a=8 , b= 4 , c=1. This is how your code is proceeding
if (a < b< c) // equivalent to if(0<c) due to left associativity// firstly a<b will be evaluated which 0 and hence statement is true as c is 1.
{
if (c > b > a) // equiavelnt to if(bool(c>b)>a) which is false as c>b is 0 hence it will reduce to if(0>c) .execution goes to else block.
{
if (a > c)
{
cout << 84;
}
else
{
cout << 48;
}
}
else
{
if (b < c) // it is false. execution goes to else
{
cout << 14;
}
else
{
cout << 41; // it is printed.
}
}
}

Decompose a number in its prime factors

I have this recursive function that decompose a number in its prime factors, and show the result standard output for example
descompon(2, 10);
Output
2 = 2
3 = 3
4 = 2^2
5 = 5
6 = 2 * 3
7 = 7
8 = 2^3
9 = 3^2
10 = 2 * 5
The code
#include <iostream>
#include <sstream>
int comprobar_primo( int* num, int e )
{
if (*num%e == 0)
{
*num /= e;
return 1 + comprobar_primo(num, e);
}
return 0;
}
std::string factor_primo(int a, int b, std::stringstream& fact)
{
unsigned exp = comprobar_primo(&a, b);
if (exp >= 1)
{
fact << b;
if (exp > 1) fact << '^' << exp;
if (a != 1) fact << " * ";
}
if (a > 1) factor_primo(a, b + 1, fact);
return fact.str();
}
void descompon(int a, int b, int ver)
{
std::stringstream fact;
//std::string result = factor_primo(a, 2, fact);
if(ver)
std::cout << a << " = " << factor_primo(a, 2, fact) << std::endl;
if(a < b)
descompon( a + 1, b, ver);
}
int main(void)
{
descompon(2, 10000, 1);
return 0;
}
The problem is that when reaches the 5922 the program remains frozen, showing:
Process returned -1073741819 <0xC0000005>
why this happens and how I can avoid?
Both your factor_primo and descompon functions can potentially cause stack overflow. Its better to convert them into iterative version. Modified code is given below:
// no need to pass b as argument since we start from b=2 and increment b by 1
std::string factor_primo(int a, std::stringstream& fact)
{
for(int b=2; a>1; b++)
{
if(a%b==0)
{
unsigned exp=comprobar_primo(&a, b);
if(exp >= 1)
{
fact << b;
if(exp > 1) fact << '^' << exp;
if(a != 1) fact << " * ";
}
}
}
return fact.str();
}
void descompon(int a, int b, int ver)
{
if(ver)
{
for(int i=a; i<=b; i++) {
std::stringstream fact;
std::cout << i << " = " << factor_primo(i, fact) << std::endl;
}
}
}
int main(void)
{
descompon(2, 10000, 1);
getchar();
return 0;
}

C++ math answer different than in class

I wanted to practice math in c++ and I tried making a program that answered this question from math class
0 < r < 1, find the number of rational r values for which the
numerator and the denominator add to make 1000 where r is in simplest
form
After an hour or two debugging, I finally got something that makes it through all the numbers. In class, the answer was 200. I got 216. Run for yourself
#include <math.h>
#include <iostream>
bool rprime_test(int a, int b) {
int tmp = 2;
std::cout << a << "/" << b;
tmp1:
for (tmp; (tmp < a) && (a % tmp != 0); tmp++) {
}
if ((b % tmp == 0 && a % tmp == 0) || b % a == 0) {
std::cout << " == irreduced\n";
return false;
} else if (!tmp < a) {
std::cout << " == reduced\n";
return true;
} else {
//std::cout << tmp << ","<< a << std::endl;
goto tmp1;
}
}
int main() {
int r = 0, a = 1;
int b = 1000 - a;
while (a < b) {
if (rprime_test(a, b)) {
r++;
}
std::cout << "total = " << r << std::endl;
a++;
b = 1000 - a;
//std::cout << "assigned " << a << "/" << b << std::endl;
}
std::cout << "final result = " << r << std::endl;
return 0;
}
please I don't know what I did wrong for this. Also, is there any better way to optimize this?
Your main issue is with your rprime_test function. Without digging too much into your existing function, try using the gcd. Two numbers a and b are an irreducible fraction when they are "coprime," which is when their "greatest common denominator" (gcd) is 1. The way you compute the gcd of two values is with the Euclidean Algorithm:
int gcd (int a, int b) {
return b % a == 0 ? a : gcd (b % a, a);
}
And your check becomes
if (gcd (a, b) == 1) {
a++;
/* etc */
}
Following works:
#include <iostream>
int gcd(unsigned int a, unsigned int b)
{
if (b < a) {
return gcd(b, a);
}
int r = a % b;
while (r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
int main()
{
int count = 0;
for (int i = 1; i != 500; ++i) {
if (gcd(1000 - i, i) == 1) {
++count;
}
}
std::cout << count << std::endl;
}
Live example

New project doesn't cout any values?

#include <iostream>
using namespace std;
int main()
{
int a = 6160;
int b = 6160;
int c = a + b;
int d = 0;
int f = 100;
int i;
for (i = 0; i <= c; i++)
{
while (c >= f)
{
d += 1;
c = c - f;
break;
}
}
cout << d << "-" << c;
system("pause");
return 0;
}
This program is supposed to increase d by 1 each time c reaches 100 and then start counting again till it's 100 and then increase d by 1 more and if there is any extra numbers that didn't reach 100 after the whole calculation is done the this will be c .. the matter is when I put these values in the program above in a and b I get this answer:
122-120
while it should be:
123-20
You should remove your for loop and the break in the while loop: http://ideone.com/WlL2JN
int main()
{
const int a = 6160;
const int b = 6160;
int c = a + b;
int d = 0;
const int f = 100;
while (c >= f) {
d += 1;
c = c - f;
}
std::cout << d << "-" << c << std::endl;
return 0;
}
BTW, you may do directly:
d = c / f;
c = c % f;

Euclid's Algorithm Function parameters

I've written a program for class where I need to recursively evaluate the extended euclid's algorithm for a and b, returning G, the greatest common divisor, as well as s and t from as+bt=gcd(a,b). I'm fairly certain I have the function written correctly but I am having issues with values being passed to and from the function. I haven't coded in a while and have only written pseudocode recently so I'm a little rusty.
For example, I have written when b=0, return (a, 1, 0), but when I input b as 0 I get returned (0, 0, 0) and cannot figure out why this is happening. Any help or guidance would be greatly appreciated.
#include <iostream>
using namespace std;
int ExtGCD (int a, int b)
{
int g, s, t, g1, s1, t1;
if (b == 0) {
return (a, 1, 0);
}
(g1, s1, t1) = ExtGCD(b, a%b);
g = g1;
s = s1;
t = s1 - ((a/b)*t1);
return (g, s, t);
}
int main(int argc, char* argv[])
{
int a,b, g2, s2, t2, temp;
cout << "Please input a: ";
cin >> a;
cout << "Please input b: ";
cin >> b;
if (b > a) {
temp = a; a = b; b = temp;
}
(g2, s2, t2) = ExtGCD (a, b);
cout << "G = "<< g2 << ", S = " << s2 << ", T = " << t2;
return 0;
}
C++11 introduces tuples, which allow you to write your code like this, with minimal modifications:
#include <iostream>
#include <tuple>
using namespace std;
std::tuple<int, int, int> ExtGCD (int a, int b)
{
int g, s, t, g1, s1, t1;
if (b == 0) {
return std::make_tuple(a, 1, 0);
}
std::tie(g1, s1, t1) = ExtGCD(b, a%b);
g = g1;
s = s1;
t = s1 - ((a/b)*t1);
return std::make_tuple(g, s, t);
}
int main(int argc, char* argv[])
{
int a,b, g2, s2, t2, temp;
cout << "Please input a: ";
cin >> a;
cout << "Please input b: ";
cin >> b;
if (b > a) {
temp = a; a = b; b = temp;
}
std::tie(g2, s2, t2) = ExtGCD (a, b);
cout << "G = "<< g2 << ", S = " << s2 << ", T = " << t2;
return 0;
}
See http://en.cppreference.com/w/cpp/utility/tuple/tie and http://en.cppreference.com/w/cpp/utility/tuple.
On a related note, you can also replace
if (b > a) {
temp = a; a = b; b = temp;
}
by
if (b > a)
std::swap(a, b);
or even by
std::tie(b, a) = std::minmax({a, b});
The C++ standard library provides many algorithmic facilities that should be learned to enjoy C++ to its full potential.
return (g, s, t);
doesn't do what you think it does. It's not possible to return multiple values from a function like that. Look up the comma operator if you want an explanation of what that code does.
There's a few different ways you could handle this. Perhaps the simplest is to return your values via references passed to the function. Like this
#include <iostream>
using namespace std;
void ExtGCD (int a, int b, int& g, int& s, int& t)
{
int g1, s1, t1;
if (b == 0) {
g = a;
s = 1;
t = 0;
return;
}
ExtGCD(b, a%b, g1, s1, t1);
g = g1;
s = s1;
t = s1 - ((a/b)*t1);
}
int main(int argc, char* argv[])
{
int a,b, g2, s2, t2, temp;
cout << "Please input a: ";
cin >> a;
cout << "Please input b: ";
cin >> b;
if (b > a) {
temp = a; a = b; b = temp;
}
ExtGCD (a, b, g2, s2, t2);
cout << "G = "<< g2 << ", S = " << s2 << ", T = " << t2;
return 0;
}
In this code g, s and t are references, which means assignments to them change the values of the variables bound to the references when the function is called.
You cannot return tuples like that.
In C++, the comma operator will throw out the stuff on the left. In your particular case, the "tuple" (a,b,c) is actually equal to just c. A more concrete example:
if (b == 0) {
return (a, 1, 0);
}
is actually the same as
if (b == 0) {
return 0;
}
To return multiple things at once, you'll have to use structs or classes.