A strange output in C++ [duplicate] - c++

This question already has answers here:
Strange numbers when array is not initialized in C++ [duplicate]
(3 answers)
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 1 year ago.
I have tried a simple code and found a strange error(wrt me)
it is something like s[10]
now i have put some number of values into this array.t
like s[0]=0;s[1]=1.s[2]=2 and all others are empty.
now i put a for loop to see how it goes and to my surprise after index 2, some random numbers popping up in output and idk why. It should be null and the loop should have exited but here, it gives me some output like 01248766575...
why is it happening? pls do help me if u know

Related

VS Code highlighting wrong errors [duplicate]

This question already has answers here:
c++ array - expression must have a constant value
(8 answers)
Why aren't variable-length arrays part of the C++ standard?
(10 answers)
Closed 1 year ago.
Even though my code is running correctly VS Code is highlighting it as an error. This started happening suddenly, until two hours back every thing was fine. I am compiling my code in C++.
void next_greater_element(int a[], int n)
{
int ans[n];
ans[n] is being highlighted as an error. The error says --
expression must have a constant value -- the value of parameter "n" (declared at line 102) cannot be used as a constantC/C++(28)
Image of the highlighted error

Write a c++ function that will prompt a user for an integer and convert it to binary and print in reverse order [duplicate]

This question already has answers here:
C++ - Decimal to binary converting
(33 answers)
Closed 3 years ago.
Here is my code, and my error message is, "error C4716: 'decToBinary': must return a value"
Basically, I want the user to input an integer and have the program return the binary expansion in reverse order. How do I go about fixing this? Thank you!
The error message is pretty clear: you declared that your function would return an int, but you never did.

Explain what for (char c : str) does? [duplicate]

This question already has answers here:
'colon' and 'auto' in for loop c++? need some help understanding the syntax
(3 answers)
colon in for loop in C++
(1 answer)
Closed 3 years ago.
I am asking since I couldnt immediately find an answer on google, I know the answer is simple.
what does for (char c : str) {} do in a for loop?
Thank you!
It iterates the individual characters of str, copying each one to the (local) variable c for use in each iteration of the loop.

Mysterious C++ declaration [duplicate]

This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 4 years ago.
Sorry for the dumb question (I'm a total C++ noob), but what does
int x[101010];
declare? Is it list, a vector et cetera? And what is the meaning of 101010? I have never seen a declaration like this.
At block scope, int x[101010]; declares a uninitialised array with 101010 elements.
At global scope, the effect is similar but the elements are set to 0.
Note that if you had written int x[010101];, then you would have created 4161 elements as a leading 0 denotes an octal literal in C++.
In C++, a good rule of thumb is to use a std::vector unless you have a good reason not to.

Why are my array values changing? [duplicate]

This question already has answers here:
How does a leading zero change a numeric literal in Java?
(3 answers)
Closed 7 years ago.
I'm trying to initialize an int array, but when I go back to reference it, my values change as you can see here. For example my values {010, 011} are changing to {8,9}. Can anyone tell me why this is happening? Thank you in advance!
Numbers starting with a zero are treated as octal by the compiler.
010 in octal is 8.
Maybe just use 10 to initialise the values.
By prefixing the 10 with 0, you are telling the compiler that it is an octal number(base-8 number). To solve this, simply initialize your values as {10,11}