Forum: Linux-Unix Programmierung
Moderatoren: juergen, Martin ConradThema: hilfe!!
hallo!!
ich habe ein (für euch sicher lächerlich scheinendes) problem! ich bin anfängerin und muss in der schule ein programm schreiben, dass die kleinste und die größte dreier zahlen ermittelt!
hier der code:
#include
#include
int main()
{
char ch; // Zeichen von stdin
short a; // Zahl a
short b; // Zahl b
short c; // Zahl c
short min; // Minimum
short max; // Maximum
system("clear");
printf("Eingabe ganzzahliger Wert [short]: ");
scanf("%hd%c", &a, &ch); // %hd . . . short, &ch . . . Tastaturpuffer leeren
printf("Eingabe ganzzahliger Wert [short]: ");
scanf("%hd%c", &b, &ch);
printf("Eingabe ganzzahliger Wert [short]: ");
scanf("%hd%c", &c, &ch);
if (a>b>c)
{max = a;
min = c;}
else if (a>c>b)
{max = a;
min = b;}
else if (b>a>c)
{max = b;
min = c;}
else if (b>c>a)
{max = b;
min = a;}
else if (c>a>b)
{max = c;
min = b;}
else if (c>b>a)
{max = c;
min = a;}
putchar('\n');
printf("Der groesste Wert: %hd\n", max);
printf("Der kleinste Wert: %hd\n\n\n", min);
printf("Programmfortsetzung . . . Eingabetaste\n\n");
system("read");
system("clear");
return(0);
}
das (vorläufige) programm ist unter linux im kate-editor geschrieben. mein problem ist hauptsächlich die schleife!!
bitte um baldige hilfe
mfg
SoleroIce
ich habe ein (für euch sicher lächerlich scheinendes) problem! ich bin anfängerin und muss in der schule ein programm schreiben, dass die kleinste und die größte dreier zahlen ermittelt!
hier der code:
#include
#include
int main()
{
char ch; // Zeichen von stdin
short a; // Zahl a
short b; // Zahl b
short c; // Zahl c
short min; // Minimum
short max; // Maximum
system("clear");
printf("Eingabe ganzzahliger Wert [short]: ");
scanf("%hd%c", &a, &ch); // %hd . . . short, &ch . . . Tastaturpuffer leeren
printf("Eingabe ganzzahliger Wert [short]: ");
scanf("%hd%c", &b, &ch);
printf("Eingabe ganzzahliger Wert [short]: ");
scanf("%hd%c", &c, &ch);
if (a>b>c)
{max = a;
min = c;}
else if (a>c>b)
{max = a;
min = b;}
else if (b>a>c)
{max = b;
min = c;}
else if (b>c>a)
{max = b;
min = a;}
else if (c>a>b)
{max = c;
min = b;}
else if (c>b>a)
{max = c;
min = a;}
putchar('\n');
printf("Der groesste Wert: %hd\n", max);
printf("Der kleinste Wert: %hd\n\n\n", min);
printf("Programmfortsetzung . . . Eingabetaste\n\n");
system("read");
system("clear");
return(0);
}
das (vorläufige) programm ist unter linux im kate-editor geschrieben. mein problem ist hauptsächlich die schleife!!
bitte um baldige hilfe
mfg
SoleroIce
hilfe!!
Martin Conrad (webmaster) am 09.12.2003 um 17:50
Bis denne
Martin
--
0xC0FFEE
ich versteh das problem auch nicht so ganz ...
von der logik her passt ja alles.
von der logik her passt ja alles.
etwas skalierbarer und uebersichtlicher gings auch so :
#include <stdio.h>
#define ZAHLEN 3
int main(void)
{
short nums[ZAHLEN];
short min, max;
int i;
for ( i = 0; i < ZAHLEN; i++ ) {
printf("\n Bitte %d. Zahl eingeben ... ", i+1 );
scanf("%d", &nums[i] );
}
min = max = nums[0];
for ( i = 1; i < ZAHLEN; i++ ) {
if ( nums[i] < min )
min = nums[i];
if ( nums[i] > max )
max = nums[i];
}
printf("\n groesste Zahl : %5d", max );
printf("\n kleinste Zahl : %5d", min );
return 0;
}
wenn ich die
if (a>b>c)
{max = a;
min = c;}
else if (a>c>b)
{max = a;
min = b;}
else if (b>a>c)
{max = b;
min = c;}
else if (b>c>a)
{max = b;
min = a;}
else if (c>a>b)
{max = c;
min = b;}
else if (c>b>a)
{max = c;
min = a;}
schleife verwende ist die größte zahl -37000 und die maximale 2050 *gg*!!
gibt es eine andere möglichkeit, die schleife zu programmieren??
mfg
if (a>b>c)
{max = a;
min = c;}
else if (a>c>b)
{max = a;
min = b;}
else if (b>a>c)
{max = b;
min = c;}
else if (b>c>a)
{max = b;
min = a;}
else if (c>a>b)
{max = c;
min = b;}
else if (c>b>a)
{max = c;
min = a;}
schleife verwende ist die größte zahl -37000 und die maximale 2050 *gg*!!
gibt es eine andere möglichkeit, die schleife zu programmieren??
mfg
