Tutorium #2

Einführung in die (strukturierte) Programmierung

Gruppe 3 – Michael Schwarz prog-tutor-michael@iicm.tugraz.at
Gruppe 5 – Jörg Müller prog-tutor-joerg@iicm.tugraz.at
Gruppe 6 – Christoph Hack prog-tutor-hack@iicm.tugraz.at
Gruppe 8 – Anja Karl prog-tutor-anja@iicm.tugraz.at
Gruppe 9 – Manuel Weber prog-tutor-weber@iicm.tugraz.at

Tutoriumsinhalt

Codingstandard 1

[...]
    if(a>=18) { 
      if(wd != 0){if( 100<=m) {
e = 1;printf("Herzlich willkommen im Casino!\n");
      } else { printf("Zugang verweigert!\n");}
  }else { puts("Zugang verweigert!");}
  } else { printf("Zugang verweigert!\n");}
[...]

Coding Standard

Einheitliche Formatierung des Programms um die Zusammenarbeit zu vereinfachen.


siehe https://palme.iicm.tugraz.at/wiki/ESP/CS

Codingstandard 2 - Besser so!

if (age >= 18 && well_dressed && money >= 100)
{
  entry = TRUE;
  printf("Herzlich willkommen im Casino!\n");
}
else
{
  printf("Zugang verweigert!\n");
}
      

HW1 Besprechung

Gut oder schlecht?

Gut oder schlecht?

Gut oder schlecht?

Gut oder schlecht?

Variablen

Eine Variable ist eine Speicherstellen im Arbeitsspeicher, die einen Wert eines bestimmten Typs speichern kann.

Variablen

1int main() { 2 int apples = 0; 3 apples = 5; 4 printf("I have %d apples.\n", apples); 5 return 0; }
0x1000
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x10000
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x10005
0x1004
0x1008
0x100C
0x1010
0x1014

I have 5 apples.

apples 0x10005
0x1004
0x1008
0x100C
0x1010
0x1014

Operatoren

1int main() { 2 int apples = 15; 3 apples = apples + 5; 4 apples = apples - 5; 5 int persons = 5; 6 int apple_per_person = apples/persons; 7 int more_persons = persons * 2; 8 return 0; }
0x1000
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x100015
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x100020
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x100015
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x100015
persons 0x10045
0x1008
0x100C
0x1010
0x1014
apples 0x100015
persons 0x10045
apple_per_person 0x10083
0x100C
0x1010
0x1014
apples 0x100015
persons 0x10045
apple_per_person 0x10083
more_persons 0x100C10
0x1010
0x1014
apples 0x100015
persons 0x10045
apple_per_person 0x10083
more_persons 0x100C10
0x1010
0x1014

Operatoren - Achtung!

1int main() { 2 int apples = 2; 3 int persons = 4; 4 int apple_per_person = apples/persons; 5 return 0; }
0x1000
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x10002
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x10002
persons 0x10044
0x1008
0x100C
0x1010
0x1014
apples 0x10002
persons 0x10044
apple_per_person 0x10080
0x100C
0x1010
0x1014
apples 0x10002
persons 0x10044
apple_per_person 0x10080
0x100C
0x1010
0x1014

Gleitkommazahlen

1int main() { 2 double apples = 2; 3 double persons = 4; 4 double apple_per_person = apples/persons; 5 return 0; }
0x1000
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x10002.0
0x1004
0x1008
0x100C
0x1010
0x1014
apples 0x10002.0
persons 0x10044.0
0x1008
0x100C
0x1010
0x1014
apples 0x10002.0
persons 0x10044.0
apple_per_person 0x10080.5
0x100C
0x1010
0x1014
apples 0x10002.0
persons 0x10044.0
apple_per_person 0x10080.5
0x100C
0x1010
0x1014

Übersicht über die Datentypen

Ganzzahlen
  char
  short
  int
  long
Dezimalzahlen
  float
  double

Normale Rechenoperationen:

a + b;Summe
a - b;Differenz
a * b;Produkt
a / b;Division
a % b;Modulo

Pre- & Post-Increment

++i;
i++;

Pre- & Post-Decrement

--i;
i--;

Zuweisung + Rechenoperation-Kombiniert

i += 3;
i -= 2;
i *= 4;
i /= 5;

Abfragen

1int main() { 2 int age = 17; 3 if(age >= 18) { printf("Willkommen im Casino!"); } 4 else { 5 printf("Zutritt verweigert!"); } 6 return 0; }
0x1000
0x1004
0x1008
0x100C
0x1010
0x1014
age   0x100017
0x1004
0x1008
0x100C
0x1010
0x1014
age   0x100017
0x1004
0x1008
0x100C
0x1010
0x1014
age   0x100017
0x1004
0x1008
0x100C
0x1010
0x1014

Zutritt verweigert!

age   0x100017
0x1004
0x1008
0x100C
0x1010
0x1014

Schleifen

For-Schleife

/* For-Loop example */
#include <stdio.h>

int main()
{
  int number_of_students = 20;
  int student_id = 0;
  
  //Init, Condition
  for (student_id = 1; student_id < number_of_students; student_id++) 
  {
    printf("Student-ID: %d\n", student_id);
    //Step
  }
  return 0;
}

While-Schleife

/* While-Loop example */
#include <stdio.h>

int main()
{
  int number_of_students = 20;
  
  int student_id = 1; //Init
  while(student_id < number_of_students) //Condition
  {
    printf("Student-ID: %d\n", student_id);
    student_id++; //Step
  }
  return 0;
}

While-Schleife

/* While-Loop example 2 */
#include <stdio.h>

int main()
{
  int max_value = 100;
  int current_value = 1;
  int add_value = 2;
  int iterations = 0; //Init
  while(current_value < max_value) //Condition
  {
    current_value += add_value;
    iterations++; //Step
  }
  printf("Iterations: %d\n", iterations);
  return 0;
}

Abgabe

Viel Erfolg bei HW2