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.
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
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?
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];
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Characterize the following algorithm in terms of Big-O notation. (Assume that all variables are properly declared.)
for (int i = 5; i <= 2 * n; i++)
cout << 2 * n + i - 1 << endl;
This question comes from the exercises in Data Structures using C++ (D.S Malik). I can't seem to make sense of it. I think I'm thrown off mostly by the conditional part of the for loop, i<=2*n.
See https://www.interviewcake.com/article/java/big-o-notation-time-and-space-complexity (drop the constants section)
So big O of 2n-5 is the same as big O of n.
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).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
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.
Improve this question
I'm trying to convert the individual contents of a string to integers. I need to take each character from the string and convert it to an integer to add to another. This is not using C++11. Is there a simple way to do it?
if the characters are numbers then the numeral value of each is
num_value(c) = c - '0'
This is only possible because the characters representing numbers are in order in the ASCII table.. All you have to do is loop across the string.
"I need to take each character from the string and convert it to an integer to add to another"
In case you want to calculate the sum of digits stored in std::string object, you could do:
std::string myNum("567632");
int sum = 0;
for (size_t i = 0; i < myNum.size(); ++i)
sum += (myNum[i] - '0');
std::cout << sum;
which outputs 29 (i.e. 5 + 6 + 7 + 6 + 3 + 2)
How about std::accumulate ?
#include<string>
#include<algorithm>
//...
std::string myNum("123456789");
std::cout<<accumulate( myNum.begin(), myNum.end(), 0,
[](int sum,const char& x){return sum+=x-'0'; });