Home > Back-end >  Check if all integers in array are the same - C
Check if all integers in array are the same - C

Time:12-06

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.

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;

}

CodePudding user response:

Two main issues (and other minor issues):

  1. The scanf statement is populating variable num but not the array, which is never initialised.
  2. The loop in the check function should stop at the second last index. 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;

}

CodePudding user response:

You are not storing the vale from scanf. Try this:

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

CodePudding user response:

You are not setting the actual array values replace your scanf call with scanf("%d", &arr[i]);

  • Related