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