Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较两个整数数组并返回下标(C)_C - Fatal编程技术网

比较两个整数数组并返回下标(C)

比较两个整数数组并返回下标(C),c,C,(我的程序的更新版本最终是正确的) 我需要用一个函数创建一个程序,该函数比较两个整数数组,并返回它们不同的第一个位置的下标 我必须使用sentinel值来指示有效输入的结束,而不是EOF,因为一个文件不能有两个文件结束 我的程序所需的输入和输出示例: 投入1: 3 4 5 3 4 6 产出1: 2 说明:两个数组的第三个值不同,因此它打印第三个下标(计数为0,1,2) 我在stackoverflow上查看了类似的问题。许多错误已经修复,现在它让我可以编译一个程

(我的程序的更新版本最终是正确的)

我需要用一个函数创建一个程序,该函数比较两个整数数组,并返回它们不同的第一个位置的下标

我必须使用sentinel值来指示有效输入的结束,而不是EOF,因为一个文件不能有两个文件结束

我的程序所需的输入和输出示例:

投入1:

    3 4 5

    3 4 6
产出1:

       2
说明:两个数组的第三个值不同,因此它打印第三个下标(计数为0,1,2)

我在stackoverflow上查看了类似的问题。许多错误已经修复,现在它让我可以编译一个程序,而不给我任何错误或警告。但这让我看不见,因为我不明白到底什么不起作用

我遇到的问题是:

  • 当我在第一个值后按Enter按钮时,程序停止工作
我假设我的tableDiff函数是错误的,但是我从老师的笔记中复制了它。除非我打错了,否则不会有什么区别

这是我提交的版本:

/*
*一个按正确顺序对数字排序的简单程序
*/
#包括
#包括
#在数组中定义最多10//最多个元素
#定义SENTINEL-999//指示有效输入的结束
int main(){
int tableFill(int a[],int max);
int tableDiff(常量int a[],常量int b[],int n,int m);
无效表格打印(常量int a[],常量int b[],int n,int m);
int a[MAX];
int b[MAX];
int m,n,索引;
m=表格填充(a,最大值);
n=表格填充(b,最大值);
表格打印(a、b、n、m);
指数=tableDiff(a,b,m,n);
如果(索引==-1)
printf(“索引相同”);
其他的
printf(“索引不同”);
返回0;
}
//将值从标准输入读取到数组中,最大值为“max”
int tableFill(int a[],int max){
int r;//来自scanf的输入
int next;//下一个输入值
int cnt=0;//读取的值计数
int*ptr;//指针
printf(“输入数字!\n”);
while((r=scanf(“%i”、&next))==1&&next!=SENTINEL)
{
if(r==0)//输入数据无效
{
printf(“输入了非数字数据。请输入一个数字。\n”);
while(getchar()!='\n');//刷新无效数据
}
其他的
*ptr++=cnt;
} 
if(r==1)//读取了另一个值,但数组已满
printf(“错误-值太多。数组大小%i.\n”,最大值);
返回ptr-a;//(ptrb-b)应返回相同的值
}
int tableDiff(常量int a[],常量int b[],int n,int m)
{
const int*ptra=a;//开始第一个数组
const int*ptrb=b;//开始第二个数组
const int*endptra=a+m;//第一个数组结束
const int*endptrb=b+n;//第二个数组结束

然而(ptra你的逻辑中有很多问题。第一个问题是

while ((r=scanf("%i", &next)) == 1 && next != SENTINEL)
防止在输入非数值的情况下执行循环中的其余代码。如果
r!=1
,则退出循环,而不在循环中进一步处理

虽然不是错误,
main
中的函数原型仅在函数互不依赖时才起作用。它们在执行过程中彼此一无所知。最好将原型移到
main
上方

您的逻辑的其余部分有点难以理解,而且过于复杂。当您在两个数组中查找差异时,您只需要迭代常见元素。如果它们的元素数不同,则您知道从第一个唯一元素开始,它们的定义不同。因此,您可以缩减比较代码相当多。像下面这样的东西很好:

/* check if array 'a' and 'b' are the same, else return index
 * of first difference, otherwise return -1 for equal arrays.
 */
int tablediff (const int *a, const int *b, int sza, int szb)
{
    int i, lim = sza < szb ? sza : szb; /* limit search to common elements */

    for (i = 0; i < lim; i++)   /* for each common element check */
        if (a[i] != b[i])
            return i;

    if (sza != szb)             /* if size differs, arrays differ */
        return lim;

    return -1;                  /* otherwise equal */
}
示例相等

$ /bin/arraycmp <../dat/20intsame.txt
 8572 -2213 6434 16330 3034 12346 4855 16985 11250 1495
 8572 -2213 6434 16330 3034 12346 4855 16985 11250 1495

 the arrays are the same.
(注意:要在windoze上生成手动
EOF
,组合键为ctrl+z)

因此,要为每个数组输入少于10个整数,可以执行以下操作:

$ ./bin/arraycmp

enter a max of 10 integers below 'ctrl+d` to end.
10 12 14 16 17
enter a max of 10 integers below 'ctrl+d` to end.
10 12 14 15 17

the arrays entered are:

 10 12 14 16 17
 10 12 14 15 17

 the arrays differ at index '3'

查看示例,如果您还有其他问题,请告诉我。

max!=SENTINEL
这是否意味着
n!=SENTINEL
?我在tableFill函数中没有变量“n”,但我将其更改为next!=SENTINEL。这样做更有意义,因为我想检查“next”sentinel的值。是的,很抱歉这是一个输入错误。意思是
next
而不是
n
。在
tableFill
int*ptr;
-->
int*ptr=a;
else*ptr++=cnt;
否则{*ptr++=next;+cnt;}
,并添加检查
cnt
,如果(r==0)
Never true:ready
r==1
指针必须指向一个特定的对象。:-)我的C班老师年纪很大,她使用的材料与她几十年前教的完全相同。因此她希望函数原型使用main(),否则为演绎等级。我猜这会导致tablePrint(a,m);tablePrint(b,n);因为它,所以用4个变量打印到表中。否则它会给我一个错误。不确定sentinel,分配告诉我这样做(磁盘附带).我想结构代码对于现代C语言来说是非常过时的-但这是我的老师告诉我的。然而,逻辑是我个人的问题:-)我会尝试修复它。你可以移动
main
中的所有函数,只要它们不相互依赖(或者您可以在
main
下面对它们进行排序,以便首先声明依赖于另一个的任何一个。)如果这是她想要的方式,那么这就是她得到它的方式。如果你有任何其他问题,让我知道,我很乐意进一步帮助。如果你必须使用
哨兵,那也没关系,但是如果你的数组值之一是
-999
,它可能会引起冲突。找到的逻辑而不是==1,我放回去了!=EOF。现在when输入了非数字数据,它是这样说的。是的,这会起作用的!Sor
#include <stdio.h>

#define MAX 10 //max elements in array

int tablefill(int *a, int max);
int tablediff (const int *a, const int *b, int sza, int szb);
void tableprn (const int *a, int sza);

int main (void) {

    int a[MAX];
    int b[MAX];
    int idx, sza, szb;

    sza = tablefill (a, MAX);
    szb = tablefill (b, MAX);

    tableprn (a, sza);
    tableprn (b, szb);

    if ((idx = tablediff (a, b, sza, szb)) == -1)
        printf ("\n the arrays are the same.\n\n");
    else
        printf ("\n the arrays differ at index '%d'\n\n", idx);

    return 0;
}

/* read values from stdin into array up to 'max' values */
int tablefill (int *a, int max)
{
    int idx = 0, tmp;
    while (idx < max && scanf (" %d", &tmp) == 1)
        a[idx++] = tmp;

    return idx;
}

/* check if array 'a' and 'b' are the same, else return index
 * of first difference, otherwise return -1 for equal arrays.
 */
int tablediff (const int *a, const int *b, int sza, int szb)
{
    int i, lim = sza < szb ? sza : szb;
    for (i = 0; i < lim; i++)
        if (a[i] != b[i])
            return i;

    if (sza != szb)
        return lim;

    return -1;
}

/* print all elements in array. */
void tableprn (const int *a, int sz)
{
    int i;  //varriable to print
    for (i = 0; i < sz; i++)
        printf (" %d", a[i]);
    printf ("\n");
}
$ /bin/arraycmp <../dat/20intsame.txt
 8572 -2213 6434 16330 3034 12346 4855 16985 11250 1495
 8572 -2213 6434 16330 3034 12346 4855 16985 11250 1495

 the arrays are the same.
$ ./bin/arraycmp <../dat/20intdif.txt
 8572 -2213 6434 16330 3034 12346 4855 16985 11250 1495
 8572 -2213 6434 16330 3034 12346 4855 16985 11250 1494

 the arrays differ at index '9'
    printf ("\nenter a max of 10 integers below 'ctrl+d` to end.\n");
    sza = tablefill (a, MAX);
    printf ("enter a max of 10 integers below 'ctrl+d` to end.\n");
    szb = tablefill (b, MAX);

    printf ("\nthe arrays entered are:\n\n");
    tableprn (a, sza);
    tableprn (b, szb);
$ ./bin/arraycmp

enter a max of 10 integers below 'ctrl+d` to end.
10 12 14 16 17
enter a max of 10 integers below 'ctrl+d` to end.
10 12 14 15 17

the arrays entered are:

 10 12 14 16 17
 10 12 14 15 17

 the arrays differ at index '3'