[圖解C] Example 811: call by address

// Example 811: call by address of 1D array

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// using sqrt() which in <math.h>

void adjust(float *, float *, int);


int main()
{
    float *grade1, *grade2;
    int num, i;
 
    printf("the number of students: ");
    scanf("%d", &num);
 
    // dynamic allocation
    grade1 = (float *)malloc(num*sizeof(float));
    grade2 = (float *)malloc(num*sizeof(float));
 
    for (i=0 ; i<num ; i++){
        printf("%dth student's grade: ", i+1);
        scanf("%f", &grade1[i]);
    }
 
    adjust(grade1, grade2, num);
 
    printf("\n==========================\n");
 
    for (i=0 ; i<num ; i++){
        printf("%dth \t %.2f \t %.2f\n", i+1, grade1[i], grade2[i]);
    }
 
    system("pause");
    return 0;
}

// edit the original data -> don't need to return any data
void adjust(float *g1, float *g2, int num)
{
     int i;
     for (i=0 ; i<num ; i++){
         g2[i] = 10*sqrt(g1[i]);
     }
}