help with recursion and iterative pls

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 1
Reputation: iamblaise is an unknown quantity at this point 
Solved Threads: 0
iamblaise iamblaise is offline Offline
Newbie Poster

help with recursion and iterative pls

 
0
  #1
Jun 26th, 2009
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]
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 440
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: help with recursion and iterative pls

 
1
  #2
Jun 26th, 2009
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->
  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.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: help with recursion and iterative pls

 
0
  #3
Jun 26th, 2009
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.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC