This question already has answers here:
C++ array[index] vs index[array] [duplicate]
(4 answers)
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 4 days ago.
#include <iostream>
using namespace std;
int main()
{
char str[]="ABCD";
for(int i=0;str[i]!='\0';i++)
{
cout<<i[str]<<" ";
}
return 0;
}
The output of the following code is A B C D but i am not able to understand how we are accessing the array
Let's consider str has address 100 (Very simplified example).
It has 5 chars in it, so the addressed are 100, 101, 102, 103, 104, where in the 104 is '\0'
When you try to access str[i], it actually calculates the address str + i, and gets the value: *(str + i). And when you try to write the reversed way i[str], it also calculates the address i + str. So you get correct result.
Note that here is working pointer arithmetic (so for every type T you get the address pointer + sizeof(T))
And your
for(int i=0;str[i]!='\0';i++)
{
cout<<i[str]<<" ";
}
Will access addresses 100, 101, 102, 103, 104. And print the right values.
Related
This question already has answers here:
What is the behavior of integer division?
(6 answers)
Integer division always zero [duplicate]
(1 answer)
Random number c++ in some range [duplicate]
(6 answers)
Closed 3 years ago.
I need to produces numbers between 0 and a max (seen in code as assetMax). In the code, the rand()/RAND_MAX always produces 0 and I cannot seem to figure out why. I use the rand() function immediately before it to produce values in a range and it works completely fine. However, here it does not.
I have tried to switch the order of the variables, create the random number in a separate double before multiplying the two, and the header.
void cPortfolio::randomize(cProblem &portfolioProblem) {
int assetCount = 6 * rand() / RAND_MAX + (portfolioProblem.assetMax-8); //this line works as expected
int test;
for (int i = 0; i < assetCount; i++) {
double num = rand() / RAND_MAX; //this always produces 0.0000
int test = num * (portfolioProblem.assetNum); } `} //cannot format these correctly please ignore the brackets
This question already has answers here:
Why does rand() yield the same sequence of numbers on every run?
(7 answers)
Closed 5 years ago.
I created a random number generator (between 100 and 1) for an Array but each time I run it it outputs the same 10 numbers between 1 and 100.
for (int x=0; x < 10; x++) {
arr[x]=rand() % 100 + 1;
The Loop runs 10 times because the array contains 10 integers. Every time I run this though, it outputs: 42, 68, 35, 1, 70, 25, 79, 59, 63, 65.
It Outputs from this line of code
for (int z=0; z<10;z++) {
cout << arr[z]<<", ";
}
Can anyone see why this is happening?
When using rand() it will generate the same sequence from the given input to srand(). Another words it will typically use the same generated numbers from the initial seed value. These functions are considered to be frowned upon in their use and some even want them to be marked as deprecated in favor of the modern std pseudo-random number generators library.
To generate random numbers try using the library from the <random> header instead.
You can find the documentations here: cppreference::random
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 6 years ago.
Looking at some weird obfuscation contest code today I realized that array indexing is symmetric, in other words, x[n] is the same as n[x]. For example, consider the code below:
#include <iostream>
int main()
{
int x[] = {0, 1, 2, 3, 4};
std::cout << x[3] << ' ' << 3[x]; // both display 3
}
Live on Coliru
Is this indeed standard compliant, and if yes, is there any good reason why? And a bonus if you can provide a standard reference/quote.
PS: the code compiles fine with both gcc and clang
The reason is that in C (and C++) both expressions are equal to *(x + 3) == *(3 + x).
This question already has answers here:
Using unique dynamic variable names (not variable values!)
(4 answers)
Closed 8 years ago.
im not sure if someone asked this question before i couldn't find it in the search,
im using a normal while loop that generates a series of numbers and i want to store all these numbers as an int variable for each, here is my code so far
int numvalue = 30;
while (numvalue<100){
numvalue= numvalue + 10;
cout << numvalue<< endl;
}
Output:
40
50
60
70
80
90
100
so i need to store each output as an int variable and assign the names automatically , how should i go about this?
Variables are statically defined so you cannot create names for them from the code in the for loop.
But you can make lists of values. In your case using the std::vector is probably the best choice:
#include <vector>
int value = 30;
std::vector<int> values;
while (numvalue<100){
numvalue= numvalue + 10;
values.push_back(numvalue);
cout << numvalue<< endl;
}
So after the loop values will contain 7 values. values[0] will be 40, values[1] will be 50, etc.
This question already has answers here:
Pointer array and sizeof confusion
(5 answers)
Closed 8 years ago.
I wrote a code a fragment of which is shown below. I don't understand why it does not print 800 for the pointer variable p.
double *p = new double [100];
double q[10];
printf("Sizeof(p) = %d\n", sizeof(p)); // prints 4
printf("Sizeof(q) = %d\n", sizeof(q)); // prints 80
I understand why it prints 80 for q (8 bytes/double * 10) but why not 800 for p? An associated question would be, how does the compiler know how much space to deallocate when it encounters the delete for p?
delete [] p;
Because the actual pointer address is stored in 4 bytes. If you wanted the size of what p points to, you would say:
sizeof(*p);