20191209ポインタ課題(解答例)

参考

課題

実数型配列に代入した値のうち25.0以上を表示させる関数を作成せよ。

解答例1

#include <stdio.h>

void check(double *x)
{
	int i;
	
	for(i=0; i<5; i++){
		if( *(x+i) >= 25.0){
			printf("%.1f\n", *(x+i) );
		}
	}
}

int main(void){
    double n[] = {10.5, 28.7, 4.8, 120.9, 20.8};
 
    check(n);
 
    return 0;
}

実行結果

28.7
120.9

解答例2

#include <stdio.h>

void check(double *x)
{
	int i;
	
	for(i=0; i<5; i++){
		if( x[i] >= 25.0){
			printf("%.1f\n", x[i] );
		}
	}
}

int main(void){
    double n[] = {10.5, 28.7, 4.8, 120.9, 20.8};
 
    check(n);
 
    return 0;
}

コメント