I got stuck :(. Can anyone tell me the purpose of this following code please? (Suggest descriptive names for the function and its parameters.)

int mystery(int x, int y[], int z) {
for (int i = 0; i < z; i++) {
if (y[i] == x) return i;
}
return -1;
}

thank you so much.

Recommended Answers

All 4 Replies

returns the index where the current element of array y is equal to x, else returns -1 if the value of x is not in the array

By the way, could you help me with this as well? this is the question:
what will print for a[], p, q after line 2, 3, 4 and 5? I have done for line 1, then I got stuck :(.

int a[] = { 0, 1, 2, 3, 4, 5, 6 };
int* p;
int* q;
p = a; q = p; *q = 6; // line 1
(*q)++; q++; (*q)++; // line 2
p = ++q; ++p; *p = 7; // line 3
*q = *p; // line 4
p[1] = q[-1]; // line 5

thanks

what will print

Nothing. There are no output statements.

Assuming that's not the correct answer, sit down with pen and paper and figure it out.

Write down the variables and their contents
Write down the pointers and where they point
Now follow the code statement by statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.