G
G
gbxbro2020-08-09 17:24:03
C++ / C#
gbxbro, 2020-08-09 17:24:03

How to solve this problem on a coursework in C++?

Hello to all programmers. I have such a thing, I just started learning to program, before that I kicked the bullshit in pairs, went to the army and returned from the academy, now I seriously took up my studies, but in a month I can acquire sufficient knowledge, especially since it doesn’t work in C ++, and here we have a coursework, so I decided to turn to the almighty internet, I don’t expect that they will write me a ready-made code with explanations, but I think that someone will be able to explain the technology for solving this problem, I just recently reached the for loops and turn to arrays, I know what functions are and their preliminary declaration. I think somehow it will be possible to solve it with a stretch, finishing off my knowledge of arrays.
5f3006bddf888610513554.png

My thoughts are something like this, create integer variables N and M, an array of numbers will be randomly formed from the number M, then loop it and output N times, then find the largest one through if through comparing elements and move it to the left, all this needs to be done through functions somehow between this cycle, because with each iteration the elements of the array will be updated. Further to compare the first elements and the greatest to throw in the first iteration somehow. Krch tupnyak, help please, there is not enough knowledge ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2020-08-09
@gbxbro

You are GIVEN a matrix. So in the program you need to
1) read the numbers N and M (get 2 variables and read them through cin)
2) Get the N * M matrix. In C++, the standard for arrays is the vector class. A two-dimensional array is a vector of vectors.
vector<vector<int>> a(n);
This will create a vector of n vectors, but they will all be empty. It is necessary during reading (you will have 2 nested loops) before reading the i-th line, the i-th vector should be resized by calling a[i].resize(m). And then it will be possible to read a[i][j]
3) Now, actually, the algorithm. Find the largest element. To do this, enter 3 variables - the current maximum (you can initialize a[0][0]) and its coordinates mx and my (initialize with zeros). Walk through the matrix with two nested loops and, if the current element is greater than the maximum, overwrite the maximum and remember the current loop variables in mx and my.
4) Now swap the 0th line with the line with the maximum. To do this, loop through the columns (from 0 to M-1) and swap a[0][j] and a[mx][j].
5) Now the same, but by columns. Loop from 0 to N-1 and swap elements a[i][0] and a[i][my].
6) Finally, output the matrix with two nested loops.
To change two values, you need a temporary variable, such as tmp.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question