@Maien sagte in Programm mit Dynamischen Vektoren:
Hättest du vielleicht noch eine Idee wie man die Funktion Selection Sort mit dynamischen Vektoren am besten schreiben könnte?
Passt doch was Du gemacht hast? Was gefällt Dir daran nicht?
Das andere Zeugs:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
void clear(FILE *stream)
{
for (int ch = fgetc(stream); ch != EOF && ch != '\n'; ch = fgetc(stream));
}
void selectionSort(double* x, size_t n)
{
assert(x && n > 0);
for (size_t i = 0; i < n - 1; i++) {
int min = i;
for (size_t j = i + 1; j < n; j++)
if (x[j] < x[min])
min = j;
if (min != i) {
double tmp = x[min];
x[min] = x[i];
x[i] = tmp;
}
}
}
double* scanVector(size_t n)
{
assert(n > 0);
double *x = malloc(n * sizeof *x);
if (!x)
return x;
for (size_t j = 0; j != n; ++j) {
while (printf("x[%zu] = ", j),
scanf(" %lf", &x[j]) != 1)
{
clear(stdin);
}
}
return x;
}
void printVector(double* x, int n)
{
assert(x && n > 0);
for (size_t j = 0; j < n; ++j)
printf("x[%zu] = %f\n", j, x[j]);
}
int main()
{
fputs("Geben sie die Länge des Vektors ein:\nN = ", stdout);
size_t N;
if (scanf(" %zu", &N) != 1) {
fputs("Input error :(\n\n", stderr);
return EXIT_FAILURE;
}
double *x = scanVector(N);
if (!x) {
fputs("Out of memory :(\n\n", stderr);
return EXIT_FAILURE;
}
selectionSort(x, N);
printVector(x, N);
putchar('\n');
free(x);
}