Created
May 2, 2012 15:46
-
-
Save mochja/2577649 to your computer and use it in GitHub Desktop.
smerniky C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
void gen(int* a, int size) // tu miesto int* a, mozeme pouzit aj zapis int a[], tu je vidiet ze pole je vlastne pointer, size je velkost pola | |
{ | |
int i; | |
srand(time(NULL)); | |
for(i = 0; i < size; i++) | |
a[i] = (rand()%41)-20; // myslim ze je vsetko jasne | |
} | |
void prin(int* a, int size) | |
{ | |
int i; | |
for(i = 0; i < size; i++) | |
printf("%d\t", a[i]); | |
} | |
void prie(int* a, int size, int* x) | |
{ | |
int i; | |
for(i = 0; i < size; i++) | |
*x += a[i]; // zapisujeme hodnotu na adresu musime pouzivat | |
*x /= size; // *x = *x / size; | |
} | |
void gthannull(int *a, int size, int* x) | |
{ | |
int i; | |
for(i = 0; i < size; i++) | |
if (a[i] > 0) (*x)++; // ak chceme pouzivat -- alebo ++ na pointeri musime ho dat do zatvorky (*x)-- pretoze inak by sme zvacsovali len adresu smernika a nie jeho hodnotu | |
} | |
int main() | |
{ | |
int a[10]; | |
int priemer = 0; | |
int big = 0; | |
gen(a, 10); // naplnime pole a cislami <-20;20> predavame a nie &a, pretoze je to pole mozeme dedukovat ze *a je to iste co a[] | |
prin(a, 10); // vypiseme pole, pole je pointer netreba predavat adresu pretoze to je adresa | |
prie(a, 10, &priemer); // opat priemerujeme pole, pozor premenna priemer je uz normalna premenna a preto musime predavat adresu (&) | |
gthannull(a, 10, &big); // cisla vacsie ako 0 to iste co v predchadzajucom | |
// vypis klas. premennych | |
printf("%d\n", priemer); | |
printf("%d\n", big); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment