You are on page 1of 7

// Endeiktiki Lysi tis Askisis 2.1 - Ak.

Etos 2020-2021

#include <stdio.h>

int main(void)
{
int a[10] = {0, 2, 4, 6, 8, 7, 6, 4, 2, 0};
int *pa = &a[1], *pb = &a[8], *pc;

printf("1. *(a + 6) = %d\n", *(a + 6));


printf("2. pb - pa = %ld\n", pb - pa);
printf("3. pb[1] = %d\n", pb[1]);
printf("4. &pb[1] = %p\n", &pb[1]);
printf("5. *pa += 3 = %d\n", *pa += 3);
printf("6. *(pb -= 3) = %d\n", *(pb -= 3));

/* a) Erwtima */
/*
* 1. --> 6, Pigainei sto 6o stoixeio tou pinaka a kai
ektipwnei to periexomeno
* 2. --> 7, Ektipwnei tin diafora twn dieuthinsewn twn
stoixeiwn a[8], a[1].
* Efoson prokeitai gia deiktes akeraiwn emfanizei
tin diafora tous
* se akeraious (oxi se bytes).
* 3. --> 0, To pb deixnei sto 8o stoixeio, ara to pb[1] tha
exei ta dedomena
* mias thesis parakatw, diladi tou 9ou stoixeiou
tou pinaka
* 4. --> 0x7fff73667984, einai i dieuthinsi tou 9ou
stoixeiou tou pinaka
* kapou sth mnhmh.
* 5. --> 5, To pa deixnei sto prwto stoixeio tou pinaka. Ara
edw exoume to
* stoixeio ayto auksimeno kata 3.
* 6. --> 7, To pb deixnei stin 8h thesi tou pinaka. To (pb -
3) deixnei stin
* 5h thesi tou pinaka. Ara edw exoume ta
periexomena tis 5hs thesis.
*/

/* b) Erwtima */
/* Anairw tis proigoumenes allages */
*pa -= 3;
pb += 3;

printf("4.1. pb[1] = %d\n", pb[1]); /* Apotelesma --> 0


*/
printf("5.1. *(pa += 3) = %d\n", *(pa += 3)); /* Apotelesma -
-> 8 */
printf("6.1. pb -= 3 = %p\n", pb -= 3); /* Apotelesma -->
0x7fff73667974 */

/* c) Erwtima */
pc = a; /* Isodynamo me pc = &a[0]; */
printf("pc=%p, *pc=%d\n", pc, *pc);

return 0;
}
// Endeiktiki Lysi tis Askisis 2.2 - Ak. Etos 2020-2021

#include <stdio.h>
#define N 10

int *search(int array[], int size, int num); /* prototype */

int main(void)
{
/* Arxikopoiw tous pinakes */
int A[N] = {0, 4, 8, 2, -2, 100, 45, -12, 45, 31};
int *res;

res = search(A, N, 100);


if(res != NULL)
{
printf("Number=%d, position(0-%d)=%ld\n", *res, N-1,
res-A);
}
else
{
printf("Number not found!\n");
}
return 0;
}

int *search(int array[], int size, int num)


{
int i;

for(i=0; i<size; i++)


{
if(array[i] == num)
{
return &(array[i]); /* Otan to vrw epistrefw
tin dieuthinsi tou */
}
}
return NULL;
}
// Endeiktiki Lysi tis Askisis 2.3 - Ak. Etos 2020-2021

#include <stdio.h>

#define N 10

/* Function Prototype */
double minmaxavg(double array[], int num, double *min, double *max);
Ως double μπορεί επιστρέψει μόνο μία τιμή, πχ το μέσο όρο. Αν επιστρέφει ο μ.ο. πως θα ενημερωθούντα min, max; Θα αλλάζουν οι τιμές με δείκτες.
Άρα, στα ορίσματα θα υπάρχουν δύο δείκτες.
int main(void)
{
double array[N] = {0.2, 4.6, 8.4, 2.5, -0.2, 100.2, 45.3, -
12.5, 45.77, 31.2};
double min, max, avg;

avg = minmaxavg(array, N, &min, &max);


printf("Average=%.2lf, Minimum=%.2lf, Maximum=%.2lf\n", avg,
min, max);
return 0;
}

double minmaxavg(double array[], int num, double *min, double *max)


