2D-Array deletes elements itself when new elements get stored [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
edit: I didn't initialise the array with the right size. I should have used 8 instead of 7. I had to learn that Arrays start counting at 0, but when initialising the don't!
I have a 2 dimensional Array storage[x][y] and want to store for each x value 8 different y values. For some reason I can read out a specific place in the array, for example [48][7] and after filling other spaces in the array the [48][7] value has changed even tough I never touched it.
int storage[127][7];
void store(){
storage[48][0] =0b01111110;
storage[48][1] =0b01111110;
storage[48][2] =0b11100011;
storage[48][3] =0b11010011;
storage[48][4] =0b11001011;
storage[48][5] =0b11000111;
storage[48][6] =0b01111110;
storage[48][7] =0b01111110;
Serial.println(storage[48][7], BIN); // returns 01111110
storage[49][0] =0b00000000;
storage[49][1] =0b11000001;
storage[49][2] =0b11000001;
storage[49][3] =0b11111111;
storage[49][4] =0b11111111;
storage[49][5] =0b00000001;
storage[49][6] =0b00000001;
storage[49][7] =0b00000000;
Serial.println(storage[48][7], BIN); // returns 0
}
Depending on the order in which I store the values some of them delete each other.
Why do I suddenly lose data?

You are defining a 2D-array with row-size 7, but you access 8 elements of a single row:
int storage[127][7];
storage[48][0] =0b01111110;
...
storage[48][7] =0b01111110;
Note that 0..7 are actually 8 elements, not 7. So you'd have to define storage as...
int storage[127][8];
One might discuss now if int storage[127][7];storage[48][7] =0b01111110 is undefined behaviour;
Yet the most probably behaviour is that storage[48][7] maps to the same memory address as storage[49][0]. Hence, when you assign storage[49][0] =0b00000000;, then you write to storage[48][7] "as well" and it will become 0b0000000.

Related

Why is 1e-12 + 1 = 1 in C++, but not in C? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
In C I put in a big number, like 1e-12, in float data and add 1. It's gave me a correct answer.
In C++ I made the same, but when I add 1 to 1e-12, it returns me 1.
float a = 1e-12;
std::cout << "The number is : " << a + 1 << std::endl;
Output:
The number is: 1
I don't have any error messages. The program just returns the wrong result.
(!! 1e-12 + 1 is not equal to 1!!)
Compilers by default take some short-cuts when doing floating-point math. (They typically have a command-line switch to enforce the rules)
In this case, the rule is that the compiler should store the double value 1e-12 as a float in a, and then, in the output statement, add 1 to the stored value.
The optimization is probably that one of the compilers never stored the value; instead, it added 1 to the double value 1e-12. With the higher precision initial value there are more low bits in the fraction part, and that will affect the result of adding 1.
So the results can be different, depending on how the compiler treats those values.
That's just handwaving, though; if this is really important to you, look at the machine code that the compiler generates in both cases to see what's being done differently.

Difference Between initializing an array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I have been getting a weird output if I initialize an array using a variable instead of a constant
the two code below produces a different output for me.
int x =7;
int arr[x];
and
int arr[7];
the first one generates this output
78 9 73 32 6422216 50 42
and the 2nd one
78 9 73 54 29 50 42
I need to use the size of an vector for the array size.
I have tried making the variable constant but it doesn't makes a difference.
edit
using the array here
int arr[size];
for(int j=i;j<nums.size();j++)
arr[j+1]=nums[j];
arr[i]=nums[signs.size()];
for(int j=0;j<nums.size();j++)
nums[j]=arr[j];
In neither of your two cases is the array initialized.
Without an explicit initializer, the contents of an array (or any variable) are indeterminate. You can't predict what those values will be. And if one of those values happens to be a trap representation, you invoke undefined behavior if you attempt to read that value.
If you want your array to have a particular set of values to start, you need to set those values explicitly, either with an initializer or by assignment.

how can I represent an array [100000][100000] storing only 0 or 1 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 7 years ago.
Improve this question
I tried:
vector<int <vector> > Graph(100000, vector<int> (100000)) ;
and
vector<bool<vector> >Graph (100000,vector<bool> (100000)) ;
and
bool Graph [100000][100000] ;
... but none of them are working.
The correct way to create a vector of vector is:
vector<vector <int> > Graph(100000, vector<int> (100000));
However note that this array will take 10^5 * 10^5 * 4 = 40 billion bytes (assuming your int is 32 bits) or about 40 GB of RAM. You will have to have that much memory of course.
Well doing this
int wMyArray[100000][100000];
will give you this compile-time error:error C2148: total size of array must not exceed 0x7fffffff bytes
And then doing it with vector:
std::vector<std::vector<int>> wMyVector(100000, std::vector<int>(100000));
wMyVector.back().back();
Just takes forever to load, you can actually see the amount of working bytes increasing when it's loading.
Not sure you really want to do this.

How do I initialize and cout an array in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I do have a problem with an array I am loading and I cout later on in the code.
The array is initialized as:
int array[51][2]={{1,2},{7,4},/* ... */ }
The dots imply the other 49 missing tuples.
Later on, when I just want to cout the array I use:
for(int k=0;k<nProducts;k++){
for(int t=1;t<=T;t++){
cout << array[k][t];
}
cout << endl;
}
Unfortunately this does not work at all. Visual Studio happily compiles the program, but when I run it (ConsoleApplication) it crashes at this point. When I comment out the "cout-part" it works fine. But I doubt the array is used as it should be in the rest of the code.
Could you help me somehow with that ? I just cannot find my mistake in any literature, which seems to perfectly do the same I do.
Change this:
for(int t=1;t<=T;t++)
To this:
for(int t=0;t<T;t++)
Indexes in C/C++/Java/etc. are between 0 and N-1.
You are accessing indices that are out of bounds - the array is indexed from 0, so the last valid index is N-1. When you try to access the element at index N, you get IndexOutOfRangeException which causes the termination of the program.

Generating normal distribution weighing constants in a range [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have some results whose relevance decreases with distance. I want to weight the result array elements with constants whose distribution is close to normal or folded normal. At start I want to generate an array with N constants starting from 1 to 0.01 by a function.
The result should be something like the following, ending with a number close to 0.01.
const double normalDistWeight[] = {
1.000, 0.997, 0.994, 0.989, 0.984, 0.977, 0.970, 0.961, 0.951, 0.939,
0.926, 0.910, 0.893, 0.874, 0.853, 0.830, 0.805, 0.778, 0.750, 0.719,
0.687, 0.654, 0.619, 0.584, 0.548, 0.512, 0.476, 0.440, 0.405, 0.370,
0.337, 0.305, 0.274, 0.246, 0.219, 0.194, 0.171, 0.150, 0.131, 0.114,
0.098, 0.085, 0.073, 0.063, 0.054, 0.047, 0.040, 0.035, 0.030, 0.027
};
Unfortunately I can't use any third party libraries or C++11 features, only plain C++.
Edit: Oh, I was over-thinking it... It's just a simple Gaussian error, so exp(-x^2) should work.
It appears to me that all you want is an array of values of the Gaussian function corresponding to uniformly spaced points on the positive half-axis, up to a point where the value is about 0.01.
This is straight-forward. The Gaussian function is f(x) = exp(−x2), like this:
In the chosen expression, we already have f(0) = 1, so all that remains is to find the final point x where we have f(x) = 0.01. Invert: x = √−log(0.01) &approx; 2.15.
So all you need to do is evaluate f on uniformly spaced points on the interval [0, 2.15].