数组数组 [英] An Array of Arrays
问题描述
我想在数组中存储一组包含2个数字的数组.但是我只希望在5个后跟一个逗号并输入另一个数字时存储2个数字.本质上,我希望程序执行的操作是从此数组数组中读取并相应地执行任务.因此,如果用户输入2,我想将(2,0)存储在数组的一个空间中,然后继续向用户询问第二个数字.但是,如果用户键入5,10,我希望程序将(5,10)存储在同一数组中.然后,我的程序可以过滤哪个数组只有一个值,哪个数组只有2,并相应地执行不同的任务.我的分配要求我们不要为每个数组要求2个数字,这样会更容易.
I want to store a group of arrays containing 2 numbers in an array. But I only want 2 numbers to be stored when 5 followed by a comma and another number is entered. Essentially, what I want my program to do is read from this array of arrays and perform tasks accordingly. So if the user enters 2, I want to store (2,0) in one space of my array and move on to ask my user for the second number. But if the user types 5,10 I want the program to store (5,10) in that same array. Then my program could filter which array has only one value and which has 2 and do different tasks accordingly. My assignment requires us to not ask 2 numbers for each array which would have made it easier.
这是我到目前为止所拥有的,我知道我错了,我只是不知道从这里去哪里
This is what I have so far and I know I'm wrong I just don't know where to go from here:
int main(void)
{
int size = 0;
int input;
int factor;
int mdArrays[100][2];
for (size_t i = 0; i < 100; i++)
{
size = i;
scanf("%d,%d", &input, &factor);
if (input != 5 && input != 9)
{
factor = 0;
for (size_t j =0 ; j< 2; j++)
{
mdArrays[i] = input;
mdArrays[j] = factor;
}
}
else if (input == 9)
{
break;
}
else
{
for(int j = 0; j< 2; j++)
{
mdArrays[i] = input;
mdArrays[j] = factor;
}
}
}
for (size_t i =0; i < size; i++)
{
for(size_t j = 0; j < 2; j++)
{
printf("%d,%d", mdArrays[i[j]]);
}
}
}
推荐答案
有一些问题.
size
太短了(应该为 i + 1
).
使用 scanf
处理 5
与 5,23
可能是 .但是,我更喜欢使用 fgets
和 strtol
并检查定界符(例如,是否为,
).
It may be possible to handle 5
vs 5,23
using scanf
. But, I prefer to use fgets
and strtol
and check the delimiter (e.g. whether it's ,
or not).
如果我们对 input == 9
进行第一个测试以停止循环,则可以简化 if/else
梯形图逻辑.
The if/else
ladder logic can be simplified if we make the first test against input == 9
to stop the loop.
根据您的代码,您要强制 factor
为零 if input!= 5
.这对我来说没有多大意义,但是[我暂时]保留了这种逻辑.
According to your code, you want to force a factor
of zero if input != 5
. That doesn't make much sense to me, but I've kept that logic [for now].
这可能不是您想要/需要的,但这是我对您的代码的最佳解释.主要目的是区分给定行上有多少个数字.因此,请根据需要调整其余部分.
That may not be what you want/need, but it was my best interpretation of your code. The main purpose is to differentiate how many numbers are on a given line. So, adjust the rest as needed.
我认为您存储/显示数组的方式不正确.我相信您想将 input
存储到 mdArrays [i] [0]
中,将 factor
存储到 mdArrays [i] [1]
.使用 j
对我没有意义.
I think the way you're storing/displaying the array is incorrect. I believe you want to store input
into mdArrays[i][0]
and factor
into mdArrays[i][1]
. Using j
makes no sense to me.
正如我在[我的最高评论]中提到的那样,最后一个循环中的 printf
是无效的.
As I mentioned [in my top comments], the printf
in the final loop is invalid.
请注意,如果我们不用多个的文字 100
硬接线尺寸,则代码会更干净>位置(例如,一次在 myArrays
声明中,再一次在外部 for
循环中).最好使用(例如) #define MAXCOUNT 100
并将 100
用 MAXCOUNT
替换其他地方(见下文).
Note that the code is cleaner if we don't hardwire the dimensions with a literal 100
in multiple places (e.g. once in the myArrays
declaration and again in the outer for
loop). Better to use (e.g.) #define MAXCOUNT 100
and replace 100
elsewhere with MAXCOUNT
(see below).
我创建了三个版本.用原始和固定代码注释的代码.另一个删除原始代码.并且,三分之一使用 struct
组织数据.
I created three versions. One that is annotated with original and fixed code. Another that removes the original code. And, a third that organizes the data using a struct
.
这是重构的代码.我已将您/旧代码括起来[vs.我的/新代码],其中包括:
Here's the refactored code. I've bracketed your/old code [vs. my/new code] with:
#if 0
// old code
#else
// new code
#endif
我添加了一个调试 printf
.无论如何,这是带有一些注释的代码:
I added a debug printf
. Anyway, here's the code with some annotations:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
int size = 0;
int input;
int factor;
int mdArrays[100][2];
for (size_t i = 0; i < 100; i++) {
#if 0
size = i;
scanf("%d,%d",&input,&factor);
#else
// get line
char buf[100];
char *cp = fgets(buf,sizeof(buf),stdin);
if (cp == NULL)
break;
// strip newline -- only needed for debug print
cp = strchr(buf,'\n');
if (cp != NULL)
*cp = 0;
// decode first number
input = strtol(buf,&cp,10);
// decode second number if it exists -- otherwise, use a sentinel
if (*cp == ',')
factor = strtol(cp + 1,&cp,10);
else
factor = -1;
printf("DEBUG: buf='%s' input=%d factor=%d\n",buf,input,factor);
#endif
// stop input if we see the end marker
if (input == 9)
break;
// remember number of array elements
size = i + 1;
// only use a non-zero factor if input is _not_ 5
if (input != 5) {
factor = 0;
#if 0
for (size_t j = 0; j < 2; j++) {
mdArrays[i] = input;
mdArrays[j] = factor;
}
continue;
#endif
}
#if 0
for (int j = 0; j < 2; j++) {
mdArrays[i] = input;
mdArrays[j] = factor;
}
#else
mdArrays[i][0] = input;
mdArrays[i][1] = factor;
#endif
}
for (size_t i = 0; i < size; i++) {
#if 0
for (size_t j = 0; j < 2; j++) {
printf("%d,%d",mdArrays[i[j]]);
}
#else
printf("%d,%d\n",mdArrays[i][0],mdArrays[i][1]);
#endif
}
return 0;
}
这是我用来测试的示例输入:
Here's the sample input I used to test:
5,3
7,6
8,9
5,37
5
9,23
这是程序输出:
Here's the program output:
DEBUG: buf='5,3' input=5 factor=3
DEBUG: buf='7,6' input=7 factor=6
DEBUG: buf='8,9' input=8 factor=9
DEBUG: buf='5,37' input=5 factor=37
DEBUG: buf='5' input=5 factor=-1
DEBUG: buf='9,23' input=9 factor=23
5,3
7,0
8,0
5,37
5,-1
这是一个稍作清理的版本:
Here's a slightly cleaned up version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCOUNT 100
int
main(void)
{
int size = 0;
int input;
int factor;
int mdArrays[MAXCOUNT][2];
for (size_t i = 0; i < MAXCOUNT; i++) {
// get line
char buf[100];
char *cp = fgets(buf,sizeof(buf),stdin);
if (cp == NULL)
break;
// strip newline -- only needed for debug print
#ifdef DEBUG
cp = strchr(buf,'\n');
if (cp != NULL)
*cp = 0;
#endif
// decode first number
input = strtol(buf,&cp,10);
// decode second number if it exists -- otherwise, use a sentinel
if (*cp == ',')
factor = strtol(cp + 1,&cp,10);
else
factor = -1;
#ifdef DEBUG
printf("DEBUG: buf='%s' input=%d factor=%d\n",buf,input,factor);
#endif
// stop input if we see the end marker
if (input == 9)
break;
// remember number of array elements
size = i + 1;
// only use a non-zero factor if input is _not_ 5
if (input != 5)
factor = 0;
mdArrays[i][0] = input;
mdArrays[i][1] = factor;
}
for (size_t i = 0; i < size; i++)
printf("%d,%d\n",mdArrays[i][0],mdArrays[i][1]);
return 0;
}
使用 struct
[YMMV],您可能受益,因此,这里有一个可以使事情井井有条的版本:
You might benefit from using a struct
[YMMV], so here's a version that keeps things organized that way:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCOUNT 100
typedef struct {
int input;
int factor;
} data_t;
int
main(void)
{
int size = 0;
data_t mdArrays[MAXCOUNT];
data_t *data;
for (size_t i = 0; i < MAXCOUNT; i++) {
// get line
char buf[100];
char *cp = fgets(buf,sizeof(buf),stdin);
if (cp == NULL)
break;
// strip newline -- only needed for debug print
#ifdef DEBUG
cp = strchr(buf,'\n');
if (cp != NULL)
*cp = 0;
#endif
data = &mdArrays[i];
// decode first number
data->input = strtol(buf,&cp,10);
// decode second number if it exists -- otherwise, use a sentinel
if (*cp == ',')
data->factor = strtol(cp + 1,&cp,10);
else
data->factor = -1;
#ifdef DEBUG
printf("DEBUG: buf='%s' input=%d factor=%d\n",buf,input,factor);
#endif
// stop input if we see the end marker
if (data->input == 9)
break;
// remember number of array elements
size = i + 1;
// only use a non-zero factor if input is _not_ 5
if (data->input != 5)
data->factor = 0;
}
for (size_t i = 0; i < size; i++) {
data = &mdArrays[i];
printf("%d,%d\n",data->input,data->factor);
}
return 0;
}
这篇关于数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!