C ++在数组中查找max(按行)和min(按列)元素 [英] C++ Find max(by row) and min(by column) element in array
问题描述
你好!我需要按行在二维(4,4)数组中找到最小元素,并按列在最大元素中找到它们,然后将它们存储在另一个数组(5,5)中.也许我没有正确解释.
这就是新数组(5,5)的外观:
Hi there! I need to find the mimimum element in two-dimensional(4,4) array by row and maximum element by column and store them in another array (5,5).Maybe I did not explain properly.
That''s how it should look new array (5,5):
1 2 3 4 min
1 2 3 4 min
1 2 3 4 min
m m m m 0
* m-max
所以这是第一个数组:
*m - max
So this is the first array:
int array[4][4];
int i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
cout << "\n array[" << i + 1 << "][" << j + 1 <<"]=";
cin >> array[i][j];
}
}
通过这个,我尝试找到每一行的最小值:
With this I try to find the min of each row:
int min[4];
for (i = 0; i<4; i++) {
min[i] = array[0][i];
for (j = 1; j<4; j++) {
if (min[i]>array[i][j])/
min[i] = array[i][j];
}
}
但我不知道此代码是否正确.我不知道如何提取它们(最少的元素).
并以此尝试创建新数组:
But I do not know if this code is correct. I dont know how to extract them(minimum elements).
And with this i will try to make new array:
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
newarr[i][j]=array[i][j];
newarr[i][5]=max[i];
}
}
for(j = 0; j < 4; j++)
newarr[5][j]=min[j];
这个可以吗?无法检查它,因为我只是找不到最小值和最大值.
Is this okay? There is no way to check it because I just can not find the min and max.
推荐答案
跑得太深了.您只需要自己的时间.
It looks somehow fine, but the assignment of your min/max values are running too deeply nested. You only need it own time.
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
newarr[i][j]=array[i][j];
if( j == 0) newarr[i][4]=max[i];//index error (and not so elegant)
}
}
for(j = 0; j < 4; j++)
newarr[4][j]=min[j];//index error
newarr[4][4]=0;//last element
真正重要的学习编程技巧:
1.使用调试器
2.使用输出(TRACE)
3.用数据编写一些测试. (具有已知结果的样本数据)
祝您好运;-)
Really important tips to learn programming:
1. use a debugger
2. use output (TRACE)
3. write some tests with data. (Sample data with known results)
Good luck ;-)
在这里,我将为您提供一个伪代码,以解决方案...您需要将其转换为C代码...
I am going to give you here a pseudo-code for a solution... you will need to translate it into a C code...
Main:
//----------
// dim the array and intialize its elements to a random value
//----------
dim a[4,4] //--define the array
for i=0 to 3
for j=0 to 3
a[i,j]=random(10) //--initialize elements to random values ranging from 0 to 10... or read from a file or input stream etc.
next
next
//--------
//-- dim an array to hold the maximum values
//-- and initialize its elements to be the top raw of the previous array
//--------
data ma;a[0,0],a[0,1],a[0,2],a[0,3]
//-------
//--- now this is the algorithm you are asking for
//--- it loops through the array row by row
//--- it intializes a temp value to the first element in the row
//--- then in each row it loops through element by element
//--- it finds the minimum element by reassigning the temp value
//--- to the element if the element is smaller than the value of the element
//--- thus it find the minimum for the row
//--- it also compares the element to the value stored in the array of maximums
//--- in its corresponding column... if it is larger then stores that in the array of maximums
//--- it also prints the element and at the end of each row it prints the min
//--- of course instead of printing you can assign to another array element etc.
//--- finally when all is done it prints the array of maximums for each column.
//----------
for i=0 to 3 //-- for each row
mi=a[i,0] //-- init the minumum value to the first element in the row
for j=0 to 3 //-- for each element in the row
print a[i,j]; //-- print it... or store it if you need to in another array
if ma[j] < a[i,j] then ma[j] = a[i,j] //-- if the current element is > than the stored maximum than store it
if mi > a[i,j] then mi = a[i,j] //-- if the current element is < than the minimum then store it
if j==3 then print mi //-- if reached the end of the row then print the minimum value ... or store it if you need to
next j
next i
for i=0 to 3 //-- print the maximums array
print ma[i];
next
end
这就是整个代码:
Well that''s the whole code:
#include <iostream>
using namespace std;
int main() {
int A[4][4];
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++) {
cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
cin >> A[i][j];
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
cout << A[i][j] << "\t";
cout << "\n";
}
{
int min[4];
for (i = 0; i < 4; i++) {
min[i] = A[0][i];
for (j = 1; j < 4; j++) {
if (min[i] > A[i][j])
min[i] = A[i][j];
}
}
int newarr[5][5];
int max[5] = { 1,2,3,4,5 };
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
newarr[i][j] = A[i][j];
newarr[i][5] = max[i];
}
}
for (j = 0; j < 4; j++)
newarr[5][j] = min[j];
cout << newarr[5][j] << "\t";
cout << "\n";
}
}</iostream>
我把随机元素最大化.因为到目前为止我只测试.但是,一旦我启动程序,它只会显示第一个数组正确的内容.新数组应该在哪里显示零.这是调试的结果:
I put random elements to max. Because so far I only test. But once I started my program it show correct only the first array. And where should be the new array it shows zero. Here it is the outcome of the debuging:
5 4 3 1
5 6 7 9
4 2 3 9
4 8 4 6
0
这篇关于C ++在数组中查找max(按行)和min(按列)元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!