如何计算C中矩阵行中的元素数量
使用整数数组,我想首先向用户询问他想要在数组中包含的行和列的数量(让我们称它们为x和y)(我知道如何执行此步骤)。重要的是,当用户输入将存储在矩阵中的值时,将从一行输入读取一行,从第二行输入读取第二行,依此类推。所以一行输入=1行 因此,如果他定义了x个列的数量,他应该在一行中为第一行输入x个数字。如何检查是否已在线路上输入x号?如果输入更少或更多,我将打印一条错误消息。是否有某种命令可以检查1行的大小,以便它可以对照用户定义的x进行检查 到目前为止,我编写的代码涉及到一些简单的步骤,但我对如何实现这个检查几乎没有什么想法,以确认用户输入的输入量与他最初定义的输入量相同如何计算C中矩阵行中的元素数量,c,arrays,matrix,input,limit,C,Arrays,Matrix,Input,Limit,使用整数数组,我想首先向用户询问他想要在数组中包含的行和列的数量(让我们称它们为x和y)(我知道如何执行此步骤)。重要的是,当用户输入将存储在矩阵中的值时,将从一行输入读取一行,从第二行输入读取第二行,依此类推。所以一行输入=1行 因此,如果他定义了x个列的数量,他应该在一行中为第一行输入x个数字。如何检查是否已在线路上输入x号?如果输入更少或更多,我将打印一条错误消息。是否有某种命令可以检查1行的大小,以便它可以对照用户定义的x进行检查 到目前为止,我编写的代码涉及到一些简单的步骤,但我对如何
非常感谢 因为您希望一次读取一行,所以应该使用
fgets
,它正好可以这样做。然后,您可以使用strtok
按空格/制表符分隔行,并尝试将每个值转换为整数。如果在填充行之前,数字已用完,或者在填充行之后,仍然有更多的数字,则可以提醒用户这一点
下面是一个简单的例子:
void read_array(int **array, int rows, int cols)
{
char line[100];
int i,count;
char *p;
for (i=0;i<rows;i++) {
printf("enter row %d: "), i-1);
fgets(line, sizeof(line), stdin);
count = 0;
p = strtok(line, " \t");
while (p && (count < cols)) {
matrix[i][count++] = atoi(p);
p = strtok(NULL, " \t");
}
if (count < cols) {
printf("too few values\n");
i--;
continue;
} else if (p) {
printf("too many values\n");
i--;
continue;
}
}
}
void read\u数组(int**array,int rows,int cols)
{
字符行[100];
int i,计数;
char*p;
对于(i=0;i这里有另一个解决方案,尽管对于您的具体需求,另一个答案比我的好
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int number_of_rows;
int number_of_columns;
printf("Enter the number of rows: ");
scanf("%d", &number_of_rows);
printf("Ok, enter the number of columns: ");
scanf("%d", &number_of_columns);
int matrix[number_of_rows][number_of_columns];
int i;
int j;
for(i = 0; i < number_of_rows; ++i) {
printf("This is %d row.\n", i+1);
for(j = 0; j < number_of_columns; ++j) {
scanf("%d", &matrix[i][j]);
}
}
/* Print's the 2D array. */
for(i = 0; i < number_of_rows; ++i) {
printf("\n");
for(j = 0; j < number_of_columns; ++j) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
#包括
#包括
内部主(空)
{
整数行数;
整列的整数;
printf(“输入行数:”);
scanf(“%d”和行数);
printf(“确定,输入列数:”);
scanf(“%d”和列数);
int矩阵[行数][列数];
int i;
int j;
对于(i=0;i<行数;++i){
printf(“这是%d行。\n”,i+1);
对于(j=0;j<列数;++j){
scanf(“%d”和矩阵[i][j]);
}
}
/*打印的是2D数组*/
对于(i=0;i<行数;++i){
printf(“\n”);
对于(j=0;j<列数;++j){
printf(“%d”,矩阵[i][j]);
}
printf(“\n”);
}
返回0;
}
这样做的目的是询问用户想要矩阵的行数和列数(2D数组),然后根据这些数字声明一个2D数组。然后它循环遍历每一行,在循环遍历每一列之前将行数打印到控制台。初始化2D数组后,它以绘制矩阵的方式打印2D数组。我希望这会有所帮助。这使用一个函数来获取一个范围内的整数d另一个用于获取一系列整数。strtol
用于解析使用fgets
获得的输入值
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
//inputs
// char *line : pointer to text to be parsed
// char **next : pointer to pointer to allow modification of caller's pointer
// char *term : pointer to characters to be considered terminators
// int *value : pointer to int to allow modification of caller's int
// int min : minimum value of range
// int max : maximum value of range
// returns : 0 failure or 1 success
int get_int_range ( char *line, char **next, char *term, int *value, int min, int max)
{
long int input = 0;
char *end = NULL;//will point to end of parsed value
errno = 0;
input = strtol ( line, &end, 10);//get the integer from the line. end will point to the end of the parsed value
if ( end == line) {// nothing was parsed. no digits
printf ( "input MUST be a number\n");
return 0;// return failure
}
// *end is the character that end points to
if ( *end != '\0' && ( strchr ( term, *end) == NULL)) {// is *end '\0' or is *end in the set of term characters
printf ( "problem with input: [%s] \n", line);
return 0;
}
if ( ( errno == ERANGE && ( input == LONG_MAX || input == LONG_MIN))
|| ( errno != 0 && input == 0)){// parsing error from strtol
perror ( "input");
return 0;
}
if ( input < min || input > max) {// parsed value is outside of range
printf ( "input out of range %d to %d\n", min, max);
return 0;
}
if ( next != NULL) {// if next is NULL, caller did not want pointer to end of parsed value
*next = end;// *next allows modification to caller's pointer
}
*value = input;// *value allows modification to callers int
return 1;// success
}
//inputs
// int cols : needed to pass in the variable length array
// int dest[][cols] : variable length array to be modified with parsed values
// int inputrow : row of the variable length array to be modified
// int min : mininum value of range
// int max : maximum value of range
// char *line : pointer to text to be parsed
// char *term : pointer to characters to be considered terminators
// returns : 0 failure or 1 success
int get_int_series ( int cols, int dest[][cols], int inputrow, int min, int max, char *line, char *term)
{
char *end = NULL;// will point to end of parsed value
char *each = NULL;// will point to start of parsed value
int valid = 0;
int input = 0;
int count = 0;
int temp[cols];// temporary storage for parsed values
each = line;// each points to start of line
do {
valid = get_int_range ( each, &end, term, &input, INT_MIN, INT_MAX);// call to parse one value
if ( !valid) {// failure
printf ( "input MUST be a number\n");
return 0;
}
if ( valid) {// success
temp[count] = input;// save parsed value in temporary array
count++;
if ( count > cols) {
printf ( "too many integers. %d entered. only enter %d\n", count, cols);
return 0;
}
}
// *end is the character that end points to
while ( *end && strchr ( term, *end)) {// if *end not '\0' and *end is in the set of term characters
end++;// yes. advance end one position
}
each = end;// set the end pointer to be the new starting pointer
} while ( end && *end);// keep looping the above while end is not NULL and *end is not '\0'
if ( count < cols) {
printf ( "too few integers. need %d entered. only entered %d\n", cols, count);
return 0;
}
while ( count) {
count--;
dest[inputrow][count] = temp[count];// set the values in the callers array to the values in the temporary array
}
return 1;// success
}
int main( int argc, char *argv[])
{
char line[900] = {'\0'};
int valid = 0;
int rows = 0;
int cols = 0;
int eachrow = 0;
int eachcol = 0;
do {
printf ( "Enter number of array rows or quit\n");
fgets ( line, sizeof ( line), stdin);//read a line
if ( strcmp ( line, "quit\n") == 0) {
return 1;// if quit is entered, exit the program
}
valid = get_int_range ( line, NULL, "\n", &rows, 0, INT_MAX);// call to parse a value
} while ( !valid);// on failure, keep looping the above
do {
printf ( "Enter number of array columns or quit\n");
fgets ( line, sizeof ( line), stdin);//read a line
if ( strcmp ( line, "quit\n") == 0) {
return 1;
}
valid = get_int_range ( line, NULL, "\n", &cols, 0, INT_MAX);
} while ( !valid);
//once the size is obtained, the variable length array can be declared
int array[rows][cols];
for(eachrow = 0; eachrow < rows; eachrow++) {// loop through the rows
do {
printf ( "Enter %d integers for row %d or quit. Ex 1 2 3\n", cols, eachrow + 1);
fgets ( line, sizeof ( line), stdin);//read a line
if ( strcmp ( line, "quit\n") == 0) {
return 1;
}
valid = get_int_series ( cols, array, eachrow, INT_MIN, INT_MAX, line, " \n");// call to obtain a series of values
} while ( !valid);// on failure, keep looping the above
}
// show the values in the variable length array
for(eachrow = 0; eachrow < rows; eachrow++) {
for(eachcol = 0; eachcol < cols; eachcol++) {
printf("[%d] ", array[eachrow][eachcol]);
}
printf("\n");
}
printf("\nDone\n");
return 0;
}
#包括
#包括
#包括
#包括
#包括
//投入
//char*line:指向要分析的文本的指针
//char**next:指向指针的指针,允许修改调用者的指针
//char*term:指向被视为终止符的字符的指针
//int*value:指向int的指针,允许修改调用方的int
//int min:范围的最小值
//int max:范围的最大值
//返回:0失败或1成功
int get_int_范围(字符*行,字符**next,字符*项,整数*值,整数最小值,整数最大值)
{
长整数输入=0;
char*end=NULL;//将指向解析值的末尾
errno=0;
input=strtol(line,&end,10);//从该行获取整数。end将指向解析值的末尾
如果(end==line){//未分析任何内容。无数字
printf(“输入必须是一个数字\n”);
返回0;//返回失败
}
//*结束是结束指向的字符
如果(*end!='\0'&&(strchr(term,*end)==NULL)){//is*end'\0'或is*end在术语字符集中
printf(“输入问题:[%s]\n”,第行);
返回0;
}
if((errno==ERANGE&&(input==LONG_MAX | | input==LONG_MIN))
||(errno!=0&&input==0)){//strtol解析错误
perror(“输入”);
返回0;
}
如果(输入<最小值| |输入>最大值){//解析值超出范围
printf(“输入超出范围%d到%d\n”,最小值,最大值);
返回0;
}
if(next!=NULL){//若next为NULL,则调用者不希望指针指向解析值的结尾
*next=end;//*next允许修改调用方的指针
}
*value=input;//*value允许修改调用者int
返回1;//成功
}
//投入
//int cols:需要传入可变长度数组
//int dest[][cols]:要使用解析值修改的可变长度数组
//int inputrow:要修改的可变长度数组的行
//int min:范围的最小值
//int max:范围的最大值
//char*line:指向要分析的文本的指针
//char*term:指向被视为终止符的字符的指针
//返回:0失败或1成功
int get_int_系列(int cols,int dest[][cols],int inputrow,int min,int max,char*行,char*项)
{
char*end=NULL;//将指向解析值的末尾
char*each=NULL;//将指向解析值的开头
int valid=0;
int输入=0;
整数计数=0;
int temp[cols];//解析值的临时存储
each=行;//每个点都指向行的起点
做{
valid=get_int_range(each,&end,term,&input,int_MIN,int_MAX);//调用解析一个值
如果(!valid){//失败
printf(“输入必须是一个数字\n”);
返回0;
}
如果(有效){//成功
temp[count]=input;//将解析后的值保存到临时数组中
计数++;
如果(计数>c