{
/* Arxikopoiw tis metavlites moy me tin prwti thesi toy
pinaka */
int i;
double sum = array[0];

*min = array[0];
*max = array[0];

/* Psaxnw ton pinaka */


for (i=1; i<num; i++)
{
if (*min > array[i]) /* Gia tin elaxisti timi */
{
*min = array[i];
}
if (*max < array[i]) /* Gia tin megisti timi */
{
*max = array[i];
}
sum += array[i]; /* Vriskw kai to athroisma */
}

return sum/(double)num; /* Epistrefw ton meso oro */


}
// Endeiktiki Lysi tis Askisis 2.4 - Ak. Etos 2020-2021

#include <stdio.h>

/* H sinartisi epistrefei deikti ston


* teleytaio xaraktira tis simvoloseiras
*/
char *string_end(char *str)
{
if (*str == '\0')
return (str); Επιστρέφει κενό string (δείκτη σε char)

/* Pigainw sto telos tis simvoloseiras */


while (*(str+1) != '\0') Όσο το επόμενο δεν είναι το \0 ...
{
str++; ... αύξησε το δείκτη κατά μία θέση
}

return (str);
}

void string_reverse(char *str)


{
/* To p deixnei sto telos tis simvoloseiras */
char c, *p = string_end(str);

/* To str auksanetai, to p meionetai


* Otan to str ftasei to p tote i
* antistrofi exei oloklirothei
*/ Όσο δείκτης τέλους είναι μεγαλύτερος από το δείκτη αρχής - καλύπτει και την
for ( ; p > str; str++, p--) περίπτωση περιττού και την περίπτωση άρτιου μήκους συμβολοσειράς.
{
/* Antallazw tous xaraktires, Ανταλλάσσω τιμές ...
* O 1os me ton teleytaio,
* O 2os me ton proteleytaio ktl...
*/
c = *str;
*str = *p;
*p = c;
}
}

int main(void)
{
char s[100];

/* O xristis dinei tin simvoloseira */


printf("Enter string: ");
fgets(s, 99, stdin);

string_reverse(s);

/* Ektypwsi apotelesmatos */
printf("Reversed: %s\n", s);

return 0;
}
// Endeiktiki Lysi tis Askisis 2.5 - Ak. Etos 2020-2021

#include <stdio.h>

int main(void)
{
int x = 1; /* 4 bytes, all are 0 except least significant
(which is 1) */
char *ptr;

/* Make it point to where x is stored. It actually points to


the first of the
* 4 bytes occupied by x.
* Because &x is an address (pointer) of an int, we cast it
as char* so that
* the compiler does not complain (ptr should point to a
char). The address
* is not changed; it is just treated like an address of a
char.
*/
ptr = (char *) &x;

/* Now just check what this first byte contains ... */


printf("Detected format: %s-endian\n", (*ptr == 1) ? "Little"
: "Big");

return 0;
}
// Endeiktiki Lysi tis Askisis 2.6 - Ak. Etos 2020-2021

#include <stdio.h>

#define N 100 /* Maximum number of elements */

/* Function Prototypes */
void mergearrays(int *A, int na, int *B, int nb, int *C);
int readarray(int *arr);

int main(void)
{
int i, na, nb;
int A[N], B[N], C[2*N]; /* Give enough space */

na = readarray(A); /* Get them from the user */


nb = readarray(B);

mergearrays(A, na, B, nb, C);

for (i = 0; i < na+nb; i++)


printf("%d ", C[i]);
printf("\n");

return 0;
}

void mergearrays(int *A, int na, int *B, int nb, int *C)
{
int i, j;

/* At each iteration use the next element of either A or B


and advance C */
for (i = j = 0; i < na && j < nb; C++)
{
if (*A < *B) /* Select the smaller */
{
*C = *A; /* We could "shortcut" as
follows: */
A++; /* *C = *(A++);
*/
i++;
}
else
{
*C = *B; /* Same here */
B++;
j++;
}
}

/* If there are no more elements of B, copy remaining


elements of A */
if (i < na)
{
for ( /* empty */; i < na; i++)
*(C++) = *(A++); /* Two shortcuts! */
}
/* If there are no more elements of A, copy remaining
elements of B */
if (j < nb)
{
for ( /* empty */; j < nb; j++)
*(C++) = *(B++);
}
}

/* Read an array and return the number of elements */


int readarray(int *arr)
{
int i, num;

/* Make sure num < N */


do
{
printf("Enter number of elements of the array (at
most %d): ", N);
scanf("%d", &num);
}
while (num > N);

printf("Give the %d elements (sorted): ", num);


for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

return num;
}

You might also like