0

why this code ask two input and then run please help

#include<stdio.h> struct Array { int A[10]; int size ; int length; }; void Display(struct Array arr) { for(int i=0; i<arr.length; i++) { printf("%d \n\n",arr.A[i]); } } int Search(struct Array Panda,int a) { for(int i=0; i<Panda.length; i++) { if(a==Panda.A[i]) return i+1; } return -1; } int main() { int a; struct Array panda= {{2,3,4,5,6},10,5}; Display(panda); printf("Enter number to search : "); scanf("%d \n",&a); printf("Found at %d position ",Search(panda,a)); //If we get -1 as return then //it means number not found return 0; }

28th Jan 2022, 7:58 AM
Vikalp Monas
Vikalp Monas - avatar
3 Answers
0
No it's asking only one input. try again
28th Jan 2022, 10:20 AM
Jayakrishna 🇼🇳
0
No brother after we entered which number we had to search if we chose example 4 then we need to enter another number to run code Answer is correct but why we need to enter one another number
28th Jan 2022, 3:38 PM
Vikalp Monas
Vikalp Monas - avatar
0
#include<stdio.h> struct Array { int A[20]; int size ; int length; }; void Display(struct Array arr) { for(int i=0; i<arr.length; i++) { printf("%d \n\n",arr.A[i]); //this displays 5 elements of A[] array if structure } } int Search(struct Array Panda,int a) { for(int i=0; i<Panda.length; i++) { //Panda.length=5 you assigned if(a==Panda.A[i]) return i+1; //this is position of element index+1 } return -1; } int main() { int a; struct Array panda= {{2,3,4,5,6},10,5};//here you have length variable assigned 5 Display(panda); //displays 5 elements of A[] array printf("Enter number to search : "); scanf("%d \n",&a); //this is only only you are asking .. printf("Found at %d position ",Search(panda,a)); //this will Search and return a value //If we get -1 as return then //it means number not found return 0; } //i enter 4, result is found at 3 //i enter 7 result is -1 //even if you have {1,2,3,4,5,6,7},for input 7 it returns -1
28th Jan 2022, 6:05 PM
Jayakrishna 🇼🇳