循环输入时的数组指针 [英] Array pointer while loop input

查看:103
本文介绍了循环输入时的数组指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序来检查来自可编程手持式条形码扫描仪的重复输入,我正在对其进行编程以能够扫描2000个条形码。

I am trying to write a program to check for duplicate input from a programmable handheld barcode scanner, I am programming it to be able to scan 2000 barcodes.

I是c编程的新手,我试图输入一个值并将其放入数组,并在每个循环中增加指向下一个的指针。

I am new to c Programming, I am trying to take an input and put it into the array and increase the pointer to the next with every loop.

示例: int数组[10];

我要 scanf() 进入第一个循环的数组位置0 ,然后每次while循环运行时,将其增加1个位置至位置2 等。

I want to scanf() into Array location 0 on the first loop then increment by 1 location to location 2 etc every time the while loops runs. Please help, your help is much appreciated.

#include <stdio.h>

int main ()
{
    int i,j,k=1,arr[2000],ArrSize=2000;

    //Welcome message
    printf("Welcome to Yamato Transport (S) Pte. Ltd.\n");
    printf("Barcode checker Ver 1.0\n");

    while (k>=2000,k++)
    {
        //Scanner Input
        for(i=0;i<ArrSize;i++)
        {
        scanf("%d",&arr[i]);
        }

        //Duplicate Logic
        printf("Duplicate Barcodes: ");
        for(i=0; i<ArrSize; i++)
        {
            for(j=i+1;j<ArrSize;j++)
            {
            if(arr[i]==arr[j])
            {
            printf("%d\n",arr[i]);
            }
        }
        }
        //Remove single duplicate

        //Clear Screen

    }
  return 0;
}


推荐答案

代码的第一个问题是这样的:

The first problem with your code is this:

int i,j,k=1,...

...

while (k>=2000,k++)
        ^^^^^^
        ups...

这将给出警告逗号表达式的左侧操作数无效,实际上意味着该行与以下内容相同:

This will give the warning "left-hand operand of comma expression has no effect" which actually means that the line is the same as:

while (k++)

将一直循环,直到出现整数溢出(这是未定义的行为)为止。因此,您不想这样做。

That will keep looping until you have integer overflow (which is undefined behavior). So you don't want to do that.

由于将 k 初始化为1,所以我假设您想要要做:

Since you initialize k to 1, I assume that you wanted to do:

while (k++ < 2000)

现在让我们假设您想要:

Now let's assume that you want:

while (k++ < 2000)  // Will loop approx 2000 times
{
    //Scanner Input
    for(i=0; i< ArrSize; i++)  // Will loop 2000 times
    {
        scanf("%d",&arr[i]);
    }

最后,您的程序调用 scanf 2000 x 2000 = 4.000.000次。那是你要的吗?外部 while 的用途尚不清楚。

So in the end your program calls scanf 2000 x 2000 = 4.000.000 times. Is that what you want? The purpose of the outer while is unclear.

您的程序首先读取2000个整数,然后似乎要删除重复项。这是一种不好的方法,因为每当需要从数组中删除重复的元素时,您可能都会结束大量内存移动操作。

Your program first reads 2000 integers and afterwards it seems you want to remove duplicates. That's a bad approach as you may end you doing a lot of memory move whenever you need to remove a duplicate element from the array.

一种更好的方法是检查是否新扫描的值是之前插入到数组中的重复值。可能看起来像这样:

A better approach is to check whether a newly scanned value is a duplicate before inserting it in the array. That could look something like:

    for(i=0; i < ArrSize; )
    {
        int tmp;
        if (scanf("%d", &tmp) != 1) 
        {
            // Input error
            exit(1);
        }

        // Check if tmp is already in the array
        int duplicate = 0;
        for (int j = 0; j < i; ++j)
        {
            if (tmp == arr[j])
            {
                duplicate = 1;
                break;
            }
        }
        if (duplicate)
        {
            printf("dup found\n");
        }
        else
        {
            arr[i] = tmp;
            ++i;
        }
    }

这应该给您 ArrSize 唯一元素。

注意:为了检查某项是否重复,您需要从头到尾扫描整个数组。当前元素数。为了提高性能,您可以考虑另一种方法,例如排序的树,哈希表等,从而可以更快地检查重复项。随着数组元素数量的增加,这一点变得越来越重要。

Notice: In order to check if something is a duplicate, you'll need to scan through the array from start to the current number of elements. To improve performance you could consider another approach, e.g. a sorted tree, hash tables, etc so that check for duplicates can be done much faster. This gets more important as the number of array elements increase.

这篇关于循环输入时的数组指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