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
I came across this code:
#include<iostream>
using namespace std;
int main()
{
int a=5,
b=a++ - 2;
cout << b;
return 0;
}
The output is 3. Why is it not 4?
-2 or - 2 should not give any error
see there are two types of operator post increment and pre increment
a++ is post increment it means first it will assign the value then it will increase the value by 1
meaning b = 5 - 2;
a will get get increased by 1
a=6 now but in the equation it will be 5
but if you do ++a
then it will first increase the value then assign
meaning b = 6 - 2;
-2 or - 2 wont give any error
check here
Lets break it down step by step.
These type of comma separated expressions happen from left to right. So its the same as this,
int a = 5;
int b = a++ - 2;
In this, a++ increments the value of a by one and then assigns it to it. Then the - 2 happens. Simply what happens under the hood is,
// Here 5 is the value of a.
int b = 5 - 2, a = a + 1;
More info: Incrementing in C++ - When to use x++ or ++x?
Related
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.
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.
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
Hey I have a problem,
The question is as follows in my book (not homework lol):
Give the value of the variables and continue with the recent gotten value:
int i,j,k;
i=j=2;
k=3;
Expression
1) i = ++j + ++k
Result after expression:
int i=3 (or 7?);
int j= 3;
int k=4;
2) (following the values after first exercise)-> i = ++j + k++
Result after expression:
int i=3 (or also 8, since k is +1 after the expression?)
int j= 4;
int k=5;
3) (following the values from #2) -> i = j++ + ++k
Result after expression
int i=3 (or also 10? since j is +1 after the expression, so we take value from #2 ?)
int j=5;
int k=6;
I am very confused since I can not check it on my code editor program, it could be easier if i was named: int answer; i guess but how do I check if what i got for int i is true?
Hopefully someone can help me!
Cheers
I identified
how do I check if what i got for int i is true?
as being your main question?
You check it by writing a short C program that calculates the values (which is simple copy-pasting) and prints them on the console, i.e. with
printf("After assignment: i=%i j=%i k=%i\n", i, j, k);
(don't forget to #include <stdio>)
Then you simply compile and run it and check its console output.
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 8 years ago.
Improve this question
I am trying to figure out the following problem for an upcoming test. I have searched everywhere, and I understand the basics of recursion. What I don't understand for this particular question is the value of int n and int k respectively. I have the answer to this question as it is a practice question, but I have no idea how the answer was found.
// Precondition: n and k are non-negative integers
int f(int n, int k) {
if (k * n == 0)
return 1
else
return f(n - 1, k - 1) + f(n - 1, k)
}
What value is returned by the call f(4, 2)?
Just look at how it's called.
f(4,2) goes into 2nd block, calls f(3,1)+f(3,2)
f(3,1) calls f(2,0)+f(2,1) = 1+f(1,0)+f(1,1)=1+1+f(0,0)+f(0,1)=1+1+1+1=4
f(3,2) calls f(2,1)+f(2,2)= f(1,0)+f(1,1)+f(1,1)+f(1,2) and so on.
You should be able to work it out from here.
I am not sure what the problem is since
f(4,2)=f(3,1) + f(3,2)
=(f(2,0)+f(2,1) )+ (f(2,1) +f(2,2))
=(1 +(f(1,0)+f(1,1))+((f(1,0)+f(1,1))+(f(1,1)+f(1,2))
=(1 + 1 +(1+1)) +( 1 +(1+1) + (1+1) +1 + 1 ))
=11
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.