#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMBERS 50
void kataxorisiArithmon(int *pinakas, int *megethos) {
printf("Eisagete mexri %d arithmous (eisagete -1 gia na termatisete tin kataxorisi) :\n", MAX_NUMBERS);
*megethos = 0;
int arithmos;
while (*megethos < MAX_NUMBERS && scanf("%d", &arithmos) && arithmos != -1) {
pinakas[(*megethos)++] = arithmos;
}
}
void evresiMegistonKaiElaxon(int *pinakas, int megethos) {
if (megethos == 0) {
printf("Den kataxorithe kanenas arithmos.\n");
return;
}
int megistos = pinakas[0], elaxistos = pinakas[0];
for (int i = 1; i < megethos; i++) {
if (pinakas[i] > megistos) megistos = pinakas[i];
if (pinakas[i] < elaxistos) elaxistos = pinakas[i];
}
printf("O megalyteros arithmos einai %d.\n", megistos);
printf("O mikroteros arithmos einai %d.\n", elaxistos);
}
float ypologismosMesouOrou(int *pinakas, int megethos) {
int athroisma = 0;
for (int i = 0; i < megethos; i++) athroisma += pinakas[i];
return megethos ? (float)athroisma / megethos : 0;
}
int sygkrisi(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
float ypologismosMesisTimis(int *pinakas, int megethos) {
if (megethos == 0) return 0;
qsort(pinakas, megethos, sizeof(int), sygkrisi);
return megethos % 2 == 0 ? (pinakas[megethos / 2 - 1] + pinakas[megethos / 2]) / 2.0 : pinakas[megethos / 2];
}
void dimiourgiaListasPollaplasion(int *pinakas, int megethos, int pollaplasio) {
if (megethos == 0) {
printf("Den kataxorithe kanenas arithmos.\n");
return;
}
printf("Pollaplasia tou %d : ", pollaplasio);
int vrethike = 0;
for (int i = 0; i < megethos; i++) {
if (pinakas[i] % pollaplasio == 0) {
printf("%d ", pinakas[i]);
vrethike = 1;
}
}
if (!vrethike) printf("Den yparchoun pollaplasia tou %d sti lista.\n", pollaplasio);
else printf("\n");
}
int main() {
int pinakas[MAX_NUMBERS], megethos = 0, pollaplasio;
char entoli;
printf("Kalos irthate ston diaxeiristi arithmon!\n");
while (1) {
printf("\nEpiloges:\n1. Kataxorisi arithmon\n2. Evresi tou megaliterou kai tou mikroterou arithmou\n3. Ypologismos mesou orou kai mesis timis\n4. Dimiourgia neas listas pollaplasion\n5. Exodos apo to programma\nEisagete tin epilogi sas: ");
scanf(" %c", &entoli);
if (entoli == '1') kataxorisiArithmon(pinakas, &megethos);
else if (entoli == '2') evresiMegistonKaiElaxon(pinakas, megethos);
else if (entoli == '3') {
printf("O mesos oros einai %.2f\n", ypologismosMesouOrou(pinakas, megethos));
printf("I mesi timi einai %.2f\n", ypologismosMesisTimis(pinakas, megethos));
}
else if (entoli == '4') {
printf("Eisagete ton arithmo gia na vreite ta pollaplasia tou: ");
scanf("%d", &pollaplasio);
dimiourgiaListasPollaplasion(pinakas, megethos, pollaplasio);
}
else if (entoli == '5') {
printf("Antio!\n");
break;
}
else {
printf("Agnosti entoli. Parakalo dokimaste xana.\n");
}
}
return 0;
}
Lab 8, Working With structs
Programmer: Maiar Khattab
Editor(s) used: Code Blocks 13.12
Compiler(s) used: Code Blocks 13.12
File: prog.cpp
Complied: Jan 8 2025 at 12:52:57
noon is 12:0:0
midnight is 0:0:0
supper is 6:0:0
bedtime is 11:30:0
�S�� ��(� is 1673624904:32766:-1393935616