简体   繁体   English

检查数组中的所有整数是否相同 - C

[英]Check if all integers in array are the same - C

I've been given an exercise that asks the user to decide the size of the array to a maximum of 30, fill it , and then check that all numbers contained within it are equal .我得到了一个练习,要求用户决定数组的大小,最大为 30,填充它,然后检查其中包含的所有数字是否相等

I tried this way, but the result is always "The elements of the array are not all the same", even though they are.我尝试过这种方式,但结果总是“数组的元素并不完全相同”,即使它们是一样的。

Could someone give me a hand?有人可以帮我吗? Below I insert the code that I have already written下面我插入我已经写好的代码

#include <stdio.h>
#include <stdbool.h>

#define MAX_DIM 30

int check(int[], int);

int main(int argc, char *argv[]) {

    int dim;
    int num;
    int flag;
    int arr[MAX_DIM];

    printf("Insert an array dimension. \n");
    printf("Remember that the maximum size the array can take is %d \n\n", MAX_DIM);
    printf("Array dimension: ");
    scanf("%d", &dim);

    if (dim <= MAX_DIM) {
        arr[dim];
    } else {
        printf("Array dimension isn't valid! \n");
        return 0;
    }

    printf("\n");

    printf("Enter the numbers to place in the array of size %d ", dim);
    for (int i = 0; i < dim; i++) {
        scanf("%d", &num);
    }

    int equals = check(arr, dim);

    if (equals == 1) {
        printf("he elements of the array are all the same \n");
    } else {
        printf("he elements of the array are not all the same \n");
    }  

}

int check(int arr[], int dim) {
    
    for (int i = 0; i < dim; i++) {
        if (arr[i] != arr[i + 1]) {
            return -1;
        }
    }
    return 1;

}

Two main issues (and other minor issues):两个主要问题(和其他小问题):

  1. The scanf statement is populating variable num but not the array, which is never initialised. scanf 语句正在填充变量 num 但不是数组,它从未被初始化。
  2. The loop in the check function should stop at the second last index.检查 function 中的循环应该在倒数第二个索引处停止。 Going to the end means you are comparing with an out of bounds value at the end.走到最后意味着您正在与最后的越界值进行比较。
#include <stdio.h>
#include <stdbool.h>

#define MAX_DIM 30

int check(int[], int);

int main(int argc, char *argv[]) {

    int dim;
    int num;
    int flag;
    int arr[MAX_DIM];

    printf("Insert an array dimension. \n");
    printf("Remember that the maximum size the array can take is %d \n\n", MAX_DIM);
    printf("Array dimension: ");
    scanf("%d", &dim);

    if (dim >= MAX_DIM) {
        printf("Array dimension isn't valid! \n");
        return 0;
    }

    printf("\n");

    printf("Enter the numbers to place in the array of size %d ", dim);
    for (int i = 0; i < dim; i++) {
        scanf("%d", arr + i);
    }

    int equals = check(arr, dim);

    if (equals == 1) {
        printf("The elements of the array are all the same \n");
    } else {
        printf("The elements of the array are not all the same \n");
    }  

}

int check(int arr[], int dim) {
    
    for (int i = 0; i < dim - 1; i++) {
        if (arr[i] != arr[i + 1]) {
            return -1;
        }
    }
    return 1;

}

You are not storing the vale from scanf.您没有存储来自 scanf 的值。 Try this:试试这个:

for (int i = 0; i < dim; i++) {
        scanf("%d", &num);
        arr[i]=num;
    }

You are not setting the actual array values replace your scanf call with scanf("%d", &arr[i]);您没有设置实际的数组值用scanf("%d", &arr[i]);替换您的 scanf 调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM