C++ increment operator and sum [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
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.

Related

How to Return in Recursion [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 1 year ago.
Improve this question
I dont really understand i have written return in the end but still it gives error
CODE :
int factorial(int num)
{
int N;
if (num > 1)
{
N = (num * factorial(num--));
}
else
return N;
}
int main()
{
cout << factorial(5);
return 0;
}
ERROR : warning: control reaches end of non-void function [-Wreturn-type]
16 | }
Your issue is that you don't return anything. If you look at the flow of the program you can see that for num > 1 you do the factorial stuff and for num <= 1 you just return N. For num > 1 the return statement is never reached. This issue can be fixed by removing the else, BUT that leaves an other issue mentioned, namely that for num <= 1 N is never initialised. If you initialise it to 1 that should solve that, but as people pointed out you don't need N, you can do return num * factorial(num - 1); and simply return 1 for num <= 1. The final problem with your code is that you do num * factorial(num--). factorial(num--) will call factorial(num), when you would need factorial(num-1), because num-- is the post-decrement operator.
Other suggestions in the comments are good to heed as well, like implementing guards from integer overflow and the like.

Absolute difference of sum of two diagonals of a 2d array in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to obtain the absolute difference of the sum of left and right diagonal of the given 2d array.
I have written the following function-
int diagonalDifference(vector<vector<int>> arr) {
int n=arr.size();
int summ1=0,summ2=0,result=0;
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
{
if(i==j)
{ summ1=summ1+arr[i][j];}
else if((i+j)==(n-1))
{ summ2=summ2+arr[i][j];}
}
}
result=abs(summ1-summ2);
return result;
}
input array
11 2 4
4 5 6
10 8 -12
Explanation- summ1=11+5+(-12)
summ2=4++5+10 result=|4-19|=|-15|=15
expected output: 15
The output I am getting is 10
Here is a solution in O(n) complexity. reference
for (int i = 0; i < n; i++)
{
summ1 += arr[i][i];
summ2 += arr[i][n-i-1];
}
The else-if should be an if by itself.
The else-if prevents the 5 from being added to summ2. Which means the final calculation is: (11+5-12)-(4+10) = -10. Whose absolute value is 10.

Can someone explain what this code means? [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
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?

Recursion Help Please [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 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

SIGSEGV error details and removal steps [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 9 years ago.
Improve this question
for the question: http://www.codechef.com/FEB14/problems/TWODOGS
i wrote the following program but on the inline judge codechef i am getting sigsegv error. I want to know specifically which instruction is creating this error in my program so that i try and remove that.
here is the code:
#include<iostream>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int arr[n+1];
for(int i=1;i<n+1;i++)
arr[i]=n+1;
for(int i=0;i<n;i++)
{
int p=min(i+1,n-i);
arr[a[i]]=min(arr[a[i]],p);
}
int time=n+1;
for(int i=0;i<n;i++)
{
int p=a[i];
if(p!=k-p&&p<k)
time=min(time,max(arr[p],arr[k-p]));
}
if(time==n+1)
cout<<"-1";
else
cout<<time;
}
Given sample input 2:
Sample Input 2:
5 5
2 4 9 2 5
You cannot do this
arr[a[i]]=min(arr[a[i]],p);
because when i == 2, arr[a[i]] will be evaluated as arr[9], which is out of bounds and results modification of memory which you do not own, that is, undefined behavior. This is where your program is failing.
You'd have an easier time if your identifiers were more meaningful. That said:
cin>>a[i];
....
arr[a[i]]=min(arr[a[i]],p);
You are indexing arr[] by user input. If you are getting a segfault, it's best to put in asserts for these situations in case the assumption is invalid, something like:
assert(a[i] < n-1);
Next:
time=min(time,max(arr[p],arr[k-p]));
again, indexing array based on user input. Add another assert to test that arr[p] and arr[k-p] are valid.
Finally, for this line:
if(p!=k-p&&p<k)
Did you mean:
if(p!=k-p&&p<n)
a is allocated based on n, not k.
If you read the question carefully ,
the element in a[i] <= 10 ^ 6 .
You have declared an array arr of size n+1 .
Constraint on n : n <= 500000
Also you are trying to access arr[ a[i] ] which may go beyond the limit of array arr.
a[i] may be greater than 5 * 10^5 .
This is leading to segmentation fault error.