Kann Fehler im Code nicht finden.
-
hallo leute!
Ich bin noch ein sehr blutiger anfänger in sachen programmiersprachen. naja hab mir halt so ein buch gekauft und soll jetzt ein programm als übung schreiben, dass 4 zahlen soritert und dann ausgibt. das hab ich auch gemacht kann den fehler jedoch nicht finden.

hier is mal der source code:
#include <iostream> using namespace std; int main() { int pos[4]={5,4,3,2}, hilf; for (int a = 0; a <=3; ++a) { cout<<pos[a]<<","<<endl; } for (int i=4; i > 0; i--) { for (int j=0; j < 4; j++) { if (pos[j] > pos[j+1]) { hilf = pos[j]; pos[j] = pos[j+1]; pos[j+1] = hilf; } } } for (int k = 0; k < 4; k++) { cout<<pos[k]<<endl; } return 0; }naja hoff ihr könnt mir helfen.
mfg
-
Hallo
Du sagst weder was falsch ist noch hast du den Debugger benutzt. Der ist bei logischen Fehlern oft ein guter Ratgeber.
chrische
-
Noch dazu funktioniert das Programm.
Es wird problemlos ausgeführt und sortiert sogar die Zahlen!
-
-
mahe schrieb:
Noch dazu funktioniert das Programm.
Es wird problemlos ausgeführt und sortiert sogar die Zahlen!debugger:
Run-Time Check Failure #2 - Stack around the variable 'pos' was corrupted.verwende visual c++ 2008, vl liegts ja daran.
aber wenn ich das programm ohne debugger ausführe wird:
5,
4,
3,
2,
-858993460
2
3
4ausgegeben.
-
wenn das :
for (int i=4; i > 0; i--) { for (int j=0; j < 4; j++) { if (pos[j] > pos[j+1]) { hilf = pos[j]; pos[j] = pos[j+1]; pos[j+1] = hilf; } } }bubblesort sein soll dann mach es doch mal so
for (int i=1; i < 4; i++) { for (int j=0; j < 4; j++) { if (pos[j] > pos[j+1]) { hilf = pos[j]; pos[j] = pos[j+1]; pos[j+1] = hilf; } } }
-
Wills mal nicht so einfach machen:
Wenn "j" von 0 bis 3 geht, dann ist "pos[j+1]" ein Problem -> (int pos[4]=...).
-
wow thx. jetzt funktionierts
