
-----------------------------------
QuantumPhysics
Mon May 06, 2013 9:41 am

C Array won't search
-----------------------------------
My friend asked me for help as to why the for() is not searching for his number. The code is as follows:


int main(int argc, char *argv

He asked me the question and I was actually unsure as to why it isn't as well. Can anyone shed some light here?
I feel extremely disappointed that I couldn't help him with this -_- ... it's arrays. But still, I'm interested now.

-----------------------------------
QuantumPhysics
Mon May 06, 2013 9:56 am

Re: C Array won't search
-----------------------------------
Never mind, I found the answer. So silly. Hahaha, I remember how values should only be pre-declared in C99, but in C11 this problem does not exist. Therefore only by pre-defining the value inside the statement as a separate iterator in the for(), only then can this problem be solved. Meh.

-----------------------------------
bl0ckeduser
Mon May 06, 2013 10:01 am

Re: C Array won't search
-----------------------------------
There are quite a few things that are wrong with your (friend's) code.

1. You ask the user for 10 entries, but your array and both "for" loops can only deal with 5.
2. You forget to initialize "i" back to 0 in the second "for", which prevents it from running at all, because the previous "for" has brought "i" to its "maximum" value.
3. Your searching loop breaks and reports the number is not in the array if the number is not in the first index of the array. The correct behaviour would be to report that the number is not in the array if it is not found in *any* index of the array.

If you want I can post a corrected (working) version of the program.

-----------------------------------
QuantumPhysics
Mon May 06, 2013 11:08 am

Re: C Array won't search
-----------------------------------
Here is the final edit of the code. Simply storing myArray values into tempArray as a new index because previous apparently what I found out is you cannot access values in memory using the same memory address as previously declared you must use a new variable to access them.


#include 

// Write a C program to scan through an array to find a particular value. 

int main(int argc, char *argv

The only reason I did not use functions yet or void() was because my friend is still learning and he did not get there yet.

-----------------------------------
Insectoid
Mon May 06, 2013 3:40 pm

RE:C Array won\'t search
-----------------------------------
[code]for (i;i