Brauche Hilfe bei meinem Programm c++ (bin noch Anfänger)
Meine Frage:
Ich will mit meinem Programm 3 Zahlen vergleichen, die Eingegen werden der Größe nach zu sortieren. Es kommt aber nur eine 1 raus. Bitte um Hilfe
Meine Ideen:
Das ist mein Ansatz bisher:
#include<iostream>
#include<fstream>
using namespace std;
#include <stdio.h>
int main()
{
int x, y, z;
cout << ("Geben Sie drei Zahlen an. Diese werden dann sortiert wieder ausgegben.\n");
cout << "x:";
cin >> x;
cout << "y:";
cin >> y;
cout << "z:";
cin >> z;
{
if (x < y && y < z) { //erste Möglichkeit
std::cout << (z > y > x);
}
if (x < z && z < y) { //2. Möglichkeit
std::cout << (y > z > x);
}
if (y < x && x <z) { //3. Möglichkeit
std::cout << (z > x > y);
}
if (z < x && x < y) { //4. MöglichkeitM
std::cout << (y > x > z);
}
if (z< y && y < x) { //5. Möglichkeit
std::cout << (x > y > z);
}
if (y < z&& z < x) { //6.Möglichkeit
std::cout << (x > z > y);
}
}
RE: Brauche Hilfe bei meinem Programm c++ (bin noch Anfänger)
Du übersiehst außerdem die Möglichkeit, dass Zahlen auch gleich sein können.
Machen wir es mal für 2 Zahlen:
code:
1:
2:
3:
4:
5:
6:
7:
8:
//x, y einlesen überspringe ich
if (x > y) { //wenn x größer ist, tausche x und y
int tmp = x;
x = y;
y = tmp;
}
//jetzt ist x <= y
cout << x << " " << y;