简体   繁体   中英

Finding a row of 2D array in which max sum of elements is c+

I am having trouble with find in which row of 2D array the maximum sum of elements is, Here is the code, could someone give me any suggestions. Thank you !

int sum=0;
   int maximum =0;
   for(int i=0;i<n;i++)
   {
       for(int j=0;j<b[i];j++)
       {
                sum = sum+a[i][j];
       }
       if(sum>maximum)
       {
           maximum=sum;
       }
       cout<<" sum of a row " <<i+1<<'='<<sum<<endl;
       sum=0;
   }
   cout<<maximum;
}
int main()
{
    int a[3][4] = {
            {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
            {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
            {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
    };
    int n =3;
    int b = 4;
    int maximum =0;
    int sum = 0;
    int row = 0;

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<b;j++)
        {
            sum = sum+a[i][j];
        }
        if(sum>maximum)
        {
            maximum=sum;
            row = i;
        }
        cout<<" sum of a row " <<i+1<<'='<<sum<<endl;
        sum=0;
    }
    cout<<maximum << endl;
    cout << row + 1;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM