How to make negative numbers not count when doing addition (C++) - c++

i tried making a program like this for schoolwork but it dosent work can somebody help me please, i dont really know where to go
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
int c;
int d;
cout<<"enter 3 numbers"<<endl;
cin>>a;
cin>>b;
cin>>c;
d = a + b + c
if ( a <= 1 - = 0 )
cout<<d<<endl;
if ( b < 1 - = 0 )
cout<<d<<endl;
else ( c < 1 - = 0 )
return 0;

You may want to try something like this:
int d = 0; // Initial value
if (a > 0)
{
d = d + a;
}
if (b > 0)
{
d = d + b;
}
if (c > 0)
{
d = d + c;
}
In the above code, terms are only added to the d variable if they are positive.

Related

C++: How to find a and b from k = 2^a * b?

Given following in C++:
$$k = 2^a \cdot b,$$
only knowing the value of k and that b is odd. How do you find the value for a and the value for b?
I did consider the following:
if k is odd, a has to be zero and b is k
if k is even, I would go through all possible b's with a for-loop and check if k % b == 0. If that is the case, I would take log2(k/b) and if that gives me back an integer, b = k/b and a = log2(b).
My problem: how do I check if log2(k/b) gives me back an integer?
C++ code:
Big k(9);
int r = 4;
int n = pow(2,r);
if (k % 2 == 1)
{
a = 0;
b = k;
}
else
{
for (int b = 1; b < n; b += 2)
{
if (k % b == 0 && LOGARITHM OF k / b IS POSSIBLE)
{
a = log2(b);
}
}
}
By the way: n is given as well. Everything but a and b is given.
You might do the following:
int a = 0;
while (k % 2 == 0) {
++a;
k /= 2;
}
int b = k;
// you have a, b
idest, divide by 2 the number 2**a*b until it is no longer even, so you found b.
Assuming k is unsigned int,
#include <bit>
...
int a = std::countr_zero(k);
unsigned int b = k >> a;

Min Unique Characters

REF: Ques 5 on this link: http://www.geeksforgeeks.org/directi-programming-questions
Given: two strings X and Y , with only one operation possible i.e swap the corresponding letters of X and Y ( i.e. X[i] and Y[i] ) which can be performed any number of times.
n(X) : number of unique characters in X
n(Y) : number of unique characters in Y
Problem : By Using the swap operations , Find the minimum possible values of max(n(X),n(Y))
INPUT:
ababa
babab
OUTPUT:
1
--------------------
INPUT:
abaaa
baaac
OUTPUT:
2
Please help me correct my solution or solve this problem with a better approach.
My Approach (Works For first and many other testcases but not the second one ) :
for(int i=0; i<x;i++)
{
if((count1[st1[i]]!=-1)||(count2[st2[i]]!=-1))
{
if(st1[i]!=st2[i])
{
if(count1[st1[i]]!=-1)
count1[st1[i]]++;
if(count2[st2[i]]!=-1)
count2[st2[i]]++;
}
else
{
ans++;
count1[st1[i]]=-1;
count2[st1[i]]=-1;
}
}
}
for(int i=97;i<123;i++)
{
if(count1[i]>0)
c1++;
if(count2[i]>0)
c2++;
if((count1[i]>0)&&(count2[i]>0))
com++;
}
un = max(c1,c2);
ans+= un-com/2;
printf("%lld\n",ans);
I'm not sure that I understood your algorithm, but here is brute-force version, which works for strings up-to 64 symbols length:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define max(a,b) (a >= b ? a : b)
#define min(a, b) (a <= b ? a : b)
#define swap(a, b, idx) (a[idx] = a[idx] ^ b[idx] ^ (b[idx] = a[idx]))
int unique(char*a) {
char c[256] = { 0 };
int u = 0;
while (*a) {
u += (c[*a] == 0);
c[*a++] = 1;
}
return u;
}
void swapWithMask(char* a, char* b, unsigned long int mask, int l) {
for (int j = 0; j < l; j++)
if ((mask & (1 << j)) != 0)
swap(a, b, j);
}
int minUnique(char*oa, char*ob) {
int l = strlen(oa);
int minu = l;
char *a = malloc(l + 1);
strcpy(a, oa);
char *b = malloc(l + 1);
strcpy(b, ob);
unsigned long int m = (1 << l);
for (unsigned long int i = 0; i < m; i++) {
swapWithMask(a, b, i, l);
minu = min(max(unique(a), unique(b)), minu);
swapWithMask(a, b, i, l);
}
free(b);
free(a);
return minu;
}
int main(void) {
puts((minUnique("directi", "itcerid") == 4) ? "ok" : "fail");
puts((minUnique("ababa", "babab") == 1) ? "ok" : "fail");
puts((minUnique("abaaa", "baabb") == 2) ? "ok" : "fail");
return 0;
}

