added array/vektor example (missing file)
This commit is contained in:
parent
68a51e7d7e
commit
1b860723b8
39
array_test.c
Normal file
39
array_test.c
Normal file
@ -0,0 +1,39 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Vektor {
|
||||
int count;
|
||||
int *v;
|
||||
};
|
||||
typedef struct Vektor Vektor;
|
||||
|
||||
Vektor create_Vektor(int count) {
|
||||
Vektor self;
|
||||
self.count = count;
|
||||
self.v = (int*) calloc(self.count, sizeof(int));
|
||||
return self;
|
||||
}
|
||||
|
||||
void delete_Vektor(Vektor *self) {
|
||||
if(self->v) free(self->v);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
printf("Number of elements to sum: ");
|
||||
scanf(" %d", &n);
|
||||
Vektor data = create_Vektor(n);
|
||||
// printf("vektor.count:%d\n", data.count);
|
||||
|
||||
for(int i = 0; i < data.count; ++i) {
|
||||
scanf(" %d", &n);
|
||||
data.v[i] = n;
|
||||
}
|
||||
n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
|
||||
printf("Sum: %d\n", n);
|
||||
|
||||
// Need to not forget this
|
||||
delete_Vektor(&data);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user