A
A
Alexandra2021-10-29 14:10:02
C++ / C#
Alexandra, 2021-10-29 14:10:02

How to swap elements of an array?

Hello, I'm solving this problem: Given an integer square matrix. Find the largest element in each
line and swap it with the main diagonal element.
needs to be solved for a static array. How do I swap the main diagonal element with another element? without using pointers

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
User700, 2021-10-29
@User700

Use tag code.
Why is this:

max = a[i][j];
if (a[i][j] = max)
{
a[i][j] = a[i][i];
a[i][i] = max;
}

Just look for max -- max. element and its index jmax. Then change [i,j] to [i,jmax]. See std::swap and the referenced links above.

O
Ocelot, 2021-10-29
@Ocelot

https://duckduckgo.com/?q=c%2B%2B+swap+array+elements

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question