Inhalte zur Vorlesungswoche #3, Informatik 1 im Wintersemester 2024/25
(EN google-translate)
(PL google-translate)
Themen
|
1 Subtraktion im Binärbereich unter Verwendung des Zweierkomplements
7-3=4
Binär bei 8 Bit:
7 = 00000111
3 = 00000011
-3 = 11111100 + 1 = 11111101 (Zweierkomplement)
Subtraktion als Addition des Zweierkomplements:
7
-3
00000111
+11111101
(1)11111110 (Überträge)
--------
(1)00000100 = 4
Code 0-1: Beispiel zur praktischen Nutzung des Zweierkomplements für die Subtraktion.
|
3-7=-4
Binär bei 8 Bit:
3 = 00000011
7 = 00000111
-7 = 11111000 + 1 = 11111001 (Zweierkomplement)
Subtraktion als Addition des Zweierkomplements:
3
-7
00000011
+11111001
00000110 (Überträge)
---------
11111100 = -4
PROBE
11111100 = -4(?)
- 1
= 11111011
Einerkomplement:
00000100 = 4 OKAY.
Code 0-2: Subtraktion mit negativem Ergebnis und anschließender Proberechnung.
2 Einführung der for-Schleife
Es werden im Unterricht Beispiele kodiert.
#include <iostream>
using namespace std;
int main(void)
{
for(int i=0;i<4;i++)
{
cout<<i<<endl;
}
return 0;
}
Code 0-3: Beispiel for-Schleife.
3 Konsolidierung der Verwendung einfacher Integer-Arrays.
Es werden im Unterricht Beispiele kodiert.
#include <iostream>
using namespace std;
int main(void)
{
int arr[4];
for(int i=0;i<4;i++)
{
arr[i]=i*i;
}
for(int i=0;i<4;i++)
{
cout<<arr[i]<<endl;
}
return 0;
}
Code 0-4: Beispiel, Integer-Array mit vier Plätzen.
4 Zahlenumwandlungen programmieren (Dezimal-Binär / Binär-Dezimal) und Modulo-Division
Ausgangspunkt soll die Programmierübung der zweiten Gruppe von gestern sein und eine studentische Lösung der ersten Aufgabe:
Im Unterricht wurde eine Methode zur handschriftlichen Transformation einer im Dezimalsystem dargestellten Zahl ins Binärsystem besprochen.
Nachfolgend finden Sie ein kleines Programm, das diese Umwandlung automatisiert, jedoch die Ziffern in umgekehrter Reihenfolge ausgibt.
Dabei kommt die Modulodivision zum Einsatz, Operator %
Die Modulo-Division wird auch Rest-Division genannt.
Symbol des Modulo-Operators in der Programmiersprache C: % Beispiele: 4%2=0 5%2=1 6%3=0 8%3=2
Code 0-5: Die Modulo Division
#include <iostream>
using namespace std;
int main(void)
{
int x = 126;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
cout<<x%2;
x=x/2;
return 0;
}
Code 0-6: Einfaches Programm zur Binärdarstellung einer Zahl, wobei die Ziffern in umgekehrter Reihenfolge dargestellt werden.
|
#include <iostream>
using namespace std;
int main(void)
{
int x;
cout<<"Welche Zahl soll in Binär umgewandelt werden?"<<endl;
cin>>x;
int y[7];
if(x >= 0 && x <= 127)
{
for(int i = 0; i<=7; i++)
{
y[i] = x%2;
cout<<y[i];
x=x/2;
}
cout<<endl<<"Einerkomplement: ";
for(int i = 0; i<=7; i++)
{
y[i] = 1 - y[i];
cout<<y[i];
}
cout<<endl;
}
else
{
cout<<"Zahl ist nicht zwischen 0 und 127."<<endl<<"Das Programm wird beendet."<<endl;
}
return 0;
}
Code 0-7: Studentische Lösung zu Aufgabe 1.
Aufgabe 2
|
|
#include <iostream>
using namespace std;
int main(void)
{
int x;
cout<<"Welche Zahl soll in Binär umgewandelt werden?"<<endl;
cin>>x;
int y[8];
if(x >= 0 && x <= 127)
{
for(int i = 0; i<=7; i++)
{
y[i] = x%2;
//cout<<y[i];
x=x/2;
}
for(int i = 7; i>=0; i--)
{
cout<<y[i];
}
cout<<endl<<"Einerkomplement: ";
for(int i = 0; i<=7; i++)
{
y[i] = 1 - y[i];
//cout<<y[i];
}
for(int i = 7; i>=0; i--)
{
cout<<y[i];
}
cout<<endl;
}
else
{
cout<<"Zahl ist nicht zwischen 0 und 127."<<endl<<"Das Programm wird beendet."<<endl;
}
return 0;
}
Code 0-8: Verbessern der Ausgabe: Die erfolgt jetzt in der richtigen Reihenfolge.
#include <iostream>
using namespace std;
int main(void)
{
int x;
cout<<"Welche Zahl soll in Binär umgewandelt werden?"<<endl;
cin>>x;
int y[8];
if(x >= 0 && x <= 127)
{
for(int i = 7; i>=0; i--)
{
y[i] = x%2;
x=x/2;
}
for(int i = 0; i<=7; i++)
{
cout<<y[i];
}
cout<<endl<<"Einerkomplement: ";
for(int i = 7; i>=0; i--)
{
y[i] = 1 - y[i];
}
for(int i = 0; i<=7; i++)
{
cout<<y[i];
}
cout<<endl;
}
else
{
cout<<"Zahl ist nicht zwischen 0 und 127."<<endl<<"Das Programm wird beendet."<<endl;
}
return 0;
}
Code 0-9: Weitere Verbesserung: Schon im Array werden die Ziffern 1 und 0 in der richtigen Reihenfolge abgelegt.
#include <iostream>
using namespace std;
int main(void)
{
int x;
cout<<"Welche Zahl soll in Binär umgewandelt werden?"<<endl;
cin>>x;
int y[8];
if(x >= 0 && x <= 127)
{
for(int i = 0; i<=7; i++)
{
y[7-i] = x%2; // 7-i liefert: 7,6,5,4,3,2,1,0
x=x/2;
}
for(int i = 0; i<=7; i++)
{
cout<<y[i];
}
cout<<endl<<"Einerkomplement: ";
for(int i = 0; i<=7; i++)
{
y[i] = 1 - y[i];
}
for(int i = 0; i<=7; i++)
{
cout<<y[i];
}
cout<<endl;
}
else
{
cout<<"Zahl ist nicht zwischen 0 und 127."<<endl<<"Das Programm wird beendet."<<endl;
}
return 0;
}
Code 0-10: VARIANTE: y[7-i] = x%2;, statt die Schleife rückwärts laufen zu lassen.
MUSTERLÖSUNG gesamte Aufgabe:
#include <iostream>
using namespace std;
int main(void)
{
int x;
cout<<"Welche Zahl soll in Binär umgewandelt werden?"<<endl;
cin>>x;
int y[8];
if(x >= 0 && x <= 127)
{
for(int i = 0; i<=7; i++)
{
y[7-i] = x%2; // 7-i liefert: 7,6,5,4,3,2,1,0
x=x/2;
}
for(int i = 0; i<=7; i++)
{
cout<<y[i];
}
}
else if(x >= -128 && x <= -1)
{
x = -x; //Betrag nehmen
int z=0;
for(int i = 0; i<=7; i++)
{
y[7-i] = 1 - x%2; // Einerkomplement direkt
x=x/2;
}
for(int i = 0; i<=7; i++)
{
z=z*2;
z=z+y[i];
}
z++; //Zweierkomplement
for(int i = 0; i<=7; i++) //Muster des Zweierkomplements bilden.
{
y[7-i] = z%2;
z=z/2;
}
for(int i = 0; i<=7; i++)
{
cout<<y[i];
}
}
else
{
cout<<"Zahl ist nicht zwischen -128 und 127."<<endl<<"Das Programm wird beendet."<<endl;
}
return 0;
}
Code 0-11: ...auch negative Zahlen berücksichtigen.
5 Übung
|
|
Bild 0-1: Mit dia erstelltes Flussdiagramm.
Lösungen zu den Aufgaben der Übung
#include <iostream>
using namespace std;
int main(void)
{
double a[20];
for(int i=0;i<20;i++)
{
a[i]=(double)(rand()%100)/10.0;
cout<<a[i]<<" ";
}
cout<<endl;
for(int i=0;i<20;i++)
{
a[i] = a[i]*2.0;
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
Code 0-12: Studentische Lösung zu Aufgabe 1 (vergl. Moodle Uebung_29_10_2024).
Übung am 07.11.2024
|
1 Präsentation zu Algorithmen: Algorithmen, die aus mathematischen Formeln resultieren, Beispiel Mittelwert
|
$ m= \frac { \sum _i x_i}{N} $
Formel 0-1: Formel für Mittelwert.
#include <iostream>
#include <math.h>
using namespace std;
int main(void)
{
double x[] = {0.1,7.5,4.3,6.5,9.5};
int N=5;
double sum = 0.0;
for(int i=0;i<N;i++)
{
sum = sum + x[i];
}
sum = sum / (double)N; // mit Cast / Typumwandlung
cout<<"Mittelwert = "<<sum<<endl;
cout<<"Wurzel aus 2 = "<<sqrt(2.0)<<endl;
return 0;
}
Code 0-13: Mittelwertbildung als C-Programm.
$ \sum 1..n= \frac {n \cdot \left(n+1\right)}{2} $
Formel 0-2: Gausssche Summenformel.
2 Aufgabe 1: Streuung berechnen.
|
$ s= \sqrt \left( \frac { \sum _i \left(x_i-m\right)^2}{N}\right) $
Formel 0-3: Formel für Streuung s.
#include <iostream>
#include <math.h>
using namespace std;
int main(void)
{
double x[] = {0.1,7.5,4.3,6.5,9.5};
int N=5;
double sum = 0.0;
for(int i=0;i<N;i++)
{
sum = sum + x[i];
}
sum = sum / (double)N; // mit Cast / Typumwandlung
//------------------------------------------------------
double s = 0.0;
for(int i=0;i<N;i++)
{
s = s + (x[i]-sum)*(x[i]-sum);
}
s = sqrt(s/(double)N);
cout<<"Streuung s = "<<s<<endl;
return 0;
}
Code 0-14: Studentische Lösung zur Streuung.
3 Aufgabe 2: Gausssche Summenformel.
|
$ \sum 1..n= \frac {n \cdot \left(n+1\right)}{2} $
Formel 0-4: Gausssche Summenformel.
Code 0-15: Studentische Lösung zu Gauss.
4 Fertigstellen und besprechen der Aufgabe 2 von letzter Woche (Zweierpotenzen von 2^0 bis 2^7 berechnen und ausgeben)
Code 0-16: Studentische Lösung zu Aufgabe 2 (vergl. Moodle Uebung_29_10_2024).