C :: Aufgabe #4 :: Lösung #2
3 Lösungen
#4
Funktion welche Minimum und Maximum aus einem Array zurückgibt
Anfänger - C
von Gustl
- 07.12.2012 um 17:36 Uhr
Programmieren Sie eine Funktion welche aus einem Array von Integer-Elementen das Minimum und Maximum zurückgibt.
Als Übergabe-Parrameter wird das Array übergeben.
Testen Sie die Funktion mit 2 Abfragen, bzw. mit 2 verschiedenen Arrays.
Hilfestellung: Sortierverfahren - Wikipedia
Als Übergabe-Parrameter wird das Array übergeben.
Testen Sie die Funktion mit 2 Abfragen, bzw. mit 2 verschiedenen Arrays.
Hilfestellung: Sortierverfahren - Wikipedia
#2
von mraimbot (590 Punkte)
- 07.01.2019 um 18:08 Uhr
Zitat:
\C-Aufgaben> .\Aufgabe-0004.exe
Array: { 20, 133, -708, 0, 5780, 9820, -10003, 19 }
arrayInfo.min = -10003
arrayInfo.max = 9820
Array: { -5, -200, -708, 8900, -5780, -9820, 10, 10 }
arrayInfo.min = -9820
arrayInfo.max = 8900
///////////////////////////////////////////////////////////
//! @file Aufgabe-0004.c
//! @date 07.01.2019
//! @author Sebastian Schröder
//! @details
//! Compiler : gcc 5.1.0
//! C-FLAGS : -pipe -std=c99 -Wall -Wextra -pedantic -Werror -D_DEFAULT_SOURCE -O0 -g -o Aufgabe-0004.exe Aufgabe-0004.c
//! Testumgebung: Windows 10 - Windows PowerShell
//!
//! Aufgabe: Funktion welche Minimum und Maximum aus einem Array zurückgibt
//! Programmieren Sie eine Funktion welche aus einem Array von Integer-Elementen das Minimum und Maximum zurückgibt.
//! Als Übergabe-Parrameter wird das Array übergeben.
//! Testen Sie die Funktion mit 2 Abfragen, bzw. mit 2 verschiedenen Arrays.
//! @see https://de.wikipedia.org/wiki/Sortierverfahren
///////////////////////////////////////////////////////////
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#define ARRAYSIZE 8
typedef struct SArrayInfo {
int min;
int max;
} SArrayInfo;
SArrayInfo getMinMax(const int *sourceArray, int sourceLength);
void printArray(const int *sourceArray, int sourceLength);
int main (/*int argc, char **argv*/) {
int myArray1[ARRAYSIZE] = { 20, 133, -708, 0, 5780, 9820, -10003, 19};
int myArray2[ARRAYSIZE] = { -5, -200, -708, 8900, -5780, -9820, 10, 10};
SArrayInfo arrayInfo;
arrayInfo = getMinMax (myArray1, ARRAYSIZE);
printArray (myArray1, ARRAYSIZE);
printf ("arrayInfo.min = %11i\n", arrayInfo.min);
printf ("arrayInfo.max = %11i\n", arrayInfo.max);
printf ("\n");
arrayInfo = getMinMax (myArray2, ARRAYSIZE);
printArray (myArray2, ARRAYSIZE);
printf ("arrayInfo.min = %11i\n", arrayInfo.min);
printf ("arrayInfo.max = %11i\n", arrayInfo.max);
return EXIT_SUCCESS;
}
SArrayInfo getMinMax(const int *sourceArray, int sourceLength) {
SArrayInfo arrayInfo;
arrayInfo.max = INT_MIN;
arrayInfo.min = INT_MAX;
for (int i = 0; i < sourceLength; ++i) {
if (sourceArray[i] < arrayInfo.min) arrayInfo.min = sourceArray[i];
if (sourceArray[i] > arrayInfo.max) arrayInfo.max = sourceArray[i];
}
return arrayInfo;
}
void printArray(const int *sourceArray, int sourceLength) {
printf ("Array: { ");
for (int i = 0; i < sourceLength; ++i) {
if (i > 0) {
printf (", ");
}
printf ("%i", sourceArray[i]);
}
printf (" }\n");
}
#undef ARRAYSIZE
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
