簡體   English   中英

如何在C編程語言中比較兩個數組?

[英]How to compare two arrays in C programming language?

我想比較兩個不同的數組,它們都是int 第一個數組是靜態的,包含 1 到 10 的數字,第二個數組要求用戶輸入十個不同的數字,程序會檢查兩個數組中的哪些元素相等。

#include <stdio.h>

int main(void) {
    int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int array2[10];
    int i;

    for (i = 0; i < 11; i++) {
        printf("Enter numbers: ");
        scanf("%d", &array2);
    }

    for (i = 0; i < 11; i++) {
        if (array1[i] != array2[i]) {
            printf("Not equal \n");
        } else {
            printf("They are equal. \n");
        }
    }
}

即使我輸入的數字等於存儲在第一個數組中的數字,程序也總是說不相等。

數組使用基於零的索引作為開始。 您錯誤地填充了array2因此您可能希望將第一個循環更改為以下內容

    for (i=0;i<10;i++) 
    {
            printf("Enter numbers: ");
            scanf("%d", &array2[i]);
    }

因為您當前的代碼只是將array2的地址作為參數傳遞給scanf

然后將第二個循環條件改為

 for (i=0;i<10;i++) 

在您的比較循環中,以便您不會訪問超出數組邊界的項目。

目前,您的第二個循環正在訪問索引 0 到 10 處的項目 - 但array1中只有 10 個項目,因此您當前的代碼存在未定義的行為。

scanf("%d", &array2);

從輸入中獲取值時,您永遠不會更新array2的索引。

嘗試

scanf("%d", &array2[i]);

至於比較,也可以使用memcmp來比較內存:

memcmp(array1, array2, sizeof(array1));
#include <stdio.h>
int main(void) {

        int array1[] = {1,2,3,4,5,6,7,8,9,10};
        int array2[10];
        int i;

        for (i=0;i<10;i++) { //fixed the range here

                printf("Enter numbers: ");
                scanf("%d", &array2[i]); //fixed the indexing
        }

        for (i=0;i<10;i++) { //fixed the range here

                if (array1[i] != array2[i]) {

                        printf("Not equal \n");
                }
                else {
                        printf("They are equal. \n");
                }
        }
}

我是初學者,我有比較兩個數組的想法。 希望它可以幫助像我這樣的人。

/***compare two array: all elements are same or not(if not sorted)***/

#include<stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    
    int array1[n], array2[n];
    int i, j;
    
    for(i=0; i<n; i++)
    {
        scanf("%d", &array1[i]);
    }
    
    for(i=0; i<n; i++)
    {
        scanf("%d", &array2[i]);
    }
    
    int flg=0;
    for(i = 0; i < n; i++)
    {
        for(j=0; j<n; j++)
        {
            if(array1[i] == array2[j])
            {
                flg += 1;
                break;
            }
        }
    }
    if(flg == n)
    {
        printf("All The Elements of array1 is present in array2... :)");
    }
    else
    {
        printf("All THe Elements of array1 is not present in array2 :(");
    }
    return 0;
}

盡管我是 C 程序的初學者,但我正在嘗試回應答案。

根據您上面編寫的程序,您正在向具有 11 個元素的int array2[10]輸入和保存值。

請記住,該數組的第一個元素的索引為零。 例如: array2[0] ,直到它到達array2[10]的最后一個元素,你已經數到了 11。

現在array1有預定義的值 write 將與您的輸入值進行比較。 輸入您的值並將其存儲到array2[]

#include <stdio.h>

int main(void) {
    int array1[] = {1,2,3,4,5,6,7,8,9,10};
    int array2[10];
    int i;

    for (i=0;i<10;i++) { //fixed the range here
        printf("Enter numbers: ");
        scanf("%d", &array2[i]); //fixed the indexing

        if (array1[i] != array2[i]) {
            printf("Not equal \n");
        }
        else {
            printf("They are equal. \n");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM