True/False function if a sum exists - c++

I want to have a program that returns true when there exist such positive integer numbers a and b such that a*a+b*b=n*n.
My code is:
bool c(int n){
int b=1;
int a=1;
for (a=1; a<=n; a++) {
for (b=a; b<=n; b++) {
if (a*a + b*b == n*n) {
return true;
else
return false;
}
}
}
However this code does not return what i want it to. What can i do to fix that?
Please everybody before you down vote please explain what could be improved in this question. I have noticed that sometimes people have a tendency to down vote questions without any explanation.

Your program doesn't compile. You probably intended to write this:
bool c(int n) {
int b = 1;
int a = 1;
for (a = 1; a <= n; a++) {
for (b = a; b <= n; b++) {
if (a*a + b*b == n*n)
return true;
else
return false;
}
}
}
which compiles, but which is wrong.
But you probably want this:
bool myfunction(int n) {
int b = 1;
int a = 1;
for (a = 1; a <= n; a++) {
for (b = a; b <= n; b++) {
if (a*a + b*b == n*n)
return true;
}
}
return false;
}
int main() {
if (myfunction(5)) {
printf("myfunction(5) returned true\n");
}
}

bool c(int n){
int b=1;
int a=1;
for (a=1; a<=n; a++) {
for (b=a; b<=n; b++) {
if (a*a + b*b == n*n) {
return true;
else
return false;
}
}
}
The code you have written will check for a=1 and b=1 and that evaluates to 1 + 1 equals to 2.
So,all that your code does is compare 2 to n.
The return false statement should be outside both the for loops.
This means that for all combination of a and b there exists no pair (a,b) such that aa + bb = n*n.

Related

Looking Bitwise solution for this

You’re given a read only array of n integers. Find out if any integer occurs more than n/3 times in the array in linear time and constant additional space.
If so, return the integer. If not, return -1.
If there are multiple solutions, return any one.
Example :
Input : [1 2 3 1 1]
Output : 1
1 occurs 3 times which is more than 5/3 times.
I've solved this problem and found some solutions for this on google. But I want a bitwise approach. If you could help me, I'd appreciate it.
Solution without bitwise:
int Solution::repeatedNumber(const vector<int> &A)
{
int len = A.size();
if (A.size() == 0)
{
return -1;
}
if (A.size() == 1)
{
return A[0];
}
int c1 = A[0];
int c2 = A[1];
int c1count = 0;
int c2count = 0;
for(int num: A)
{
if(c1 == num)
{
c1count++;
}
else if(c2 == num)
{
c2count++;
}
else if(c1count == 0)
{
c1 = num;
c1count = 1;
}
else if(c2count == 0)
{
c2 = num;
c2count = 1;
}
else
{
c1count--;
c2count--;
}
}
c1count = 0;
c2count = 0;
for(int num : A)
{
if(c1 == num)
{
c1count++;
}
else if(num == c2)
{
c2count++;
}
}
if(c1count > len/3)
{
return c1;
}
else if(c2count > len/3)
{
return c2;
}
else
{
return -1;
}
}

how to use functions that return a bool value?

I was writing a program to print 70 prime numbers (7 per row).
I was required to define and use two functions isprime() and printprime.
the program builds and runs but it doesn't print the prime numbers.
Can anyone tell me what's wrong with this code that why I am unable to print the prime numbers?
edit: corrected some mistakes in the for loops and isprime()
#include "pch.h"
#include <iostream>
using namespace std;
bool isprime(int num)
{
bool f = true;
int c,h = 0;
for (int j = 1; j <= num; j++)
{
c = num % j;
if (c == 0)
{
h++;
if (h > 2)
{
f = false;
break;
}
}
}
return f;
}
void printprime(int x, int y)
{
bool f = false;
int s = 0, h = 0, j = 0, num = 2;
num = 1; x = 70; y = 7;
for (int d = 0; d < x; d+=7)
{
s = 0;
cout << " " << endl;
for (;s < 7;s++)
{
f = isprime(num);
if (f == true)
{
cout <<" "<<num;
num++;
}
else if (f == false)
{
num++;
s--;
}
}
}
}
int main()
{
int x = 70, d = 0, h = 0, j = 0, y = 7, num = 1;
num = 1; y = 7;
printprime(x, y);
return 0;
}
There are couple of issues,
bool isprime(int num) is not returning the computed value f instead it returning true in all the case
if number of deviser are more than 2 you can break the loop, instead computing till the end.
printprime is not collecting return value from isprime
Code snippet as follows,
bool isprime(int num)
{
bool f = true;
int c,h = 0;
for (int j = 1; j <= num; j++)
{
c = num % j;
if (c == 0)
{
h++;
if (h > 2) //Check for more than 2 since every prime has min 2 devisers 1 and number iteslf
{
f = false;
break;
}
}
}
return f;//return the computed value
}
void printprime(int x, int y)
{
bool f = false;
int s = 0, num = 2;
//Note Num started from 2 as 1 is not a prime
for (int d = 0;d < x; d+=7)
{
s = 0;
while (s < 7)
{
f = isprime(num);
if (f == true)
{
cout << num <<" ";
s++;
}
num++;
}
cout << endl;
}
}
int main()
{
int x = 70, y = 7;
printprime(x, y);
return 0;
}
This line in printprime() is likely the cause of your issue:
for ((d < x); d += y;);
That trailing semi-colon stops the body of your for loop from executing.

C++ Getting a "Control may reach end of a non-void function on a Johnson-Trotter code

What can I do to silence this warning? Do I need to add another return statement somewhere or do I need to change something within the functions?
Also could someone help me add arrows into the Johnson-Trotter algorithm. It would be nice to have them to show the direction but I am very confused on how to do it; though this isn't the main concern right now I just want the program to run. Thank you in advance.
These are the two functions with the warning:
int searchArr(int k[], int n, int mobile)
{
for(int i = 0; i < n; i++)
{
if (k[i] == mobile)
{
return i + 1;
}
}
}
int printOnePerm(int k[], bool dir[], int n)
{
int mobile = getMobile(k, dir, n);
int pos = searchArr(k, n, mobile);
if (dir[k[pos - 1] - 1] == RIGHT_TO_LEFT)
{
swap(k[pos - 1], k[pos -2]);
}
else if (dir[k[pos - 1] - 1] == LEFT_TO_RIGHT)
{
swap(k[pos], k[pos -1]);
}
for(int i = 0; i < n; i++)
{
if (k[i] > mobile)
{
if (dir[k[i] - 1] == LEFT_TO_RIGHT)
{
dir[k[i] - 1] = RIGHT_TO_LEFT;
}
else if(dir[k[i] - 1] == RIGHT_TO_LEFT)
{
dir[k[i] - 1] = LEFT_TO_RIGHT;
}
}
}
for(int i = 0; i < n; i++)
{
cout << k[i];
}
cout << endl;
}
For the first function, searchArr(), one question is what do you expect it to return if the value is not found. Since the return values are in the range [1,n], I'm guessing that zero means not found.
I prefer to design functions which have a single return at the end, whenever possible. A default fail value can be set at the start of the function. I would exit the loop when the value is found, or fall through with the default value set.
Here is what I would write:
int searchArr(int k[], int n, int mobile)
{
int ret = 0; /* not found value */
for(int i = 0; i < n; i++)
{
if (k[i] == mobile)
{
ret = i + 1;
break;
}
}
return ret;
}
Alternately, and perhaps a bit more obscurely, if the value is not found in the array, then i will equal n when the for loop completes. This would be a possible function:
int searchArr(int k[], int n, int mobile)
{
for(int i = 0; i < n; i++)
{
if (k[i] == mobile)
{
break;
}
}
if (i < n)
return i + 1;
else
return 0;
}
The for loop can be shrunk to
for(int i = 0; i < n && k[i] != mobile; i++) ;
And the return can be shrunk to
return (i < n) ? i + 1 : 0;
Although I generally discourage using the ?: operator.
As mentioned above, the second function doesn't return any value and should be declared "void".
The first one:
int searchArr(int k[], int n, int mobile)
{
for(int i = 0; i < n; i++)
{
if (k[i] == mobile)
{
return i + 1;
}
}
}
will not return anything if for some reason nothing in your array matches. In that case, you need to return a default or error value:
int searchArr(int k[], int n, int mobile)
{
for(int i = 0; i < n; i++)
{
if (k[i] == mobile)
{
return i + 1;
}
}
return -1; // not found
}
The second one doesn't seem to want to return anything. In C++, the way to do this is with a void, not an int (That was okay in C. C++ not so much):
// assuming we don't want to return anything
void printOnePerm(int k[], bool dir[], int n)

for loop to while conversion

Here I have a C++ code with for loops.
for(int A=1;A<=3;A++)
{
cout<<A*2;
for(int B=1;B<=A;B++)
cout<<"*";
cout<<endl;
}
and it gives me this output.
2*
4**
6***
I need to do the same thing using while loops. so I convert the above code to this one.
while(A<=3)
{
cout<<A*2;
while(A>=B)
{cout<<"*";
B++;}
cout<<endl;
A++;
}
but this code give me the output
2*
4*
6*
Can someone tell me what I'm doing wrong there in my while loop.
int A = 1;
int B;
while (A <= 3)
{
cout << A * 2;
B = 1;
while (B <= A)
{
cout.put('*');
++B;
}
cout.put('\n');
++A;
}
ioccc-style:
#include <iostream>
int main()
{
int A{1};while(!(A>>2)&&std::cout.put((A<<1)|0x30)){
int B{A++};while(std::cout.put((!B)["*\n"]),B--);}
}
You are not giving A or B initial values.
void func()
{
int A = 1;
while (A<=3)
{
std::cout << A*2;
int B = 1;
while(A>=B)
{
std::cout << "*";
B++;
}
std::cout<<endl;
A++;
}
}
Your second snippet doesn't show where you declared A or B. I'm assuming it's outside of the outer while loop which won't work. You have to declare B inside the first while or reinitialize it every loop.
int A = 1;
while(A <= 3)
{
cout << A * 2;
int B = 1;
while(A >= B)
{
cout << "*";
++B;
}
++A;
cout << endl;
}
Result.
You should use B as local variable, alson change the while comparation to less and equal.
int A = 1;
while(A<=3) {
cout<<A*2;
int B = 1; // B as local variable
while(B<=A) {
cout<<"*";
B++;
}
cout<<endl;
A++;
}
Output:
2*
4**
6***
int A = 1;
while(A <= 3)
{
cout<<A*2;
int B = 1;
while(A >= B){
cout<<"*";
B++;
}
cout<<"/n";
A++;
}
output :
2*
4**
6***

the biggest common divisor of 2 numbers using arrays

How could I find the biggest common divisor of 2 numbers using array? I tried to solve it using 2 arrays and I couldn't finish it. How could I improve this program?
#include <iostream>
using namespace std;
int main()
{
unsigned int A[2][10], B[2][10], a, b, c_exp, d, i1, P, x;
bool apartine = false;
cout << "a="; cin >> a;
cout << "b="; cin >> b;
P = 1;
c_exp = 0;
i1 = 0;
while (a % 2 == 0)
{
c_exp++;
a = a/2;
}
if (c_exp != 0)
{
A[i1][0] = 2;
A[i1][1] = c_exp;
i1++;
}
d = 3;
while (a != 1 && d <= a)
{
c_exp=0;
while (a % d == 0)
{
c_exp++;
a = a/d;
}
if (c_exp!=0)
{
A[i1][0] = d;
A[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea A contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << A[i][j] << ",";
}
c_exp = 0;
i1 = 0;
while (b % 2 == 0)
{
c_exp++;
b = b/2;
}
if (c_exp != 0)
{
B[i1][0] = 2;
B[i1][1] = c_exp;
i1++;
}
d = 3;
while (b != 1 && d <= b)
{
c_exp = 0;
while (b % d == 0)
{
c_exp++;
b = b/d;
}
if (c_exp != 0)
{
B[i1][0] = d;
B[i1][1] = c_exp;
i1++;
}
d = d+2;
}
cout << "\nMatricea B contine:";
for (int i = 0; i < i1; i++)
{
cout << "\n";
for (int j = 0; j < 2; j++)
cout << B[i][j] << ",";
}
return 0;
}
From now on I have to find if the first number of first array exist in the second array and after this I have to compare the exponents of the same number of both array and the lowest one I have to add it to product. After this I have to repeat the same proccess with the second number to the last one of the first array. The problem is that I don't know how to write this.I have to mention that this program isn't complete.
Any ideas?
If you need better solution then you can avoid array and use the below logic.
int main()
{
int a =12 ,b = 20;
int min = a>b ? a:b; // finding minimum
if(min > 1)
{
for (int i=min/2; i>1; i--)//Reverse loop from min/2 to 1
{
if(a%i==0 && b%i==0)
{
cout<<i;
break;
}
}
}
else if(min == 1)
{
cout<<"GCD is 1";
}
else
cout<<"NO GCD";
return 0;
}
You can also check the working example Greatest Common Divisor
I am not quite sure what you are trying to achieve with your code. It looks over complicated. If I were to find the biggest common divisor of two numbers I would do something like the following:
## This is not a correct implementation in C++ (but close to it) ##
Read the two integers **a** and **b**
int max_div(int a, int b){
int div = a > b ? a : b;
while (div != 1 && (a%div != 0 && b%div != 0)){
div--;
}
return div;
}
This function starts with the minimum of a and b as the highest possible common divisor and then works its way backwards until one of two possible outcomes:
It finds a common divisor (a%div == 0 and b%div == 0)
It reaches one (always a common divisor)
EDIT : Now returns one if no higher divisor is found. (Was returning zero which made no sense)