Cant make a nested loop

This loop works as long as l is 1 and h can be any number. But i need it to work from different ranges such as l = 20 h = 40? Can anyone tell me how to do it? I would greatly appreciate it.
#include <iostream>
#include <vector>
#include <list>
#include <math.h>
#include <algorithm>
using namespace std;
int main(void)
{
int a, b, c, i = 0;
unsigned long l = 1;
unsigned long h = 25;
int array[3];
for ( a = l; l <= a && a <= h; ++a )
for ( b = a; l <= b && b <= h; ++b )
for ( c = b; l < c && c <= h; ++c )
if ( a * a + b * b == c * c )
{
array[0] = a;
array[1] = b;
array[2] = c;
if (array[0]+array[1]+array[2] <= h)
cout << array[0] << " " << array[1] << " " << array[2] <<endl;
else
break;
}
return 0;
}
If I understand you right, you're trying to bruteforce Diophant's system
a^2 + b^2 = c^2
a + b + c < h
this is the solution
#include <iostream>
using namespace std;
int main() {
const int l = 1;
const int h = 25;
for ( int a = l; a <= h; ++a )
for ( int b = l; b <= h; ++b )
for ( int c = l; c <= h; ++c )
if ((a * a + b * b == c * c ) &&
(a + b + c <= h))
cout << a << " " << b << " " << c <<endl;
return 0;
}
the output is:
3 4 5
4 3 5
6 8 10
8 6 10
if you don't need to distinguish a and b, second cycle could be
for ( int b = a; b <= h; ++b )
so you will get this:
3 4 5
6 8 10
There is something funny with this line, I can't tell what you are trying to do...
for ( a = l; l <= a && a <= h; ++a )
So, on the first round a=l, a++, and the condition is checked l<=a. a will be l+1, which means that l<=l+1, so you are going to exit your loop after the first time. I suspect that is not the behavior you want, but I really don't know what you do want. I could speculate that you want something like this:
for ( a = 0; l <= a && a <= h; ++a )
EDIT: From your comments, I can see what you are trying to do, and this should work better. Basically, you don't need to have the conditional for the lower value, it is the root of your problems. Also, I don't see why you bother to put the values into an array, which is written over each time, so I removed that.
for ( a = l; a <= h; ++a ) {
for ( b = a; b <= h; ++b ) {
for ( c = b; c <= h; ++c ) {
if ( a * a + b * b == c * c ) {
if (a+b+c <= h) {
cout << a << " " << b << " " << c <<endl;
}
else {
break;
}
}
}
}
}
I think one problem is in your third loop, which is slightly different from the other two:
for ( c = b; l < c && c <= h; ++c ) {
On the first pass, a == 1 and b == 1 and l == 1 so c is set to 1, and l < c evaluates to false, so the inner loop does not execute.
You really don't need to test your lower bounds (the l < c, or l <= b etc in earlier loops) because you know from the way you set them up that the condition should be true, except when you make a typo in the condition.
The canonical form for a for loop in C++ is:
for (int i = lo; i < hi; ++i)
for a suitable type (int here), index variable (i) going from a lower bound lo up to but not including an upper bound hi. This works in C99 too, but not C89. If you need the loop index's value after the loop completes, you might declare the variable at larger scope than just the loop as shown, but you usually avoid doing that. (I used i++ in a comment because I'm an unreformed C programmer, but the pre-increment is better in C++ in general.)
at the first loop the condition will be false from the first time because l>a

Project Euler 009 Problems

