gpt4 book ai didi

c - 如何一次扫描 3 个输入,处理它们,然后继续扫描 C 中的其余输入

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:57 25 4
gpt4 key购买 nike

#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;

}
}

关于c - 如何一次扫描 3 个输入,处理它们,然后继续扫描 C 中的其余输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com