Sum relative numbers C++ [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
There is any function to sum relative numbers?
Example
I Have 5 and -10 So the result should be: 15
or
-5 (+) 15 -> 20
-1 (+) 1 -> 2
There is any function in C++ to sum numbers like that?

Do you intend absolute values? You can use the abs function.
abs(-5) + abs(15) gives 20 as a result.

I don't know about such function, however you can simply create one: just add absolute values:
#include <iostream>
int sumAbs( int a, int b) {
return std::abs( a) + std::abs( b);
}
int main() {
int a = -5;
int b = 10;
std::cout << sumAbs( a, b); // 15
return 0;
}

Do you mean the difference? abs(15 - (-5)) also gives 20.

Related

Use of . / in c++ - operator or float? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
The following piece of code outputs 0 and I'm not sure why. I'm not sure what the meaning of of . is in this context. Is it an operator or is it just indicating a float? Is it related to *Int?
#include <iostream>
using namespace std;
int main(){
int *Int = new int;
*Int = 1 / 2 * 2 / 1. * 2. / 4 * 4;
cout << *Int;
return 0;
}
It's not an operator. It indicates a double, not a float.
42. means 42.0, and .42 means 0.42. A . alone is a compiler error (rather than 0.0).
If you add a trailing f, it will become a float instead of double, e.g. 1.f, .1f, 1.0f.
1 / 2 == 0, 0 multiplied by anything is 0 again.

C++ effective calculation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
which is the most effective way to calculate does double is equal to another double, does it less with 1 or greater with one of it.
EDIT:
Example 18 > 18, 17 and 19 - true; 5,5 > 5,5, 4,5, and 6,5 - true.
If 18 is is in range between 17 and 19 - return true. Else return false.
If 5,5 is equal to 5,5 or it is in range of 4,5 to 6,5 - return true. Else return false.
Code started:
bool inRange(double one, double two)
{
return (one== one);
}
#include <cmath>
bool inRange(double dNumber, double dMiddle)
{
return std::fabs(dNumber - dMiddle) <= 1;
}

How to trace a C++ program by hand? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
#include<iostream>
int fun(int &x,int y=10)
{
if(x%y==0)
return (++x);
else
return (y--);
}
int main()
{
int p=10,q=13;
q=fun(p,q);
cout<<p<<q<<endl;
q=fun(q);
cout<<p<<q<<endl;
q=fun(p);
cout<<p<<q<<endl;
return 0;
}
The output as shown is as follows:
1013
1010
1111
But when I try to solve it using pen and paper I am stuck. Please show me how to arrive at the correct output so as to keep my concepts clear.
p=10 q=13
1st call
p= 10 , q = 13
p is passed by reference
returned will be y-- since it is post decrement it will return 13
2nd call
q = 13
passed by ref
x = 13 y =10
return will be y-- which is =10 not 9 since it is post
now p = 10 and q =10
3rd call
x = 10 and y =10 by default
return ++x and x = 11 ,so q=11,now since its is call by ref x =11 so p=11 and

How to implement this exercise (dynamic array)? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm having like an assesment exercise.
So given a Number, for example 12345, I must find out the sum sequence of the digits of the given number (1 + 2 +3 + 4 +5) and then add to it the result (15), and repeat this till the sum sequence of the last number is a digit (in this case is 6).
Example : 12345 + 15 + 6 = 12366;
666 + 24 + 6 = 696;
I've been thinkig to store the digits in an array, but then I realized the array's size is static. Now I'm thinking to make a linked list, but I'm not really sure. Does it involve linked lists?
Just guide me to the right path. What should I use?
There's no magic needed here. Just do the obvious computation on integers:
int reduce(int n)
{
int result = 0;
while (n != 0) { result += n % 10; n /= 10; }
return result;
}
int your_problem(int n)
{
int result = n;
while (n >= 10) { n = reduce(n); result += n; }
return result;
}

trying to solve 8th project euler number 8 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This is the link of the problem.
https://projecteuler.net/problem=8
below is my code.
#include <stdio.h>
int main() {
long i,sum;
long temp = 0;
long arr[1000] = {
// Increasingly large number is ommitted//
// I just add ',' between each numbers//};
for(i=0; i<988; i++){
sum = arr[i]*arr[i+1]*arr[i+2]*arr[i+3]*arr[i+4]*arr[i+5]*arr[i+6]
*arr[i+7]*arr[i+8]*arr[i+9]*arr[i+10]*arr[i+11]*arr[i+12];
if(temp<sum){
temp = sum;
}
}
printf("%ld",temp);
return 0;
}
so I got 2091059712 which seems kind of reasonable answer.
The real problem here is, that you did not account for the size of the product. An integer is 10 digits max (2,147,483,647). So this or something alike might happen:
sum = 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9;
This gives: 2,541,865,828,329 which overflows your integer leading to undefined behaviour.
Use a larger integer type or take a different approach.
That's a brute force solution that will work fine for this size of problem.
Potential improvements:
Split the array on "0", and only test the substrings that are longer than the desired length.
Print out the numbers that ended up being the best substring. That way you can test that it actually is present in the original and the multiplication is done correctly.