查找最小值和最大值并将其存储在新数组中 [英] Find min and max and store them in new array

查看:70
本文介绍了查找最小值和最大值并将其存储在新数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了新的主题,因为当我回答我的旧主题时,代码只会破坏我试图在二维(4,4)数组中逐行找到最大元素,并在列中找到最大元素并将它们存储在另一个数组中(5,5).也许我没有正确解释.

这就是新数组(5,5)的外观:
< pre lang ="c ++"> 1 2 3 4分钟
1 2 3 4分钟
1 2 3 4分钟
m m m m 0</pre>

* m-max</pre>

这是代码:

I make new topic because when i answer to my old theme the code just spoils I am trying 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):
<pre lang="c++">1 2 3 4 min
1 2 3 4 min
1 2 3 4 min
m m m m 0</pre>

*m - max</pre>

This is the code:

#include <iostream>

int main()
{
    // Initialised inline to simplify example.
    int A[5][5];

    // Only uses 4x4 of the 5x5 array meaning that you don't need to use two arrays.
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            std::cout << "Enter in value for row " << x << ", column " << y << ".\n";
            std::cin >>  A[x][y];
        }
    }

    std::cout << "Input:" << std::endl;
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            std::cout << A[x][y] << "\t";
        }
        std::cout << "\n";
    }

    // Finds the max down each column in the array
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            if (A[x][4] < A[x][y])
                A[x][4] = A[x][y];
        }
    }

    // Finds the min across the array (along row) 
    for (int y = 0; y < 4; ++y)
    {
        for (int x = 0; x < 4; ++x)
        {
            // Assign the min to a value in that row.
            A[4][y] = A[1][y];
            if (A[4][y] > A[x][y])
                A[4][y] = A[x][y];
        }
    }

    std::cout << "Output:" << std::endl;
    for (int x = 0; x < 5; ++x)
    {
        for (int y = 0; y < 5; ++y)
        {
            std::cout << A[x][y] << "\t";
        }
        std::cout << "\n";
    }

    std::cin.get();
    std::cin.get();
    return 0;



问题是输出不正确:
http://oi65.tinypic.com/n8llz.jpg



The problem is that the output is not correct:
http://oi65.tinypic.com/n8llz.jpg

How to fix it?

推荐答案

问题是元素a [i] [4]和a [4] [i]未被初始化为任何有意义的值并且可能包含内存中已经存在的任何内容.

您需要初始化第五行和第五列

最好的办法是将第5行的元素初始化为与第1行的元素相同...也将第5列的元素初始化为与第1列相同.


还可以针对您的原始问题查看一种更有效的伪代码算法.请参阅此处

C ++查找最大值(按行)和数组中的min(按列)元素 [
The problem is that the elements a[i][4] and a[4][i] are not initialized to any meaningful values and may contain anything that was already in memory.

You need to initialize the 5th row and the 5th column

The best thing would be to initialize the elements of the 5th row to be the same as the elements of the 1st row... also initialize the elements of the 5th column to be the same as the 1st column.


Also have a look at a more efficient pseudo-code algorithm for your original question... see here

C++ Find max(by row) and min(by column) element in array[^]


我的5美分除了解决方案1:

最小值和最大值应初始化的那个值是多少?查看以下定义:
http://www.cplusplus.com/reference/climits [
My 5 cents in addition to Solution 1:

What is that value which min and max should be initialized with? Look at these definitions: http://www.cplusplus.com/reference/climits[^].

For example, for int type, when min is to be found, it should be initialized with INT_MAX; to find max, initialize it with INT_MIN; I hope it should be quite obvious why.

—SA


这篇关于查找最小值和最大值并将其存储在新数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