Good afternoon, the task is to help find the error in the opcode to the task (The task is normal with acmp, once written in Pascal long ago, but now with C ++):

Two unordered sets of integers are given (maybe with repetitions). To issue without repetition in ascending order all those numbers that are found in both sets.

Input data

In the first line of the input file INPUT.TXT, two integers are written N and M (1 ≤ N, M ≤ 106) - the number of elements of the first and second sets, respectively. The following lines contain first the N numbers of the first set, and then the M numbers of the second set. Numbers are separated by spaces or line breaks. Each of these numbers falls in the range from 0 to 105.

Output

In the output file OUTPUT.TXT you need to write in ascending order without repeating all the numbers that are included in both the first and the second set. Separate numbers with one space. If there are no such numbers, the output file should remain empty.

Opcode:

#include "stdafx.h" #include <iostream> using namespace std; void main() { int M, N, i, j, f, g; int a[10], b[10]; cin >> N >> M; cout << "\n\n\n\n"; for (f = 0; f < N; f++) { cin >> a[f]; } cout << "\n\n\n\n"; for (g = 0; g < M; g++) { cin >> b[g]; } cout << "\n\n\n\n"; for (i = 0; i < N; i++) { for (j = 0; j < M; j++) ; { if (a[i] == b[j]) { cout << b[j]; } } } } 

I use 10 studio (I neglected to work with input and output files, I do not plan to donate to acmp). Please suggest where the error!

  • one
    What error do you have? And in what place? - BuilderC
  • The same arrays of the arrays are not output, that is, at this place cout << "\ n \ n \ n \ n"; for (i = 0; i <N; i ++) {for (j = 0; j <M; j ++); {if (a [i] == b [j]) {cout << b [j]; }}} - SnowSnow
  • I just did not see the verification of the repeatability of matching elements. - skegg

2 answers 2

Everything is very simple, your program simply does not reach the output of identical numbers, since you have the body of the second for loop empty (standing; after the loop)! This is how it works:

 void main() { int M, N, i, j, f, g; int a[10], b[10]; cin >> N; cin >> M; cout << "\n\n\n\n"; for (f = 0; f < N; f++) { cin >> a[f]; } cout << "\n\n\n\n"; for (g = 0; g < M; g++) { cin >> b[g]; } cout << "\n\n\n\n"; for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { if (a[i] == b[j]) { cout << b[j] << endl; } } } system("pause"); } 
  • Thank you so much, plus you to the rating, I think in the process of studying (when I get used to the syntax) I will avoid such silly mistakes. - SnowSnow

You can do everything much easier:

 int main() { const int maxValue=105; int elementCount[2]; cin >> elementCount[0] >> elementCount[1]; bool arrays[2][maxValue+1]={false}; for(int j=0; j<2; j++) for(int i=0; i<NM[j]; i++) { int value; cin >> value; arrays[j][value]=true; //помечаем, что в этом наборе есть число value } //Выводим только те числа, которые повторяются в двух наборах for(int i=0; i<=maxValue; i++) if(arrays[0][i] && arrays[1][i]) cout << i; } 

In this way we automatically get rid of problems with possible repetition in the same array.

I did a task by the condition (just input to the screen instead of a file), without parsing your code. I did not understand why you have arrays of 10 elements, if they can be 105, and why do you output cout << "\n\n\n\n" ? By the condition of this should not be.