completet chapter 5

This commit is contained in:
Jean Jacques Avril 2021-10-20 10:47:06 +02:00
parent d6287d6002
commit d76e6b7ffc
8 changed files with 113 additions and 2 deletions

View File

@ -1,5 +1,6 @@
{
"files.associations": {
"iterator": "cpp"
"iterator": "cpp",
"iostream": "cpp"
}
}

View File

@ -19,7 +19,7 @@ int* ret_array2(){
int main(){
int new_array[10];
ret_array(new_array,10); //Array is already a pointer, pointing to the first element;
ret_array(new_array,10); // But handled as a normal pointer we loose the array size
for(int i:new_array)
cout <<"Number "<<i<<endl;

View File

@ -0,0 +1,41 @@
#include <iostream>
using namespace std;
const int kEauationVal = 32;
template <class T>
void getVal(const char text[], T *val);
int main()
{
float fahrenheit = 0, schritt_weite = 0, maximal_wert = 0;
float celsiusBuf[10];
cout << "Eingabe\n\n";
getVal("Fahrenheit", &fahrenheit);
cout << "\nAusgabe\n\n";
for (int i = 0; i <10; i++)
{
celsiusBuf[i] = ((fahrenheit+i*10 - kEauationVal) * 5) / 9;
}
for (int i = 0; i <10; i++){
cout<<"Fahrenheit: "<<(fahrenheit+10*i)<<" --> Celsius: "<< celsiusBuf[i]<<endl;
}
return 0;
}
template <class T>
void getVal(const char text[], T *val)
{
do
{
cout << "\t" << text << " = ";
if (cin.fail())
{
cin.clear();
cin.ignore();
}
cin >> *val;
} while (cin.fail() || *val <= 0);
}

Binary file not shown.

View File

@ -0,0 +1,34 @@
#include <iostream>
#include <cstdlib>
using namespace std;
void sortieren(string *namen, int size);
int main(int argc, char *argv[])
{
string namen[5];
cout << "Bitte geben Sie 5 Namen ein:" << endl;
for (int i = 0; i < 5; i++)
{
cout << "Name " << i + 1 << ": ";
cin >> namen[i];
}
sortieren(namen, 5);
for(string name:namen)
cout<<name<<endl;
system("PAUSE");
return 0;
}
void sortieren(string *namen, int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = i + 1; j < size; j++)
if (namen[i] > namen[j])
{
string tempName = namen[i];
namen[i] = namen[j];
namen[j] = tempName;
}
}
}

Binary file not shown.

View File

@ -0,0 +1,35 @@
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void bubbleSort(int *array, int size)
{
for (int n = size; n > 1; n--)
for (int i = 0; i < n - 1; i++)
if (array[i] > array[i + 1])
{
int swap = array[i];
array[i] = array[i + 1];
array[i + 1] = swap;
}
}
void printArray(int *array, int size)
{
cout << "->";
for (int i = 0; i < size; i++)
cout << array[i] << (i == size - 1 ? "" : ";");
cout<<"\n\n";
}
int main()
{
srand(time(NULL));
int numbers[100];
for (int i = 0; i < 100; i++)
{
numbers[i] = rand() % 100 + 1;
}
printArray(numbers, 100);
bubbleSort(numbers, 100);
printArray(numbers, 100);
}

Binary file not shown.