Answer the question
In order to leave comments, you need to log in
How to find the largest number in a 2D array?
I'm learning C++ from purecodecpp.com/archives/1158 and found a problem in the comments that I can't solve.
The array filled, divided into one-dimensional and then stalled, I donβt know how to find the largest number.
Here is the code with the condition.
/*
Π ΠΌΠ΅ΡΠ°Π½ΠΈΠΈ ΠΌΠΎΠ»ΠΎΡΠ° ΡΠΎΡΡΡΠ·Π°Π΅ΡΡΡ n ΡΠΏΠΎΡΡcΠΌΠ΅Π½ΠΎΠ². ΠΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· Π½ΠΈΡ
ΡΠ΄Π΅Π»Π°Π» m Π±ΡΠΎΡΠΊΠΎΠ².
ΠΠΎΠ±Π΅Π΄ΠΈΡΠ΅Π»Π΅ΠΌ ΡΠΎΡΠ΅Π²Π½ΠΎΠ²Π°Π½ΠΈΠΉ ΠΎΠ±ΡΡΠ²Π»ΡΠ΅ΡΡΡ ΡΠΎΡ ΡΠΏΠΎΡΡΡΠΌΠ΅Π½, Ρ ΠΊΠΎΡΠΎΡΠΎΠ³ΠΎ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»Π΅Π½ Π½Π°ΠΈΠ»ΡΡΡΠΈΠΉ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ ΠΏΠΎ Π²ΡΠ΅ΠΌ Π±ΡΠΎΡΠΊΠ°ΠΌ.
Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° Π΄ΠΎΠ»ΠΆΠ½Π° Π½Π°ΠΉΡΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° Π² Π΄Π°Π½Π½ΠΎΠΌ ΠΌΠ°ΡΡΠΈΠ²Π΅,
Π° ΡΠ°ΠΊΠΆΠ΅ Π΅Π³ΠΎ ΠΈΠ½Π΄Π΅ΠΊΡΡ (ΡΠΎ Π΅ΡΡΡ Π½ΠΎΠΌΠ΅Ρ ΡΠΏΠΎΡΡΡΠΌΠ΅Π½Π° ΠΈ Π½ΠΎΠΌΠ΅Ρ ΠΏΠΎΠΏΡΡΠΊΠΈ).
ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
ΠΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° ΠΏΠΎΠ»ΡΡΠ°Π΅Ρ Π½Π° Π²Ρ
ΠΎΠ΄ Π΄Π²Π° ΡΠΈΡΠ»Π° n ΠΈ m, ΡΠ²Π»ΡΡΡΠΈΠ΅ΡΡ ΡΠΈΡΠ»ΠΎΠΌ ΡΡΡΠΎΠΊ ΠΈ ΡΡΠΎΠ»Π±ΡΠΎΠ² Π² ΠΌΠ°ΡΡΠΈΠ²Π΅.
ΠΠ°Π»Π΅Π΅ Π²ΠΎ Π²Ρ
ΠΎΠ΄Π½ΠΎΠΌ ΠΏΠΎΡΠΎΠΊΠ΅ ΠΈΠ΄Π΅Ρ n ΡΡΡΠΎΠΊ ΠΏΠΎ m ΡΠΈΡΠ΅Π», ΡΠ²Π»ΡΡΡΠΈΡ
ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΠΌΠΈ ΠΌΠ°ΡΡΠΈΠ²Π°.
ΠΡΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅
ΠΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° Π²ΡΠ²ΠΎΠ΄ΠΈΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°, Π·Π°ΡΠ΅ΠΌ Π½ΠΎΠΌΠ΅Ρ ΡΡΡΠΎΠΊΠΈ ΠΈ Π½ΠΎΠΌΠ΅Ρ ΡΡΠΎΠ»Π±ΡΠ°,
Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΎΠ½ Π²ΡΡΡΠ΅ΡΠ°Π΅ΡΡΡ. ΠΡΠ»ΠΈ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ
ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ²,
ΡΠΎ Π½ΡΠΆΠ½ΠΎ Π²ΡΠ²Π΅ΡΡΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ Π½ΠΎΠΌΠ΅Ρ ΡΡΡΠΎΠΊΠΈ, Π² ΠΊΠΎΡΠΎΡΠΎΠΉ Π²ΡΡΡΠ΅ΡΠ°Π΅ΡΡΡ ΡΠ°ΠΊΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ,
Π° Π΅ΡΠ»ΠΈ Π² ΡΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ°ΠΊΠΈΡ
ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ, ΡΠΎ Π½ΡΠΆΠ½ΠΎ Π²ΡΠ²Π΅ΡΡΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ Π½ΠΎΠΌΠ΅Ρ ΡΡΠΎΠ»Π±ΡΠ°.
ΠΠ΅ Π·Π°Π±ΡΠ΄ΡΡΠ΅, ΡΡΠΎ Π²ΡΠ΅ ΡΡΡΠΎΠΊΠΈ ΠΈ ΡΡΠΎΠ»Π±ΡΡ Π½ΡΠΌΠ΅ΡΡΡΡΡΡ Ρ 0.
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip> // setw(ΠΎΡΡΡΡΠΏ)
using namespace std;
int main() {
setlocale(0, "");
srand(time(NULL));
const int ROWSIZE = 3;
const int COLSIZE = 3;
int arr[ROWSIZE][COLSIZE] = {};
// Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠΌΠ°ΡΡΠΈΠ²Π° Ρ ΠΊΠ»Π°Π²ΠΈΠ°ΡΡΡΡ
/*
for (int rowNum = 0; rowNum < ROWSIZE; rowNum++) {
for (int colNum = 0; colNum < COLSIZE; colNum++) {
cout << "ΠΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, Π²Π²Π΅Π΄ΠΈΡΠ΅ ΠΈΡΡ
ΠΎΠ΄Π½ΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΌΠ°ΡΡΠΈΠ²Π°: ";
cout << rowNum + 1 << "-Ρ ΡΡΡΠΎΠΊΠ° ";
cout << colNum + 1 << "-Ρ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠ° ";
cin >> arr[rowNum][colNum];
}
}
*/
// ΠΠ°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΠΌΠ°ΡΡΠΈΠ² ΡΠ»ΡΡΠ°ΠΉΠ½ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ
for (int rowNum = 0; rowNum < ROWSIZE; rowNum++) {
cout << "Π‘ΡΡΠΎΠΊΠ° " << rowNum + 1 << ": ";
for (int colNum = 0; colNum < COLSIZE; colNum++) {
arr[rowNum][colNum] = 1 + rand() % 9;
cout << setw(2) << arr[rowNum][colNum];
}
cout << endl;
}
// Π Π°Π·Π΄Π΅Π»ΡΠ΅ΠΌ Π΄Π²ΡΠΌΠ΅ΡΠ½ΡΠΉ ΠΌΠ°ΡΡΠΈΠ² Π½Π° ΠΎΠ΄Π½ΠΎΠΌΠ΅ΡΠ½ΡΠ΅
int row[ROWSIZE]{};
int col[COLSIZE]{};
for (int r = 0; r < ROWSIZE; r++) {
row[r] = arr[r - 1][COLSIZE];
cout << row[r] << "|";
for (int c = 0; c < COLSIZE; c++) {
col[c] = arr[c][r];
//cout << col[c] << "|" << endl;
}
}
system("pause");
return 0;
};
Answer the question
In order to leave comments, you need to log in
Think about how to use a loop to iterate over the elements of an array and what and with what you need to compare to determine the maximum value.
The solution to the problem of finding the maximum value is easily searched for by a search engine.
changed your code:
#include <iostream>
#include <string>
#include <ctime>
#include <iomanip> // setw(ΠΎΡΡΡΡΠΏ)
#include <limits>
int main() {
setlocale(0, "");
srand(time(NULL));
const size_t ROWSIZE = 3;
const size_t COLSIZE = 3;
int arrMaxValue = std::numeric_limits<int>::min();
int arr[ROWSIZE][COLSIZE] = {};
// ΠΠ°ΠΏΠΎΠ»Π½ΡΠ΅ΠΌ ΠΌΠ°ΡΡΠΈΠ² ΡΠ»ΡΡΠ°ΠΉΠ½ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ
for (size_t rowNum = 0; rowNum < ROWSIZE; rowNum++) {
std::cout << "Π‘ΡΡΠΎΠΊΠ° " << rowNum + 1 << ": ";
for (size_t colNum = 0; colNum < COLSIZE; colNum++) {
int value = 1 + rand() % 9;
arr[rowNum][colNum] = value;
if (value > arrMaxValue)
arrMaxValue = value;
std::cout << std::setw(2) << arr[rowNum][colNum];
}
std::cout << std::endl;
}
std::cout << "Max value is " << arrMaxValue << std::endl;
return 0;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question