A
A
Andrey Rudenko2020-11-04 23:27:30
C++ / C#
Andrey Rudenko, 2020-11-04 23:27:30

How to find paired array elements that are greater than the number A, and their number, enter the number A from the keyboard?

Good evening! I ran into a problem, I need to solve the problem, but I don’t understand a little how to do it, I’ve done a lot in the code and I understand nothing. Help me please.

Here the conditions of the problem - given an integer one-dimensional array, consisting
of 16 elements. Find paired elements of the array that are greater than the number A,
and their number. Enter the number A from the keyboard.
Thank you!

#include <stdio.h>
#define N 16
main() {
    int a[N],A, i, j, f;
    int count=0;
    printf("Input A="); scanf ("%f", &A);
    for (i=0; i<N; i++) {
        a[i] = rand() % 15;
        printf("%d ", a[i]);
    }
    printf("\n");
    for (i=0; i<N; i++) {
        count++;
        f = 1;
        for (j=0; j<N; j++)
            if (a[i] == a[j] && i != j) {
                f = 0;
                break;
            }
        if ((f == 1) > A) printf("%d ", a[i]);

    }
    printf("\n%d", count);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey_Alive, 2020-11-06
@FlapsBat

You have a super curved code. For example, in Expression (f == 1) there will be either 0 or 1. Why are you comparing it with A, it is not clear. Wrote a working version.
(f == 1) > A

#include <stdio.h>
#include <stdlib.h>


int main()
{
    enum {arrSize = 16};
    _Static_assert(arrSize >= 1, "arrSize should be more than 0");

    int arr[arrSize];

    for(size_t i = 0; i < arrSize; ++i)
    {
        arr[i] = rand() % 10; //Чтобы точно были парные элементы
    }

    int defNum;
    scanf("%d", &defNum);

    size_t count = 0;

    for(size_t i = 0; i < arrSize - 1; ++i)
    {
        if(arr[i] <= defNum)
        {
            continue;
        }

        size_t tmp = 0;

        for(size_t j = i + 1; j < arrSize; ++j)
        {
            if(arr[i] == arr[j])
            {
                ++tmp;
                arr[j] = defNum;
            }
        }

        if(tmp)
        {
            count += tmp + 1;
            printf("%d ", arr[i]);
        }
    }

    printf("\n%llu", count);

    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question