943,749 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 541
  • C++ RSS
Jun 26th, 2009
0

help with recursion and iterative pls

Expand Post »
this is the instruction:
//Prompt the user with this menu:
1. Linear Search
2. Binary Search
3. Exit

Item 1 is similar to Exercise 1's item 5. This time however, the linearSearch function must be implemented using recursion.

Item 2 is what we call the Binary Search. As discussed, the implementation of binarySearch is iterative. //


below is my code:

which needs a lot of help.




[code = #include <iostream.h>
#include <conio.h>

//bool linearSearch(int[] ,int , int );
//bool binarySearch(int[],int x, int n);


void main(){
int choice, numbers[9], number ;

do{
cout<<"MENU:"<<endl;
cout<<"\t1.Linear Search\n\t2. Binary Search\n\t3. Exit\n";
cout<<"Enter your choice: ";
cin>>choice;

if (choice==1){
cout<<"Please enter 10 numbers: "<<endl;
for(int i=1;i<=10;i++)
cin>>numbers[9];

cout<<"Please enter 11th number: "<<endl;
cin>>number;

/* bool flag= linearSearch(numbers[],number,a);
if (flag==true){

cout<<"11th number is found in the previous ten numbers."<<endl;}
else if(flag==false){
cout<<"11th number is not found in the previous ten numbers."<<endl;}

}

*/




/* bool linearSearch(int nums[],int x, int n)
{

if(x==0); // size of the array
if(int nums[x-1]==n); // check if number in array is equal to 11th number
return true;
else
return linearSearch(int nums[],x-1,n); //goes to next number in array
else
return false;
}



*/


}

else if(choice==2){
cout<<"Please enter 10 numbers: "<<endl;
for(int i=1;i<=10;i++)
cin>>numbers[9];
cout<<"Please enter 11th number: "<<endl;
cin>>number;




/*bool binarySearch(int[9],int x, int n)
{*/

/*
lower=numbers[0];
upper=numbers[9];
mid= (lower+upper)/2;

while (lower!=upper)


*/


}
}
while (choice!=3);



}




] [/code]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamblaise is offline Offline
1 posts
since Jun 2009
Jun 26th, 2009
1

Re: help with recursion and iterative pls

Before even going into your logic i have a couple of things I can tell you

1-> Read the code-tags page carefully and learn to use them correctly. it would make your code much easier to read.
2-> don't use 'void main' and old style includes <.h>. Use int main(). Find out more about this in many threads on daniweb and on the web in general.
3->
c++ Syntax (Toggle Plain Text)
  1. if (choice==1){
  2. cout<<"Please enter 10 numbers: "<<endl;
  3. for(int i=1;i<=10;i++)
  4. cin>>numbers[9]; // This is wrong, you are writing each input on the last element on the array, numbers[9]. Make it numbers[i] and even for single line loops try to use {}, makes it more readable.
  5. //And yes arrays are 0 indexed, so your loop has to change to loop from 0 - 9.
Last edited by Agni; Jun 26th, 2009 at 5:43 am.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jun 26th, 2009
0

Re: help with recursion and iterative pls

Congratulations, Your code is rusted!!
That means it uses the style of coding that was used aprox 13 years ago. You are using all the deprecated and non portable headers. You are using void main, which is a sin.
In short you are practicing everything that a Standard C++ developer should not.
I bet you would be using the old, crappy, good for nothing Turbo C++ compiler.

Here is a guide which helps you to migrate to Standard C++ (the C++ which we use today)
If you are a school student, and your teacher teaches you rusted C++, you may still learn the standard C++ and use the rusted counterparts in your school.

The proper code tags are:
[code=cplusplus]
//your code goes here
[/code]


>which needs a lot of help.
What do you mean by 'lot of help'? We won't be correcting and rewriting the program for you. If you are unable to find the errors in such a trivial code, consult a good text book about C++

And as a Side note, did you bothered to read:
Announcement: We only give homework help to those who show effort
Read Me: Read This Before Posting
Last edited by siddhant3s; Jun 26th, 2009 at 6:54 am.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Heres a hard one: ostream include / msvcp90d.dll not working
Next Thread in C++ Forum Timeline: Win32 C++ Book





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC