It does not work adequately, although logically, it seems, all the rules:

#include <iostream> #include <time.h> using namespace std; void main() { const int row = 4, col = 4; int arr[row][col]; srand(time(NULL)); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = rand() % 10; cout << arr[i][j] << " "; } cout << endl; } cout << endl; /*arr[3][0] = 1; arr[3][1] = 1; arr[3][2] = 1; arr[3][3] = 1; arr[3][4] = 1;*/ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << arr[i][j] << " "; } cout << endl; } int index = 0, count = 0, tmp = 0, num = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { num = arr[i][j]; for (int z = 0; z < row; z++) { //здСсь ΠΏΠ΅Ρ€Π΅Π±ΠΎΡ€ ΠΈ сравнСниС tmp = 0; for (int x = 0; x < col; x++) { if (arr[z][x] == num) { if ((i != z) && (j != x)) { tmp++; } } if (tmp > 0) { if (tmp >= count) { index = z; count = tmp; } } } } } } cout << index<<endl; } 
  • do not write void main() , this is wrong. - Abyx
  • @Abyx right - tkachuk

2 answers 2

Without the use of containers, this will in fact be pure C code. The only difference is the use of new , not malloc . The most non-trivial part of the task is to find the β€œlargest number of repeating elements in a row”. If it is meant that in line 1, 2, 1, 2, 1 this number is 3 (3 units), the solution of this problem simply requires separation into a separate function. This feature might look like this:

 size_t count_els(size_t size, int row[]) { if (!row || !size) return 0; int *elements = new int[size]; // массив ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… элСмСнтов Π² row size_t *counter = new size_t[size]; // массив счСтчиков количСства элСмСнтов if (!elements || !counter) { cout << "cannot allocate memory"; return 0; } for (size_t i=0; i<size; ++i) counter[i] = 0; // Π΅Π³ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΠΎΠ±Π½ΡƒΠ»ΠΈΡ‚ΡŒ size_t current_index = 0; for (size_t i = 0; i < size; ++i) { int element = row[i]; // find element in elements[0:current_index] bool need_to_add = true; for (size_t j = 0; j < current_index; ++j) { // ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° counter[j]!=0 Π΄ΠΎΠ»ΠΆΠ½Π° ΡΡ‚ΠΎΡΡ‚ΡŒ ΠΏΠ΅Ρ€Π²ΠΎΠΉ! if (counter[j] != 0 && elements[j] == element) { // нашли need_to_add = false; // Π΄ΠΎΠ±Π°Π²Π»ΡΡ‚ΡŒ Π½Π΅ Π½ΡƒΠΆΠ½ΠΎ counter[j]++; break; // Ρ‚.ΠΊ. элСмСнты ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅, дальшС ΠΈΡΠΊΠ°Ρ‚ΡŒ Π½Π΅ Π½ΡƒΠΆΠ½ΠΎ } } if (need_to_add) { // элСмСнт row[i] Π½Π΅ Π½Π°ΠΉΠ΄Π΅Π½ Π² массивС elements, добавляСм elements[current_index] = element; counter[current_index] = 1; ++current_index; } } // Π’Π΅ΠΏΠ΅Ρ€ΡŒ Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΉ элСмСнт Π² массивС counter size_t max_counter = 0; for (size_t i = 0; i < current_index; ++i) { if (counter[i] > max_counter) max_counter = counter[i]; } delete[] counter; delete[] elements; return max_counter; } 

In fact, we need to count the number of all unique elements in a string, and find the maximum. For this (IMHO) one cannot do without two additional arrays (the unique elements of the elements and the counter of their number counter ). Their size cannot exceed the length of the string. In principle, nothing prevents you from writing all this code directly in main , but it will look awfully clumsy.

With this function, main (after filling the initial two-dimensional array m size of rows x cols ) may look like this:

 size_t needed_row = 0, longest = 0; for (size_t row = 0; row < rows; ++row) { current = count_els(cols, m[row]) if (current > longest) { longest = current; needed_row = row; } } cout << "needed row index is " << needed_row << endl; 

If your teacher objects to a particular function, argue. If he is not a fool, he will understand that she is needed here. Well, if you're a fool, there's nothing you can do.


Code using standard library containers:

 #include <iostream> #include <vector> #include <utility> using namespace std; template<typename T> size_t maxcounted_element(const vector<T> &v) { // Найти наибольшСС число ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΡΡŽΡ‰ΠΈΡ…ΡΡ элСмСнтов Π² Π²Π΅ΠΊΡ‚ΠΎΡ€Π΅ vector<pair<size_t, T>> t; t.reserve(v.size()); for (const T& el: v) { bool to_push = true; for (size_t i=0; i<t.size(); ++i) { if (t.at(i).second == el) { ++t.at(i).first; to_push = false; break; } } if (to_push) { t.push_back({1, el}); } } size_t maxs = 0; for (auto &e: t) { if (e.first > maxs) maxs = e.first; } return maxs; } template<typename T> size_t find_row(size_t size, const vector<T> m[]) { // Π½Π°ΠΉΡ‚ΠΈ строку с наибольшим количСством ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΡΡŽΡ‰ΠΈΡ…ΡΡ элСмСнтов if (!m || !size) return 0; size_t longest = 0; size_t index = 0; for (size_t i=0; i<size; ++i) { size_t current = maxcounted_element(m[i]); if (current > longest) { longest = current; index = i; } } return index; } int main() { vector<double> m[] = { {1, 2, 3}, {1, 1, 1, 4, 5}, {1, 2, 5, 7, 9, 2, 2, 2}, {}, {1.23, 1.23, 3.14} }; size_t row = find_row(5, m); cout << "Row number " << row << " contains " << maxcounted_element(m[row]) << " equals elements" << endl; return 0; } 

The output of the program:

 Row number 2 contains 4 equals elements 

    Why make this mess? He confuses himself and it is not interesting to understand others in this pile of letters. It is easier and more efficient to break the code into subtasks, debug each of them separately, and only then assemble into a single whole. You can start small: with writing a function that finds the longest sequence in a single array. For example, I throw on my knee:

     size_t longest_int_chain( const int *data, size_t size ) { size_t longest = 1; /* максимальная Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠ° */ size_t current = 1; /* Π΄Π»ΠΈΠ½Π° Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΉ Ρ†Π΅ΠΏΠΎΡ‡ΠΊΠΈ */ int c; /* Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ */ size_t i = 0; /* ΠΏΠ°Ρ€Π°Π½ΠΎΠΈΠΊΠΎΠΌ Π±Ρ‹Ρ‚ΡŒ Π½Π΅ Π²Ρ€Π΅Π΄Π½ΠΎ */ if( !data || !size ) { return 0; } c = data[0]; while( ++i < size ) { if( data[i] == c ) { current++; } else { if( current > longest ) { longest = current; } c = data[i]; current = 1; } } return longest; } 

    And when this function will be debugged and will begin to produce the correct result guaranteed - we pass it through the arrays in search of the maximum value.

    • Your function finds the longest sequence, and in the task the largest number of duplicate elements. For example {1,2,1,2,1,3,4,5} - the longest sequence = 1, and the number of repeating elements = 3 (3 units). - andy.37
    • And, for sure :) But the principle and approach does not change. - gottar
    • @ andy.37 without functions, without vectors, without templates, without anything you need) - tkachuk
    • @gottar without functions, without vectors, without templates, without anything you need) - tkachuk
    • one
      @tkachuk completed the answer - andy.37