In Project Euler's problem 9, I encounter a problem: infinite loops.
Here is my code:
#include <iostream>
#include <cmath>
bool isPythagorean(int a, int b, int c);
int main(){
int a;
int aa;
int b;
int bb;
int c;
for(a = 0; a <= 1000; a++){ /*a loop*/
aa = a;
for(b = aa; b <= 1000; b++){ /*b loop*/
bb = b;
for(c = bb; c <= 1000; c++){
if(isPythagorean(a,b,c)){
if(a + b + c == 1000){
std::cout << (a * b) * c;
return 0;
}
else
continue;
}
}
}
return 1;
}
bool isPythagorean(int a, int b, int c){
int Pa = (int) pow(a, 2);
int Pb = (int) pow(b, 2);
int Pc = (int) pow(c, 2);
if(a < b && b < c){
if(Pa + Pb == Pc)
return true;
else
return false;
}
else
return false;
}
Courtesy of everyone who has helped the idiot writing this, the code has been changed, but the error still stands:
When the code is run, nothing is output to the terminal. Could anyone kindly tell me what is going wrong here?
(I am such an idiot; My thanks go to everyone that is even looking at this.)
Thank you, istrandjev for noticing a whole host of bad pieces of code.
Thank you, Blastfurnace for noticing that stupid error.
I don't get much of the logic you are trying to apply. What is a%1==0&&b%1==0&c%1==0 checking? You can simply write it if(true), you know. Also when is the cycle supposed to end? How is a triplet supposed to be Pythagorean if one of the conditions is a > c and then you want to have a*a + b*b == c*c?

finding a pythagorean triplet (project euler)

I'm well aware this brute force method is bad and that I should be using something like Euclid's formula, and that the final loop isn't needed as c = 1000 - (a + b) etc... but right now I just want this to work.
bool isPythagorean(int a, int b, int c) {
if((a*a + b*b) == c*c && a < b && b < c) {
cout << a << " " << b << " " << c << endl;
return true;
} else {
return false;
}
}
int main()
{
int a = 1;
int b = 2;
int c = 3;
for(a = 1; a < b; ++a) {
for(b = 2; b < c; ++b) {
for(c = 3; a + b + c != 1000 && !isPythagorean(a, b, c); ++c) {
}
}
}
return 0;
}
For the most part, the code works as I expect it to. I cannot figure out why it is stopping shy of a + b + c = 1000.
My final triplet is 280 < 294 < 406, totalling 980.
If I remove the a < b < c check, the triplet becomes 332, 249, 415 totalling 996.
All results fit the pythagorean theorem -- I just cannot land a + b + c = 1000.
What is preventing me?
This part of the code iterates very strangely:
for(a = 1; a < b; ++a) {
for(b = 2; b < c; ++b) {
for(c = 3; a + b + c != 1000 && !isPythagorean(a, b, c); ++c) {
}
}
}
Initially, a = 1, b = 2, c = 3. But upon the first for(c), c=997, so the second iteration of for(b) will run up to b=996. Keep doing this, and at some point you find a triple (a,b,c), at that point, c is probably not close to 1000, b will iterate up to whatever state c was is in... and so on. I don't think you can accurately predict the way it's going to come up with triples.
I suggest you go with something like
for(a = 1; 3*a < 1000; ++a) {
for(b = a+1; a+2*b < 1000; ++b) {
for(c = b+1; a + b + c != 1000 && !isPythagorean(a, b, c); ++c) {
}
}
}
That way, loops won't depend on the previously found triple.
... and you really should use Euclid's method.
The condition in your innermost for loop explicitly says to never test anything where a + b + c is equal to 1000. Did you mean a + b + c <= 1000?
Alternate possible Solution:
#include <iostream>
#define S(x) x*x
int main() {
int c = 0;
for(int a=1;a<(1000/3);++a) {
// a < b; so b is at-least a+1
// If a < b < c and a + b + c = 1000 then 'a' can't be greater than 1000/3
// 'b' can't be greater than 1000/2.
for(int b=a+1;b<(1000/2);++b) {
c = (1000 - a - b); // problem condition
if(S(c) == (S(a) + S(b) ))
std::cout<<a*b*c;
}
}
return 0;
}
For additional reference please refer the following posts
Finding Pythagorean Triples: Euclid's Formula
Generating unique, ordered Pythagorean triplets