W.A.P. C++

WAP | CPP | Programs in C++ | C++ Solutions

Snehil Khanor's Binary Log

Write a program to search an element in an array (Linear searching)


Size of array is taken from user.. Elements are entered.. Then user is asked to enter element to be searched.. Search is performed..
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int ar[10],n,num,no;
  7. cout<<"Enter size of the array: ";
  8. cin>>n;
  9. cout<<"Enter array element: "<<endl;
  10. for(int i=0;i<n;i++)
  11. {
  12. cout<<"Enter element "<<i<<": ";
  13. cin>>ar[i];
  14. }
  15. //Coding by: Snehil Khanor
  16. //http://WapCPP.blogspot.com
  17. cout<<"Elements of array: "<<endl;
  18. for(i=0;i<n;i++)
  19. cout<<"Element at index number "<<i<<" is: "<<ar[i]<<endl;
  20. //////To search////
  21. cout<<"Enter number to search: ";
  22. cin>>num;
  23. for(i=0;i<n;i++)
  24. {
  25. if(num==ar[i])
  26. {
  27. cout<<"Found at index number: "<<i;
  28. no=0;
  29. break;
  30. }
  31. else
  32. {
  33. no=1;
  34. continue;
  35. }
  36. }
  37. if(no==1)
  38. cout<<"Sorry your search returned NO results. :(";
  39. getch();
  40. }

1 comments:

Unknown March 21, 2013 at 11:06 PM  

there is a mistake in this program .
in this program u declared array with 10 size and then during program u again want size of array.
2ndly this may take values from user because of loop but when it cout the array it print numbers declared with in array size and values which is store in index number great then initialy declared size.its print the random values to that index number .hope u understand.03008526184.my number

Search

About WAP C++

Here you'll find a wide range of programs' solution ranging from beginer level to advanced level.
All programs here are made, compiled and tested by me..and are running absolutely fine.. still if you find any bug in any program do let me know :)

Followers

Subscribe via email

Enter your email address:

Delivered by FeedBurner

my Binary Log

Blog Archive



eXTReMe Tracker