【问题标题】:sum of rows maximum and minimum is not correct. whats the issue?最大和最小行的总和不正确。什么问题?
【发布时间】:2015-06-12 14:39:09
【问题描述】:

我有一个 C 程序。我计算了数组每一行的总和,然后将它们进行比较以找出哪一行是最小总和,哪一行是最大总和。但是我的程序有时会给出正确的输出,但有时会出错。问题出在哪里?请帮我。谢谢

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;


///////Prototypes of functions
void displayMaxMinElement(int array[10][10]);
void fillBoard(int array[10][10]);
void rowSum(int array[10][10]);
void displayBoard(int array[10][10]);


/////Starting main function
main() {

    int board[10][10] = {0};      

    system("cls"); ///To clear previous data on screen
    fillBoard(board);
    displayBoard(board);
    displayMaxMinElement(board);        
    rowSum(board);  

    system("pause>nul");    ///Pausing the program without printing "Press any key to continue"
} 


void displayMaxMinElement(int array[10][10]) {
    int max = 0, min = 100;
    for(int i=0;i<10;i++) {
        for(int j=0; j<10; j++) {

            if(array[i][j] <= min)
                min = array[i][j];

            if(array[i][j] >= max)
                max = array[i][j];
        }
    }    
    cout<<"\n\nArray element with maximum value: "<<max<<endl;
    cout<<"Array element with minimum value: "<<min<<endl<<endl;    
}

void fillBoard(int array[10][10]) {
    srand(time(0));
    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
            array[i][j] = rand()%100+1;
        }    
    }
}   

void rowSum(int array[10][10])
{
     int maxRow=0, minRow=10000;
     int minRowNo, maxRowNo;
     int sum[10]={0};


     for(int i=0; i<10; i++)
             for(int j=0; j<10; j++) {
                     sum[i]=sum[i]+array[i][j];
                    /// cout<<endl<<sum[i]<<"="<<sum[i]<<"+"<<array[i][j];


             }

     for(int i=0; i<10; i++)
             {

                    if(sum[i]<=minRow){
                                   minRow=sum[i];
                                   minRowNo=i;
                                   }
                    else if(sum[i]>=maxRow){
                                   maxRow=sum[i];
                                   maxRowNo=i;
                                   }

                  /// cout<<endl<<"*********"<<sum[i];


              }
     cout<<endl<<"Row"<<minRowNo<<"is minimum and having sum of"<<minRow;
     cout<<endl<<"Row"<<maxRowNo<<"is maximum and having sum of "<<maxRow;

}



void displayBoard(int array[10][10]){
    int *ptr = *array;  

    for(int i = 1; i <= 100; i++) {       
        cout<<*ptr<<"\t";       
        ptr++;

        if(i%10 == 0) 
            cout<<endl;
    }
    cout<<endl;
    ptr = NULL;
}    

【问题讨论】:

  • 它正在运行,但有一些逻辑或隐藏错误
  • 这明明是C++,不是C,应该是int main()
  • else ifrowSum() 中是否正确?
  • @SouravGhosh else if 有什么问题?
  • 你应该提供你的程序给出错误输出的输入。

标签: c++ arrays search


【解决方案1】:

使用 stl,你的代码可能是

void rowSum(const int (&array)[10][10])
{
    int sum[10] = {0};

    for(int i = 0; i < 10; ++i) {
        sum[i] = std::accumulate(std::begin(array[i]), std::end(array[i]), 0);
    }
    auto p = std::min_max_element(std::begin(sum), std::end(sum));
    std::cout << std::endl << "Row" << std::distance(std::begin(sum), p.first) << "is minimum and having sum of" << *p.first;
    std::cout << std::endl << "Row" << std::distance(std::begin(sum), p.second) << "is maximum and having sum of "<< *p.second;
}

【讨论】:

    【解决方案2】:
                 else if(sum[i]>=maxRow){
    

    应该是

                 if(sum[i]>=maxRow){
    

    因为查找 minmax 值是独立的。

    例如,如果 minRow1000 并且 sum[i]20002000 不符合 min 值的条件。但它可以是 max 值的候选者。

    【讨论】:

    • 谢谢。我删除了“其他”,现在工作正常......谢谢
    【解决方案3】:

    看起来正在发生的事情是,在 rowSum 中,当您将行值提供给 minRow 和 maxRow 时,您使用的是 else if() 语句。

    else if(sum[i]>=maxRow){
    

    这是一个问题,因为如果总和是按降序排列的,您的程序会不断地给 minRow 一个新值,但永远不会给 maxRow 赋值。

    当然,如果总和按照特定的顺序,这可以给出正确的答案,但结果将不一致。

    【讨论】:

      猜你喜欢
      • 2015-05-25
      • 2014-03-27
      • 2011-10-03
      • 1970-01-01
      • 2019-08-18
      • 2020-09-02
      • 2019-08-07
      • 1970-01-01
      • 2020-01-08
      相关资源
      最近更新 更多