2 Replies - 2425 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.


#include<iostream.h>
#include<conio.h>

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

for(i=0;i<n;++i)
cin>>a[i];
cout<<”\nEnter Element to search:”;
cin>>x;

for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}

if(flag)
cout<<”\nElement is Found at position “<<i+1;
else
cout<<”\nElement not found”;
getch();
}

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 offline

  • member icon


Reputation: 4005
  • View blog
  • Posts: 12,361
  • 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;
}



Jim
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 653
  • View blog
  • Posts: 2,240
  • 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