dynamischer Speicher

This commit is contained in:
Leaced 2017-04-10 14:57:05 +02:00
parent e583c9ff83
commit edf26de378

View file

@ -5,6 +5,9 @@
#include<cstdlib> #include<cstdlib>
#include<ctime> #include<ctime>
void SelectionSort(long double *array, unsigned short amount);
void SelectionSort(int *array, unsigned short amount);
int main() int main()
{ {
std::string input; std::string input;
@ -14,28 +17,59 @@ int main()
getline(std::cin, input); getline(std::cin, input);
std::stringstream(input) >> amount; std::stringstream(input) >> amount;
long double array[amount];
std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n "; std::cout << "\nM\x94 \bchtest du mit zuf\x84lligen Zahlen arbeiten? y/n ";
getline(std::cin, input); getline(std::cin, input);
if(input == "y" || input == "Y") if(input == "y" || input == "Y")
{ {
int *array = new int[amount];
srand(time(0)); srand(time(0));
for(unsigned short idx = 0; idx < amount; idx++) for(unsigned short idx = 0; idx < amount; idx++) array[idx] = rand()%100;
{ SelectionSort(array, amount);
array[idx] = rand()%100; delete[] array;
}
} }
else else
{ {
long double *array = new long double[amount];
for(unsigned short idx = 0; idx < amount; idx++) for(unsigned short idx = 0; idx < amount; idx++)
{ {
std::cout << "Gib die " << idx+1 << ". Zahl ein: "; std::cout << "Gib die " << idx+1 << ". Zahl ein: ";
getline(std::cin,input); getline(std::cin,input);
std::stringstream(input) >> array[idx]; std::stringstream(input) >> array[idx];
} }
SelectionSort(array, amount);
delete[] array;
} }
getch();
return(0);
}
void SelectionSort(long double *array, unsigned short amount)
{
std::cout << "\n";
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
std::cout << array[amount-1] << "\n"; //Ausgabe des unsortierten Arrays
for(unsigned short idx = 0; idx<amount-1; idx++)
{
unsigned short indexMin = idx;
for(unsigned short idx2 = idx+1; idx2<amount; idx2++) if(array[idx2]<array[indexMin]) indexMin = idx2;
if(indexMin!=idx)
{
long double temp = array[idx];
array[idx] = array[indexMin];
array[indexMin] = temp;
}
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
}
}
void SelectionSort(int *array, unsigned short amount)
{
std::cout << "\n"; std::cout << "\n";
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", "; for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
@ -56,7 +90,4 @@ int main()
for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", "; for(unsigned short idx =0; idx<amount-1; idx++) std::cout << array[idx] << ", ";
std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt std::cout << array[amount-1] << "\n"; //Ausgabe des Arrays nach jedem Sortierungsschritt
} }
getch();
return(0);
} }