0

Can anyone tell me what's wrong in my code ... I'm still a beginner started with c programming

#include <stdio.h> #include <stdlib.h> int main() { int a[10],i,j,temp,key ,low,high,mid,n; printf("Enter how many elements you want to store"); scanf("%d",&n); printf("Enter %d elements into your array",n); printf("\ngiven elements are \n "); for (i=0;i<n;i++) { printf("\t %d", a[i]); } for (i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]<a[j]) temp = a[i]; a[i] = a[j]; a[j] = temp; } } printf("given elements in ascending order "); for(i=0;i<n;i++) { printf("\t %d ",a[i]); printf("Enter search key elements"); scanf("%d",&key);

29th Dec 2023, 8:33 PM
Akhya Tist
Akhya Tist - avatar
4 Answers
+ 2
Hello @Akhya, In your code, you are not taking array elements as input from user therefore array is still empty. You need to take array elements as input using scanf function. Add the following line after the line "printf("Enter %d elements into your array",n);" - for(i=0; i<n; i++){ scanf("%d", &a[i]); } Above code will ask you to enter n integers as input. You can give input as space separated numbers like 10 20 30 25 23 or enter numbers one by one, pressing enter after each number.
30th Dec 2023, 3:21 AM
Vijay Meena
0
printf("Enter search key elements"); scanf("%d",&key); low = 0; high = n-1; }while(high>=low) { mid = (low+high)/2; if(key==a[mid]) { } break; else { if(key>=a[mid]) { low = mid+1; else { high = mid-1; if(key==a[mid]){ } } } } } printf("key %d found at %d position and %d location",key,mid,mid+1); else { printf("key %d not found",key); } return 0; }
29th Dec 2023, 8:35 PM
Akhya Tist
Akhya Tist - avatar
0
? Con-uses
31st Dec 2023, 12:44 PM
Misky F