R
R
Reslyukov Alexander2020-10-26 15:59:20
C++ / C#
Reslyukov Alexander, 2020-10-26 15:59:20

How to compare each element of an array with all other elements?

According to the conditions of the problem, an array is given, I choose the size myself, I took 10. The goal is to display the number of repeated array elements in the definition area from m1 to m2 (values ​​are set by the user), and if there are none, display a message.
I don’t quite understand how I can write a comparison of the current element of the array in the for loop with all its other elements, or can I even do without a loop here?
Here's what's recorded so far:

#include <stdio.h>
#include <math.h>
int main () {
  int m1, m2;
  const int N=10;
  int arr [N];
  int i;
  int count=0;
  do {
  printf ("Input m1, m2");
  scanf ("%f%f", &m1, &m2);
}
while (m1>=m2);
for (i=m1; i<=m2; i++){
  printf ("Element with index %f: ", i)
  scanf ("%f", &arr[i]);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2020-10-26
@Alexandr_202

I don’t quite understand how I can compare the current element of the array in the for loop with all its other elements

A possible solution is to do it in a nested loop.
Another possible solution is to use a hash table (but its implementation will still contain cycles inside).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question