Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wie rekursiv lösen?

Empfohlene Antworten

Veröffentlicht

Hallo, ich habe ein Programm geschrieben und will es jetzt rekursiv lösen, ich habe aber keine Ahnung wie ich das am besten machen sollte. Kann mir da jemand helfen??? Hier das Programm:


#include<iostream>

#include<string>


using namespace std;


int strbinsearch(string*, string);

void main()

{


	int pos;

	string name;

	string Namen[]={"Alfons","Beate","Ines","Klaus","Michael","Nadine","Peter","Ruth","Sabine","Sebastian","Tom"};

	cout<<"Welcher Name soll gesucht werden?: ";

	cin>>name;

	pos=strbinsearch(Namen,name);

	cout<<"\nDer Name liegt auf Platz "<<pos<<endl;

}


int strbinsearch(string* Namen, string Name) // Das rekursiv lösen, nur wie??

{

	int l=0, r=10;

	int mitte;


	do

	{

		mitte=(l+r)/2;

		if(Name<Namen[mitte]) r=mitte--;

		else if(Name>Namen[mitte]) l=mitte++;

	}while(Name!= Namen[mitte] && r>=1);

	return mitte+1;

}

Du gibst deiner Funktion zwei weitere Parameter, die angiben in welchem Bereich als nächstes gesucht werden soll.


int strbinsearch(string* Namen, string Name, int lBound = 0, int UBound = 10) // Das ging doch auch mit C++, oder?
{
int mitte;

if (lBound > uBound) return -1; // nicht gefunden/ Fehler

mitte=(lBound+uBound)/2;
if(Name<Namen[mitte])
return strbinsearch(Namen, Name, lBound, mitte-1);
else if(Name>Namen[mitte])
return strbinsearch(Namen, Name, mitte+1, uBound);
else
return mitte +1;
}
[/PHP]

Danke....ich stand irgendwie auf dem Schlauch....;)

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.