basic data types in c“λώσσα c... · array of instances of structs assignment member of an...
TRANSCRIPT
![Page 1: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/1.jpg)
Basic Data types in C Τι πρέπει να ξέρετε ήδη
και αν όχι, χρειάζεται να το μάθετε
• int
• char
• float
• double
![Page 2: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/2.jpg)
Variable declaration in C:
int x, y, z; Example:
Variable assignment:
x = 35; Example:
type variables / instances
value
![Page 3: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/3.jpg)
Boolean
•Has two values, true and false.
•In C we use integers as Booleans.
•0 represents false.
•Any !=0 integer represents true.
•Έχετε δοκιμάσει x==-1;
![Page 4: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/4.jpg)
Arrays (1D)
•All the elements are of the same type.
•An element: array1D[index]
•In C, the first element has index 0 (zero).
array1D:
0 1 N - 1
![Page 5: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/5.jpg)
Strings : array of characters
char name[30]; Example:
Unlike other arrays,
strings require an end-of-string character : ‘\0’
max length
including ‘\0’
![Page 6: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/6.jpg)
Multidimensional Arrays
•All the elements are of the same type.
•An element: array2D[row][column]
array2D:
![Page 7: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/7.jpg)
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Example 1:
![Page 8: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/8.jpg)
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
File inclusion header
![Page 9: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/9.jpg)
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Function definition
![Page 10: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/10.jpg)
Function name #include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
![Page 11: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/11.jpg)
Must be compatible with the function’s return type
Function return type
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
![Page 12: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/12.jpg)
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Parameter type
Function parameter
![Page 13: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/13.jpg)
#include <stdio.h>
int leapYear(int year)
{
if ((year % 4 == 0 && year % 100 != 0))
|| (year % 400 == 0) )
{
return 1;
}
else
{
return 0;
}
}
Example 1
![Page 14: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/14.jpg)
int dayTable[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int dayOfYear(int year, int month, int day)
{
int i;
int isLeap = leapYear(year);
for (i = 1; i < month; i++) {
day += dayTable[isLeap][i];
}
return day;
}
Example 2
![Page 15: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/15.jpg)
Global variable
Local variable
int dayTable[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int dayOfYear(int year, int month, int day)
{
int i;
int isLeap = leapYear(year);
for (i = 1; i < month; i++) {
day += dayTable[isLeap][i];
}
return day;
}
![Page 16: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/16.jpg)
2-dimensional array of int
int dayTable[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int dayOfYear(int year, int month, int day)
{
int i;
int isLeap = leapYear(year);
for (i = 1; i < month; i++) {
day += dayTable[isLeap][i];
}
return day;
}
![Page 17: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/17.jpg)
int dayTable[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int dayOfYear(int year, int month, int day)
{
int i;
int isLeap = leapYear(year);
for (i = 1; i < month; i++) {
day += dayTable[isLeap][i];
}
return day;
}
![Page 18: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/18.jpg)
int dayTable[2][13] = {
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
int dayOfYear(int year, int month, int day)
{
int i;
int isLeap = leapYear(year);
for (i = 1; i < month; i++) {
day += dayTable[isLeap][i];
}
return day;
}
Example 2
![Page 19: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/19.jpg)
main()
{
int year, month, day;
printf(“Enter year, month and day: ”);
scanf(“%d %d %d”, &year, &month, &day);
day = dayOfYear(year, month, day);
printf(“\nDay of Year = %d\n”, day);
}
Example 2 (main)
![Page 20: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/20.jpg)
main()
{
int year, month, day;
printf(“Enter year, month and day: ”);
scanf(“%d %d %d”, &year, &month, &day);
day = dayOfYear(year, month, day);
printf(“\nDay of Year = %d\n”, day);
}
Function call
![Page 21: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/21.jpg)
Struct (δομή)
•Members may have different types.
structure.member1
•structs are also known as “records” (εγγραφές) and members as
“fields”(πεδία)
Structure Name
member3
member4
member2
member1 (μέλος)
![Page 22: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/22.jpg)
typedef
•Gives a new name to a type that has already
been defined.
•E.g.
typedef int Boolean;
•Saves typing struct SomeRec everywhere.
![Page 23: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/23.jpg)
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
Example 3:
![Page 24: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/24.jpg)
Macro substitution
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
![Page 25: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/25.jpg)
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
Structure declaration
![Page 26: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/26.jpg)
Structure name / tag
Members
Don’t forget this!
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
![Page 27: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/27.jpg)
Data type
New type name
#include <stdio.h>
#define MAXNAME 80
struct StudentRec
{
char name[MAXNAME];
int mark;
};
typedef struct StudentRec Student;
![Page 28: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/28.jpg)
Student readStudent(void)
{ Student next;
scanf(“%s %d”, next.name, &next.mark);
return next;
}
/* next.name without & */
void printStudent(const Student student)
{
printf(“%s %d\n”, student.name, student.mark);
}
![Page 29: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/29.jpg)
An instance of the struct
A member of a struct variable “Package”
Student readStudent(void)
{ Student next;
scanf(“%s %d”, next.name, &next.mark);
return next;
}
void printStudent(const Student student)
{
printf(“%s %d\n”, student.name, student.mark);
}
![Page 30: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/30.jpg)
#define MAXCLASS 100
main()
{
Student class[MAXCLASS];
int n, i, best = 0;
printf(“Enter number of students: ”);
scanf(“%d”, &n);
for (i = 0; i < n; i++) {
class[i] = readStudent();
if (class[best].mark < class[i].mark) {
best = i;
}
}
printf(“Best student is: ”);
printStudent(class[best]);
}
![Page 31: Basic Data types in C“λώσσα C... · Array of instances of structs Assignment Member of an array element #define MAXCLASS 100 main() { Student class[MAXCLASS]; int n, i, best](https://reader036.vdocuments.mx/reader036/viewer/2022062603/5f09e4dc7e708231d42902a3/html5/thumbnails/31.jpg)
Array of instances of structs
Assignment
Member of an array element
#define MAXCLASS 100
main()
{
Student class[MAXCLASS];
int n, i, best = 0;
printf(“Enter number of students: ”);
scanf(“%d”, &n);
for (i = 0; i < n; i++) {
class[i] = readStudent();
if (class[best].mark < class[i].mark) {
best = i;
}
}
printf(“Best student is: ”);
printStudent(class[best]);
}