#include <stdio.h>
#define ROWS 3
#define COLS 3
void assign(double A[][COLS], double nrows);
void print(double A[][COLS], double nrows);
int main(int argc, char* argv[]){
double A[ROWS][COLS];
assign(A, ROWS);
print(A, ROWS);
return 0;
}
void
assign(double A[][COLS], double nrows){
double mass, velocity_terminal, area;
while(scanf("%lf, %lf, %lf", &mass, &velocity_terminal, &area)==3){
int i;
for (i= 0; i<nrows; i++){
A[i][0]= mass;
A[i][1]= velocity_terminal;
A[i][2]= area;
}
}
}
void
print(double A[][COLS], double nrows){
int i, j;
for(i=0; i<nrows; i++){
for(j=0; j<nrows; j++){
printf("%5lf",A[i][j]);
}
printf("\n");
}
}
我很抱歉格式化。目标是将输入排列在二维数组中。我正在尝试读取提示中从文本文件传输的输入。所以一次带 3 个并将它们分配到 places 。上面的代码只将最后 3 个输入放入数组。
现在,当您接受值时,while 循环运行正常。但实际上你错过的是你在 While 中编写了 for 循环 这就是为什么每次它都会覆盖你的 data.means 而不是编写代码:
void assign(double A[][COLS], double nrows){
double mass, velocity_terminal, area;
while(scanf("%lf, %lf, %lf", &mass, &velocity_terminal, &area)==3){
int i;
for (i= 0; i<nrows; i++){
A[i][0]= mass;
A[i][1]= velocity_terminal;
A[i][2]= area;
}
}
}
你需要写:
void assign(double A[][COLS], double nrows){
double mass, velocity_terminal, area;
int i=0;
while(scanf("%lf, %lf, %lf", &mass, &velocity_terminal, &area)==3){
A[i][0]= mass;
A[i][1]= velocity_terminal;
A[i++][2]= area;
}
}
我是一名优秀的程序员,十分优秀!