tanszek:oktatas:szamitastechnika:strukturak
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tanszek:oktatas:szamitastechnika:strukturak [2025/10/29 08:02] – [Gyakorlás] szabom | tanszek:oktatas:szamitastechnika:strukturak [2025/11/18 18:26] (current) – knehez | ||
|---|---|---|---|
| Line 76: | Line 76: | ||
| </ | </ | ||
| - | Mutatóval (hatékonyabb): | + | Mutatóval (másképpen): |
| <sxh c> | <sxh c> | ||
| void kiir(const Szemely *s) { | void kiir(const Szemely *s) { | ||
| Line 99: | Line 99: | ||
| </ | </ | ||
| + | **Struktúra adatainak bekérése és kiírása: | ||
| <sxh c> | <sxh c> | ||
| + | #include < | ||
| + | typedef struct{ | ||
| + | char nev[30]; | ||
| + | int kor; | ||
| + | double magassag; | ||
| + | }EMBER; | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | EMBER e; | ||
| + | int osszeg = 0; | ||
| + | double atlag = 0; | ||
| + | double max = 0; | ||
| + | | ||
| + | for(int i = 0; i < 3; i++){ | ||
| + | printf(" | ||
| + | fgets(e.nev, | ||
| + | printf(" | ||
| + | scanf(" | ||
| + | printf(" | ||
| + | scanf(" | ||
| + | osszeg += e.kor; | ||
| + | if(max < e.magassag){ | ||
| + | max = e.magassag; | ||
| + | } | ||
| + | } | ||
| + | atlag = (double)osszeg / 3; | ||
| + | printf(" | ||
| + | printf(" | ||
| + | } | ||
| </ | </ | ||
| + | |||
| + | ---- | ||
| + | |||
| + | <sxh c> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | struct DIAK{ | ||
| + | int id; | ||
| + | char nev[50]; | ||
| + | double atlag; | ||
| + | }; | ||
| + | |||
| + | int main() | ||
| + | { | ||
| + | | ||
| + | d1.id = 1; | ||
| + | | ||
| + | | ||
| + | |||
| + | //d1 = {0," | ||
| + | |||
| + | | ||
| + | {2, "Jkl Éva", 4.4}, | ||
| + | {3, " | ||
| + | }; | ||
| + | |||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | printf(" | ||
| + | printf(" | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| ===== Gyakorlás ===== | ===== Gyakorlás ===== | ||
| Line 217: | Line 288: | ||
| </ | </ | ||
| + | 6. Készíts programot, ami 5 diák 3 tantárgyból kapott jegyeit tárolja és minden diáknak kiszámolja az átlagát! | ||
| + | <sxh c> | ||
| + | #include < | ||
| + | #define N 5 | ||
| + | #define M 3 | ||
| + | typedef struct { | ||
| + | char nev[30]; | ||
| + | int jegyek[M]; | ||
| + | float atlag; | ||
| + | } Diak; | ||
| + | |||
| + | int main() { | ||
| + | Diak diakok[N] = { | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | }; | ||
| + | |||
| + | for (int i = 0; i < N; i++) { | ||
| + | int osszeg = 0; | ||
| + | for (int j = 0; j < M; j++) | ||
| + | osszeg += diakok[i].jegyek[j]; | ||
| + | diakok[i].atlag = (float)osszeg / M; | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | for (int i = 0; i < N; i++) { | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 7. Definiálj egy Matrix struktúrát, | ||
| + | <sxh c> | ||
| + | #include < | ||
| + | |||
| + | #define SIZE 3 | ||
| + | |||
| + | typedef struct { | ||
| + | int adat[SIZE][SIZE]; | ||
| + | } Matrix; | ||
| + | |||
| + | Matrix transzponalt(Matrix m) { | ||
| + | Matrix t; | ||
| + | for (int i = 0; i < SIZE; i++) | ||
| + | for (int j = 0; j < SIZE; j++) | ||
| + | t.adat[i][j] = m.adat[j][i]; | ||
| + | return t; | ||
| + | } | ||
| + | |||
| + | void kiir(Matrix m) { | ||
| + | for (int i = 0; i < SIZE; i++) { | ||
| + | for (int j = 0; j < SIZE; j++) | ||
| + | printf(" | ||
| + | printf(" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | Matrix A = {{{1,2,3}, {4,5,6}, {7,8,9}}}; | ||
| + | printf(" | ||
| + | kiir(A); | ||
| + | printf(" | ||
| + | Matrix T = transzponalt(A); | ||
| + | kiir(T); | ||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 8. Tárolj 5 darab 2D pontot egy struktúra tömbben, és határozd meg, melyik két pont van egymástól legmesszebb! | ||
| + | <sxh c> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define N 5 | ||
| + | |||
| + | typedef struct { | ||
| + | float x, y; | ||
| + | } Pont; | ||
| + | |||
| + | float tav(Pont a, Pont b) { | ||
| + | return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y)); | ||
| + | } | ||
| + | |||
| + | int main() { | ||
| + | Pont pontok[N] = { | ||
| + | {0, 0}, {3, 4}, {5, 1}, {2, 8}, {9, 9} | ||
| + | }; | ||
| + | |||
| + | float maxTav = 0; | ||
| + | int p1 = 0, p2 = 1; | ||
| + | |||
| + | for (int i = 0; i < N; i++) { | ||
| + | for (int j = i + 1; j < N; j++) { | ||
| + | float d = tav(pontok[i], | ||
| + | if (d > maxTav) { | ||
| + | maxTav = d; | ||
| + | p1 = i; | ||
| + | p2 = j; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | printf(" | ||
| + | | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | 9. Tölts fel 4 diák adatait 4 jeggyel (mátrix formában), majd minden diákhoz írd ki a legjobb és legrosszabb jegyet! | ||
| + | <sxh c> | ||
| + | #include < | ||
| + | |||
| + | #define N 4 | ||
| + | #define M 4 | ||
| + | |||
| + | typedef struct { | ||
| + | char nev[30]; | ||
| + | int jegyek[M]; | ||
| + | } Diak; | ||
| + | |||
| + | int main() { | ||
| + | Diak diakok[N] = { | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | {" | ||
| + | }; | ||
| + | |||
| + | for (int i = 0; i < N; i++) { | ||
| + | int min = diakok[i].jegyek[0]; | ||
| + | int max = diakok[i].jegyek[0]; | ||
| + | for (int j = 1; j < M; j++) { | ||
| + | if (diakok[i].jegyek[j] < min) min = diakok[i].jegyek[j]; | ||
| + | if (diakok[i].jegyek[j] > max) max = diakok[i].jegyek[j]; | ||
| + | } | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 10. Készíts programot, ami két 3×3-as mátrixot ad össze és kiírja az eredményt. | ||
| + | <sxh c> | ||
| + | #include < | ||
| + | |||
| + | #define N 3 | ||
| + | |||
| + | typedef struct { | ||
| + | int adat[N][N]; | ||
| + | } Matrix; | ||
| + | |||
| + | int main() { | ||
| + | Matrix A = {{{1, | ||
| + | Matrix B = {{{9, | ||
| + | Matrix C; // eredmény mátrix | ||
| + | |||
| + | // Mátrixok összeadása | ||
| + | for (int i = 0; i < N; i++) { | ||
| + | for (int j = 0; j < N; j++) { | ||
| + | C.adat[i][j] = A.adat[i][j] + B.adat[i][j]; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Eredmény kiírása | ||
| + | printf(" | ||
| + | for (int i = 0; i < N; i++) { | ||
| + | for (int j = 0; j < N; j++) { | ||
| + | printf(" | ||
| + | } | ||
| + | printf(" | ||
| + | } | ||
| + | |||
| + | return 0; | ||
| + | } | ||
| + | |||
| + | </ | ||
tanszek/oktatas/szamitastechnika/strukturak.1761724934.txt.gz · Last modified: 2025/10/29 08:02 by szabom
