initial import
[staff/goc4/2018bti7061.git] / c-4-pointers / examples / c-pointers / dynamic1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4
5 double avrge(double* marks, int size);
6
7 main(){
8 int i, n;
9 double* marks;
10 /* read in a value for n */
11
12 puts("Number of marks:");
13 scanf(" %d", &n);
14
15 if(n>0){
16
17 marks = (double *) malloc(n*sizeof(double));
18
19 for(i=0;i<n;i++){
20 printf("%d) Enter Mark:",(i+1));
21 // equivalent to scanf(" %lf",&marks[i]);
22 scanf(" %lf",marks+i);
23
24 }
25 double average = avrge(marks,n);
26 printf("Average is:%lf\n",average);
27 return 0;
28 }
29 printf("Need at least one number to make a mean!\n");
30 }
31
32
33 double avrge(double* marks, int size){
34 double sum=0;
35 int i;
36 for(i=0;i<size;i++){
37 sum += *(marks+i); // equivalent to marks[i]
38 }
39 return sum/size;
40
41 }