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
void main(), this is wrong. - Abyx