如何从用户扫描两个数组?
我对编程比较陌生,我必须编写一个函数来读取用户的输入并填充两个数组,然后比较它们。 我想我感到困惑的是如何读取两个数组。
这就是我应该做的,
编写一个 table_diff 函数来比较两个整数数组并返回它们第一个不同位置的下标。 如果数组相同,该函数应返回 -1 ex:
345 & 第345章--> -1(相同)
345 & 第346章——> 2(索引 2 处不同)
1234 和 123 --> 3(索引 3 处不同)
这就是我所拥有的,感谢任何帮助!
while((r = scanf("%i", &value)) != 1 && ptra < endptra)
{
*ptra ++ = value;
if (r==1)
printf("No room after reading values\n\n");
else if(r != EOF)
printf("invalid char");
}
while((r = scanf("%i\n", &value))!= 1 && ptrb < endptrb){
*ptrb ++ = value;
if (r==1)
printf("No room after reading values\n\n");
else if(r != EOF)
printf("invalid char");
}
I'm relatively new to programming and I have to write a function that reads in input from the user and to fill two arrays then compare them. I guess what I'm confused on is how to read in both arrays.
This is what I'm supposed to do,
Write a table_diff function that compares two arrays of integers and returns the subscript of the first place they differ. If the arrays are the same, the function should return -1 ex:
345 & 345 --> -1 (same)
345 & 346 --> 2 (differ at index 2)
1234 & 123 --> 3 (differ at index 3)
This is what I have, any help is appreciated!
while((r = scanf("%i", &value)) != 1 && ptra < endptra)
{
*ptra ++ = value;
if (r==1)
printf("No room after reading values\n\n");
else if(r != EOF)
printf("invalid char");
}
while((r = scanf("%i\n", &value))!= 1 && ptrb < endptrb){
*ptrb ++ = value;
if (r==1)
printf("No room after reading values\n\n");
else if(r != EOF)
printf("invalid char");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为您想将代码更改为以下内容:
*
运算符的优先级高于++
。I think you want to change your code to the following:
The
*
operator has higher precedence than++
.这并没有真正回答你的问题,但在你的“while”条件下,你已经检查了“r != 1”,所以在 while 块内,检查“if(r == 1) ...”是不必要的,以及技术上无法访问的代码。
该计划的重点是什么? 现在看起来,你读入数组 ptra 直到一个空行,然后读入 ptrb 直到一个空行......然后你说你需要比较两者,但这是什么意思? 结果应该是什么? 逐项比较数组? 还是只按内容而不按位置? 需要更多信息...
This doesn't really answer your question, but in your "while" condition, you already check for "r != 1", so within the while block, checking "if(r == 1) ..." is unnecessary, and technically unreachable code.
What is the point of the program? The way it looks now, you read into array ptra until a blank line, then read into ptrb until a blank line... then you say you need to compare the two, but what do you mean by that? what should the result be? compare the arrays item by item? or just by contents not by position? need more info...
另外,这里还有一个
复制-粘贴
的情况。 你做同样的事情两次。 你这样做怎么办? 使用通用代码编写一个函数并仅使用相应的参数调用它。Also, you have a case of
copy - paste
here. You do the same thing twice. What do you do thus? Write a function with the common code and call it with just the respective parameters.看来您缺少实际的检查部分。 现在您已经读取了每个数组,您应该使用另一个循环遍历它并检查每个数组中的值。
It looks like you are missing the actual checking part. Now that you ahve read in each array you should walk across it with another loop and check the value in each one.
我不认为 scanf 的工作方式是你想象的那样。 如果不看看你是如何输入的,就很难说。 为了像这样循环,我认为你需要在每个整数输入后发送一个 EOF (来自 stdin 的 CTRL-D )。 如果这就是您的做法,那么唯一终止循环的就是指针比较。 您是否知道最大数组大小? 另一方面,如果您的输入是“1234^D”,那么您最终将得到整数 1234 的一个数组条目,而不是 1,2,3,4。
最后,该 if 语句看起来像是您正在尝试确定循环退出的原因; 如果是这样,它应该在 while 循环之外。
I don't think scanf works the way you're thinking it works. It's hard to say without seeing how you do your input; in order to loop like that, I think you'd need to be sending an EOF (CTRL-D from stdin) after every integer entry. If that is how you're doing it, then the only thing that will terminate your loop is the pointer comparison. Do you have a known maximum array size? If, on the other hand, your input is "1234^D", you're going to end up with one array entry of the integer 1234, rather than 1,2,3,4.
Finally, that if statement looks like you're trying to determine why the loop exited; if so, it should be outside of the while loop.
解决编程中循环问题的方法之一是我大约 15 年前在大学课程中学到的东西之一。 这就是“循环不变式”。
发表一项永远正确的声明。 在循环内部,条件可能会改变,但请确保如果进行下一次迭代,您的不变量再次有效。
就像:
“数组的前 i 个位置是相同的”
所以从 0 开始(总是 true),并且在循环期间首先检查第一个位置是否相同。 如果为真,您可以将“i”变量加一。 对 2nd 等做同样的事情。所以基本上 i 变量决定将检查下一个索引。
我确信互联网会提供一些更好的示例,但它可能会对您有所帮助,同时提高您的开发人员技能。
One of the ways to fix your problems with loops in programming is one the things I learned in one of my university courses about 15 years ago. That is the "loop invariant".
Make one statement that shall always be true. Inside the loop, the condition may change, but make sure that if you do the next iteration, your invariant is valid again.
Like:
"The first i positions of the arrays are the same"
So start with 0 (always true), and during the loop check first if the first position is the same. If true, you are able to increase the 'i' variable with one. Do the same with the 2nd , etc. So basically the i variable determines which next index will be checked.
I'm sure the internet will provide with some better examples, but it might help you and at the same time increase your developers skill.