Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

array and diagonal

Posts: 2Member
Hello! I have two dimensional array - 6 rows,6 columns. I need to switch the biggest number (which is over the main diagonal) with the least number (which is under the main diagonal). It looks like this:

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

so, i need to switch 30 with 7. Any ideas?
· ·

• Posts: 6,349Member
: Hello! I have two dimensional array - 6 rows,6 columns. I need to switch the biggest number (which is over the main diagonal) with the least number (which is under the main diagonal). It looks like this:
:
: 1 2 3 4 5 6
: 7 8 9 10 11 12
: 13 14 15 16 17 18
: 19 20 21 22 23 24
: 25 26 27 28 29 30
: 31 32 33 34 35 36
:
: so, i need to switch 30 with 7. Any ideas?
:
First you'll have to find the location of those numbers. This involves two for-do loops and an if-then. Here is an untested example of finding the biggest number above the main dia.:
[code]
var
BigX, BigY, X, Y: integer;
begin
BigX := 2; { start with the top-left most number as begin value for the max: 2 in the example }
BigY := 1;
for Y := 1 to 6 do
for X := Y to 6 do begin { iterate through top-right half, inc. main dia. }
if (X <> Y) then begin { Remove main dia. }
if Numbers[X, Y] > Numbers[BigX, BigY] then begin
BigX := X; { Check for a larger number }
BigY := Y;
end;
end;
end;
end;
[/code]
A similar code can be constructed for the smallest number below the diagonal. Switching the numbers shouldn't be too difficult, but here is the code anyway:
[code]
Temp := Numbers[BigX, BigY];
Numbers[BigX, BigY] := Numbers[SmallX, SmallY];
Numbers[SmallX, SmallY] := Temp;
[/code]
· ·
• Posts: 7Member
· ·
• Posts: 7Member
· ·
• Posts: 7Member
· ·
• Posts: 7Member
· ·
• Posts: 12Member
[code]

#include
int main()
{
int array[6][6],i,j,k=1,min=999,col_min,row_min,max=-999,col_max,row_max,temp;
for (i=0; i<6; i++){
for (j=0; j<6; j++){
array[i][j]=k;
k++;
}
}
for (i=0; i<6; i++){
for (j=i+1; j<6; j++){
if (array[i][j]>max){
max=array[i][j];
row_max=i;
col_max=j;
}
}
}
for (i=1; i<6; i++){
for (j=0; j<i; j++){
if (array[i][j]<min){
min=array[i][j];
row_min=i;
col_min=j;
}
}
}
temp=array[row_min][col_min];
array[row_min][col_min]=array[row_max][col_max];
array[row_max][col_max]=temp;
for (i=0; i<6; i++){
for (j=0; j<6; j++){
printf("%d ",array[i][j]);
}
printf("
");
}
return 0;
}

[/code]
· ·
• Posts: 12Member
[code]

#include
int main()
{
int array[6][6],i,j,k=1,min=999,col_min,row_min,max=-999,col_max,row_max,temp;
for (i=0; i<6; i++){
for (j=0; j<6; j++){
array[i][j]=k;
k++;
}
}
for (i=0; i<6; i++){
for (j=i+1; j<6; j++){
if (array[i][j]>max){
max=array[i][j];
row_max=i;
col_max=j;
}
}
}
for (i=1; i<6; i++){
for (j=0; j<i; j++){
if (array[i][j]<min){
min=array[i][j];
row_min=i;
col_min=j;
}
}
}
temp=array[row_min][col_min];
array[row_min][col_min]=array[row_max][col_max];
array[row_max][col_max]=temp;
for (i=0; i<6; i++){
for (j=0; j<6; j++){
printf("%d ",array[i][j]);
}
printf("
");
}
return 0;
}

[/code]
· ·
• Posts: 12Member
[code]

#include
int main()
{
int array[6][6],i,j,k=1,min=999,col_min,row_min,max=-999,col_max,row_max,temp;
for (i=0; i<6; i++){
for (j=0; j<6; j++){
array[i][j]=k;
k++;
}
}
for (i=0; i<6; i++){
for (j=i+1; j<6; j++){
if (array[i][j]>max){
max=array[i][j];
row_max=i;
col_max=j;
}
}
}
for (i=1; i<6; i++){
for (j=0; j<i; j++){
if (array[i][j]<min){
min=array[i][j];
row_min=i;
col_min=j;
}
}
}
temp=array[row_min][col_min];
array[row_min][col_min]=array[row_max][col_max];
array[row_max][col_max]=temp;
for (i=0; i<6; i++){
for (j=0; j<6; j++){
printf("%d ",array[i][j]);
}
printf("
");
}
return 0;
}

[/code]
· ·