2 Replies - 8676 Views - Last Post: 26 June 2013 - 11:21 AM Rate Topic: -----

#1 smilkitty  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-June 13

C++ program: implementation of linear search

Posted 26 June 2013 - 10:25 AM

Given a list of integers and an element x, locate this list using a recursive implementation of a linear search. You are required to input the list of integers and x from the console.

I have written a program for the given question want to know if it is correct or what changes should I make.It is a c++ program to run in visual studios.


void main()
int a[20],n,x,i,flag=0;
cout<<”How many Elements?”;
cout<<”\nEnter Elements of the Array\n”;

cout<<”\nEnter Element to search:”;


cout<<”\nElement is Found at position “<<i+1;
cout<<”\nElement not found”;

This post has been edited by jimblumberg: 26 June 2013 - 10:35 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ program: implementation of linear search

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 4894
  • View blog
  • Posts: 15,283
  • Joined: 25-December 09

Re: C++ program: implementation of linear search

Posted 26 June 2013 - 10:40 AM

What version of Visual Stdio are you using?

The program you posted shouldn't compile on any of the recent versions of this compiler. Looks more like a Turbo-C++ program than a valid standard C++ program.

You need to find an indentation style you like and use it consistently. This will make your program much easier to read.

Next your include file should be <iostream> not <iostream.h> and main() should be defined to return and int, and you should return an int from this function.
int main()
   return 0;

Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 783
  • Posts: 2,767
  • Joined: 31-December 10

Re: C++ program: implementation of linear search

Posted 26 June 2013 - 11:21 AM

In order for you to do a search recursively, you are required to write a function that is recursive, meaning a function that calls itself. You would define the function so it takes a list of numbers, the current index to check, and the sentinel value that tells you when to stop. The function needs to know when to stop calling itself or else it would become an infinite loop because the function would just keep calling itself and not stop until you run out of memory or kill the program.

Another thing I noticed in your code is that you declare certain variables, with poorly chosen names, and you use the variable before it has been initialized with a proper value. When you declare a variable without a value, it will contain some garbage value that will most likely lead to a bug somewhere in your program if you decide to use that variable without setting its value first.

This post has been edited by vividexstance: 26 June 2013 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1