We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,866 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

the purpose of the code, please help

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.

3
Contributors
4
Replies
8 Hours
Discussion Span
9 Months Ago
Last Updated
5
Views
love_you_4rever
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

thank you zeroliken :)

love_you_4rever
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

love_you_4rever
Newbie Poster
10 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0890 seconds using 2.68MB