The largest sum of elements

Hello! I created tables n x n and I filled it any real numbers

#include<iostream>
using namespace std;

int main()
{
unsigned int n, max=0;
cin>>n;
float *tab=new float[n];
float **mac=new float*[n];
for(int i=0;i<n;i++)
mac[i]=new float[n];

for(int x=0;x<n;x++)
for(int y=0;y<n;y++)
while( !(cin>>mac[y][x]) )
{
cout<<"ERROR\n";
cin.clear();
cin.sync();
}
for(int x=0;x<n;x++)
for(int y=0;y<n;y++)
tab[x]+=mac[x][y];
for(int x=1;x<n;x++)
if(tab[x]>tab[x-1]) tab[x]=max;
else tab[x-1]=max;

cout<<max;
for(int i=0;i<n;i++)
delete [] mac[i];
delete [] mac;
delete [] tab;

return 0;
}


I have trouble finding the column, which is the largest sum of elements.
Are you able to add all elements in one column?
Then, repeat this for every column and store the results in another array (that is not necessary, but may be more clear). Then find the maximum in that array.
I modified the code:

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
#include<iostream>
using namespace std;

int main()
{
    unsigned int n, colum=1;
    cin>>n;
    float *tab=new float[n];
    float **mac=new float*[n];
    for(int i=0;i<n;i++)
       mac[i]=new float[n];
       
   for(int x=0;x<n;x++)
       for(int y=0;y<n;y++)
             cin>>mac[y][x];
   for (unsigned int i=0; i<n; i++) tab[i] = 0;
       for(int x=0;x<n;x++)
           for(int y=0;y<n;y++)
               tab[x]+=mac[x][y];
   for(int x=1;x<n;x++)
       if(tab[0]<tab[x])
        {
            tab[0]=tab[x];
            colum=x+1;
        }
    cout<<colum;
    for(int i=0;i<n;i++)
       delete [] mac[i];
    delete [] mac;
    delete [] tab;
    system("pause");
    return 0;
}


The problem is that as you type eg 3 x 3 the same value of the items it displays 1 and this is wrong

I have no idea...
how is that wrong? the task was to find the column with greatest sum. If all elements are equal, all sums are equal. So it could output any column. You have 'colum' initialized to 1, so it prints 1. What do you want it to print?
Topic archived. No new replies allowed.