Problem with sum

Write your question here.
can somone help to me with sum? 1 collum sum up correct but second one is divided by 2 collums. How i need suum up all collums not togeher like 1 collum (sr for bad english)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 #include <iostream>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

struct abc {

int k;
double v[99];

};



int main()
{


struct abc abc[99];
int kartai;
double suma[90][90];
ifstream is ("Duomenys.txt");
ofstream os ("Rezultatas.txt");

is>>kartai;

for (int i =0 ; i<kartai; i++)
        {
        is>> abc[i].k;
        cout << " sum " <<abc[i].k << endl;

            for (int j=0;j<abc[i].k;j++)
            {
                is>>abc[i].v[j];
                abc[i].v[j]=1/abc[i].v[j];

                 cout << " vatu " <<abc[i].v[j]<< endl;
            }


        }

for (int ii =0 ; ii<kartai; ii++)
{
    for (int jj=0;jj<abc[ii].k;jj++)
    {
        suma[ii][jj]=abc[ii].v[jj++]+abc[ii].v[jj];


cout <<" total " <<ii <<"     "<< suma[ii][jj]<< " ";
    }


}


//cout <<suma[1][1]<< " ";
//duomenis pajema po kableliu pirma syki

/*

for (int ii =0 ; ii<kartai; ii++)
        {

            for (int jj=0;jj<abc[ii].k;jj++)
            {
                is>>abc[ii].v;
                abc[ii].v+=abc[ii].v;

            }
            cout << ii<< "  pirma suma " <<abc[ii].v<< endl;

        }
/*
                for (int iii =0 ; iii<kartai; iii++)
                {
                abc[iii].v=1/abc[iii].v;
                cout << iii<< "  padalinus antra syki is 1 " <<abc[iii].v<< endl;

                }
*/
            return 0;



}






data:
3
2 15 41
4 1 2 3 4
3 22 11 24
I'm not sure what collum is, so lets try with the example data.

Do you want to compute 3 sums from it?
15 + 41 = sum1
1 + 2 + 3 + 4 = sum2
22 + 11 + 24 = sum3

Hi thanks for helping.


first of all i have to divide these numbers from 1 for example sum1(1/15+1/41)
than this same with sum2 and sum3. after this i have summ it all total(1/sum1+1/sum2+1/sum3)
The inversions are irrelevant to the principle of what to sum up.

The input data seems to contain:
N
datasets

The N is integral value. There are N datasets. One dataset contains:
K values

The K is integral value. One dataset has K values. Are the values all integral? Your program reads them as doubles.

You do compute a sum for each dataset. Therefore, you get N sums.


In your last post you do reveal that once you have the N sums, you will compute the sum of their inverses. That is a separate step.


It is interesting that you include the algorithms header (even though you don't use it) yet you do use static arrays rather than std::vector.

Lets have some fun:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <vector>
#include <numeric>
#include <iostream>

template <typename T>
double invsum( double lhs, T rhs ) { return lhs + 1.0 / rhs; }

int main() {
  std::vector<std::vector<int>> foo { {15, 41}, {1, 2, 3, 4}, {22, 11, 24} };
  std::vector<double> sums( foo.size() );
  for ( size_t dataset = 0; dataset < foo.size(); ++dataset ) {
    const auto & bar = foo[dataset];
    sums[dataset] = std::accumulate( std::begin(bar), std::end(bar), 0.0, invsum<int> );
  }
  for ( auto sum : sums ) {
    std::cout << sum << '\n';
  }
  std::cout << "Total: "
    << std::accumulate( std::begin(sums), std::end(sums), 0.0, invsum<double> )
    << '\n';
 return 0;
}


Last edited on
http://cpp.sh/ here in this url run's perfectly and answer is good but in my program (codebloks (c++)) not even start. What i have to do?

in lane 9 program show me mistace (no maching function for call to 'std::vector<std::vector<int> > :: vector(<brace-enclosed iinitializer list>)'

And in lane 9 can i use ifsream to read these numbers from file txt? foo { {15, 41}, {1, 2, 3, 4}, {22, 11, 24} })



p.s. i am begginer at programming so i don't know much. :) (thanks alot for helping to me)
@Egidijus You have not enabled c++11. c++11 adds support for initializing from a brace list, as you ave on line 9


Go to Toolbar -> Settings -> Compiler
In the "Selected compiler" drop-down menu, make sure "GNU GCC Compiler" is selected
Below that, select the "compiler settings" tab and then the "compiler flags" tab underneath
In the list below, make sure the box for "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" is checked
Click OK to save
Last edited on

how i can change lane 9 to read numbers from txt file? with ifstream? :)
Last edited on
The 'abc' in your program (if that reference is ambiguous, then the blame is on you) and the 'foo' in my program are both "array of arrays". If you can read into abc, then reading into foo should not be terribly difficult.

You should read the reference documentation of std::vector that is on this site. Pay particular attention to member functions reserve() and push_back().
Topic archived. No new replies allowed.