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 4 years ago.
Improve this question
I'm having trouble on the last part of my program.
The code substracts an array p[j] with an input number nStevilo and then puts in into absolute. It then compares the result with a substraction of an array p[j++].
The code should go through the array and find the smallest value and append it into najblizjeStevilo, but for some reason it doesn't work?
while (j < 20){
if (abs(p[j] - nStevilo) < abs(p[j++] - nStevilo)){
najblizjeStevilo = p[j];
}
}
The array includes 20 prime numbers, starting from 2 (2, 3, 5, 7, 11...), so p[0] = 2, p[1] = 3...
You are relying on sequencing that isn't there. You assume that j will be incremented only after the left hand side of the comparison has finished running. There is no such guarantee by the C++ standard. So your program has undefined behavior on account of modifying j and reading it in one full expression without proper sequencing.
Rather than being clever and writing j++ opt instead to be explicit in how things need to be sequenced:
while (j < 20){
if (abs(p[j] - nStevilo) < abs(p[j + 1] - nStevilo)){
++j;
najblizjeStevilo = p[j];
}
}
Related
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.
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 6 years ago.
Improve this question
I am working on an assignment where I am supposed to try out a class I made in four different ways.
This shall be done 100 times in every way, and every single of those 100 iterations contains another for-loop wich runs 5000 iterations.
My problem is that. whilst running, the program randomly stops. It just stops.
No exception. No breakpoint. No crash. No nothing.
It just stops computing.
I've written down a couple of the places it stops
Simplifyed code:
for(int i = 0; i < 100; i++)
{
//Some stuff
for(int j = 0; j < 5000; j++)
{
//Some other stuff
cout << i << "\t" << j << endl;
}
}
Som of the places it has stopped computing at are:
3 3564
1 2273
1 4999
2 4999
0 3430
7 4566
1 4916
0 4999
So the only pattern I see is that it quite often stops at 4999 of the "j-loop" and very early in the "i-loop".
I am really confused about this since I'm sure that what I'm doing in the loops should not be a problem.
Please help!
Complete code Link
In the first line:
for(int i; i < 100; i++)
the variable i is not initialized. So it contains an arbitrary value that gets incremented every iteration and eventually it stops. Try initializing it: for(int i = 0; i < 100; i++).
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
That is the code:
point [1][2][1] = 3;
cout << point[1][2][1] << endl;
point [1][3][0] = 4;
cout << point[1][2][1] << endl;
And that is what the console says when I run the application:
3
4
If I change to
point[1][3][0] = 5;
it says
3
5
How can I remove this annoying error? I cant continue that way.
When your variable is declared as
int point[100][100][1];
Then the valid indexes are respectively 0...99, 0...99, 0...0.
Your access to point[1][2][1] is therefore quite inappropriate. Depending on which index you make out of range, you might access an area outside the array entirely, or in a different slice of the array.
If you really want to access array elements arbitrarily, then I suggest you discard the triple-subscript notation and use:
int point[m][n][p];
int* p = &point[0][0][0];
p[x*n*p + y*p + z]
Now you are in control over row-major vs column-major access, and any computation that yields an offset less than m*n*p is valid.
Note that in your case m=n=100 and p=1, so that point[1][3][0] is p[1*100*1 + 3*1 + 0] = p[103] and also point[1][2][1] is p[1*100*1 + 2*1 + 1] = p[103]. So both really are the same location.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
int rVals[];
string rNum;
for (i=0; i < rNum.length(); ++i) {
if((rVals[i] < rVals[i+1]) && (rNum[i] =='C' || rNum[i]=='X' || rNum[i]=='I')){
continue; //checks to see if preceeding value is < the next value
} else {
valid = false;
cout << "you can't subtract by M, D, L, or V\n" << endl;
break;
}
}
rVals[] is a dynamic array and is set correctly. No matter what the input is the if statement seems to evaluate to false. what is wrong with the if statement?
Take a look at this: rVals[i] < rVals[i+1]. If rVals length is 10 for instance and i is 9 rVals[i+1] will "point" to the 11th element of the array (since the indexing of an array is starting from 0 and between 0 and 9 you heave 10 elements - the size of our array).