数组问题
定义一个double类型的二维数组data[12][5].用2.0~3.0的值初始化第一列元素(每步增加0.1)。如果行中的第一个元素值是x,该行的其他元素值分别是1/x,x的平方,x的3次方,x的4次方。输出数组中的值,每一行放在一行上,每一列要有标题。求解,最好能有注释,还有解题思路。
#include <stdio.h> #include <math.h> int main() { double i; printf("%10s%10s%10s%10s%10s\n", "i", "1 / i", "i ^ 2", "i ^ 3", "i ^ 4"); for (i = 2; i < 3.15; i += 0.1) { printf("%10.5lf%10.5lf%10.5lf%10.5lf%10.5lf\n", i, pow(i, -1), pow(i, 2), pow(i, 3), pow(i, 4)); } return 0; }
# include <stdio.h> int main(void) { double data[12][5]; double a; data[0][0] = 2.0; data[1][0] = 2.1; data[2][0] = 2.2; data[3][0] = 2.3; data[4][0] = 2.4; data[5][0] = 2.5; data[6][0] = 2.6; data[7][0] = 2.7; data[8][0] = 2.8; data[9][0] = 2.9; data[10][0] = 3.0; data[11][0] = 3.1; printf(" 1 2 3 4 5\n\n"); for(int i = 0; i < 12; i++) { for(int j = 0; j < 5; j++) { switch(j) { case 0: a = data[i][0]; break; case 1: a = 1.0 / data[i][0]; break; case 2: a = data[i][0] * data[i][0]; break; case 3: a = data[i][0] * data[i][0] * data[i][0]; break; case 4: a = data[i][0] * data[i][0] * data[i][0] * data[i][0]; break; } printf(" %lf", a); } printf("\n"); } return 0; }
#include <stdio.h> #include <math.h> int main() { double data[12][5] = {0}, a; int i, j; for (i = 0, a = 2; i < 12; i++, a += 0.1) { data[i][0] = a; data[i][1] = pow(a, -1); for (j = 2; j < 5; j++) { data[i][j] = pow(a, j); } } printf("%10s%10s%10s%10s%10s\n", "i", "1 / i", "i ^ 2", "i ^ 3", "i ^ 4"); for (i = 0; i < 12; i++) { for (j = 0; j < 5; j++) { printf("%10.5lf", data[i][j]); } printf("\n"); } return 0; }