| | |
help with recursion and iterative pls
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
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]
//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]
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-> 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)
if (choice==1){ cout<<"Please enter 10 numbers: "<<endl; for(int i=1;i<=10;i++) 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. //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
-chandra
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
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
(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
![]() |
Similar Threads
- factorial numbers!!! (Java)
- Recursion (C++)
- Types Of Recursion (C++)
- Recursivity (C++)
- help (Java)
- iterative v/s recursion ... (C)
- Recursion: when do you use it? (C++)
- Recursion (C++)
Other Threads in the C++ Forum
- Previous Thread: Heres a hard one: ostream include / msvcp90d.dll not working
- Next Thread: Win32 C++ Book
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





