C Array won't search
Author |
Message |
QuantumPhysics
|
Posted: Mon May 06, 2013 9:41 am Post subject: 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:
C: |
int main(int argc, char *argv[]) {
int myArray[5],i=0,f_num;
printf("Please enter 10 values into the array: \n");
for (i;i<5;++i){
scanf("%d",&myArray[i]);
}
printf("Please enter a number to search for: ");
scanf("%d",&f_num);
for (i;i<5;++i){
if (myArray[i]==f_num){
printf("Index of your value is: %d", i);
break;
}
else{
printf("The number you entered is not in the index.");
break;
}
}
return 0;
}
|
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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: Mon May 06, 2013 9:56 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
bl0ckeduser
|
Posted: Mon May 06, 2013 10:01 am Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: Mon May 06, 2013 11:08 am Post subject: 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.
C: |
#include <stdio.h>
// Write a C program to scan through an array to find a particular value.
int main(int argc, char *argv[]) {
int myArray[5],i=0,f_num,k=0,temp[5];
printf("Please enter 5 values into the array: \n");
for (i;i<5;++i){
scanf("%d",&myArray[i]);
temp[i]=myArray[i];
}
printf("Please enter a number to search for: ");
scanf("%d",&f_num);
for (k;k<5;++k){
if (temp[k]==f_num){
printf("Integer %d found at index #%d.", f_num, k);
break;
}
else if (k==4 && temp[4] != f_num){
printf("Integer %d was not found in index.",f_num);
break;
}
}
/*
for (k;k<5;++k){
if (myArray[k]==f_num){
printf("Index of your value is: %d", k);
break;
}
else{
printf("The number you entered is not in the index.");
break;
}
}
*/
return 0;
}
|
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. |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Mon May 06, 2013 3:40 pm Post subject: RE:C Array won\'t search |
|
|
This line is identical to the following:
The first statement in the for loop declaration is an initial instruction. The loop executes this line once before it begins looping. By just putting 'i' there, it really does nothing. This is equivalent to having the line 'i;' in your code. In fact, the compiler probably just gets rid of it on its own since it quite clearly does not need to be there. However, it would be useful to, say, set i to 0 in that statement. You've already set it to 0 earlier in the code, but what happens if you're messing around with it and accidentally change something? It's better to keep that statement with the for loop, isn't it?
code: | for (i = 0; i<5; ++i){ |
This allow you to remove the initial assignment earlier in your code. If you ever stick this block inside another loop, you don't need to worry about moving the assignment statement around because now it's part of the loop statement. Even if you don't do that, it means I don't have to hunt around to figure out what i or k are, since that information is in the same spot that you used it.
I feel like I wrote a lot for something that shouldn't need that many words... |
|
|
|
|
![](images/spacer.gif) |
iso
![](http://compsci.ca/v3/uploads/user_avatars/1110112343518c93b99a891.png)
|
Posted: Fri May 10, 2013 2:45 am Post subject: Re: C Array won't search |
|
|
Why not simply define something, a fixed number, as the length of your array, and use that instead of having arbitrary numerals in various places?
It would make the program more easily alterable and help avoid confusion. |
|
|
|
|
![](images/spacer.gif) |
|
|