From 21a17e33fa0cec807cc0cbd226fa45ed87dd3f57 Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Thu, 24 Apr 2025 08:20:09 +0200 Subject: [PATCH] Add more C code --- C/helloworld.cpp | 16 ++++++++++++++++ C/histogram.cpp | 30 ++++++++++++++++++++++++++++++ C/histogram2.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100755 C/helloworld.cpp create mode 100755 C/histogram.cpp create mode 100755 C/histogram2.cpp diff --git a/C/helloworld.cpp b/C/helloworld.cpp new file mode 100755 index 0000000..9ec0f09 --- /dev/null +++ b/C/helloworld.cpp @@ -0,0 +1,16 @@ +#include +#include +#include + +using namespace std; + +int main() +{ + vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; + + for (const string& word : msg) + { + cout << word << " "; + } + cout << endl; +} diff --git a/C/histogram.cpp b/C/histogram.cpp new file mode 100755 index 0000000..db09c6d --- /dev/null +++ b/C/histogram.cpp @@ -0,0 +1,30 @@ +#include + +int main() +{ + // Deklaration der Variablen + int hist[26] = {}; + int i, k, c, count; + int num = 0; + + // Zeichen bis EOF einlesen (nur a-z werden berücksichtigt) + // EOF wird im Terminal mit CTRL-D eingegeben! + while ((c = getchar()) != EOF) { + if ((c>96) && (c<123)) { + // Zähler für diesen Buchstaben erhöhen + hist[c-97]++; + num++; + } + } + + // Histogramm ausgeben + printf("\n"); + for (i=0; i<26; i++) { + count = hist[i]; + if (count > 0) { + printf("%c : %f\n",i+97, (count/(float)num)*100); + } + } + + return 0; +} \ No newline at end of file diff --git a/C/histogram2.cpp b/C/histogram2.cpp new file mode 100755 index 0000000..8ae51b4 --- /dev/null +++ b/C/histogram2.cpp @@ -0,0 +1,31 @@ +#include + +int main() +{ + // Deklaration der Variablen + int hist[26] = {}; + int i, k, c, count; + + // Zeichen bis EOF einlesen (nur a-z werden berücksichtigt) + // EOF wird im Terminal mit CTRL-D eingegeben! + while ((c = getchar()) != EOF) { + if ((c>96) && (c<123)) { + // Zähler für diesen Buchstaben erhöhen + hist[c-97]++; + } + } + + // Histogramm ausgeben + printf("\n"); + for (i=0; i<26; i++) { + count = hist[i]; + if (count > 0) { + printf("%c : ",i+97); + for (k=0; k