+ 1

Any body tell me what is the error in this code i want to print the location of element?

#include<stdio.h> #define MAX_SIZE 100 int main() { int arr[MAX_SIZE]; int i,search,found,j; for(i=1; i<=5; i++) { printf("Enter the %d Element of an Array: ",i); scanf("%d", &arr[i]); } printf("Elements of an Array are:"); for(i=1; i<=5; i++) { printf("%d ", arr[i]); } printf("\nEnter the Element to search in an Array:"); scanf("%d", &arr[i]); found = 0; for(i=1; i<=5; i++) { if (arr[i] == search) { found = 1; break; } } if(found==1) { printf("Element %d found at %d Location",search,i); } else { printf("Element not found"); } return 0; }

13th Jan 2021, 12:45 PM
Sajjad Qayyum
Sajjad Qayyum - avatar
4 Respostas
+ 3
Sajjad Qayyum you are never assigning a value to the `search` variable. On line 18, where you should've assigned the input to the `search` variable, you are instead assigning it to `arr[i]`, i.e. `arr[6]`. Also, you are assigning, printing, and searching in the array from index 1 through 5, which is senseless and a but confusing. You should stick to using an array from the 0th index. Lastly, please copy your code and save it into a c++ code in the code playground, and then link it here so that referencing line numbers and compiler errors are easier.
13th Jan 2021, 1:04 PM
XXX
XXX - avatar
+ 3
Sajjad Qayyum , please do not post a complete code direct in the post. it is better to put it in playground and then link it here. Please also use an appropriate tag for mentioning the programming labguage. Here some hints how to get help from the community: https://www.sololearn.com/Blog/38/8-simple-rules-to-get-help-from-the-communit
13th Jan 2021, 3:23 PM
Lothar
Lothar - avatar
+ 1
On line 21: Seems like you did not assign the input to "search" variable.
13th Jan 2021, 1:05 PM
noteve
noteve - avatar
0
XXX bro can you rewrite this code again and send me here?
13th Jan 2021, 1:36 PM
Sajjad Qayyum
Sajjad Qayyum - avatar